Block bad output

A function decides pass or fail

A guardrail returns a pair: True keeps the result, False sends back a short reason.

Instructions ask the model to behave. A guardrail is code that checks what came back.

Press Try it Yourself to run the check on a fixed sentence. Passed True means the answer would ship.

Example

class Result:
    raw = "An agent takes a role, then a task."

def short_enough(output):
    text = output.raw
    if len(text) <= 400:
        return (True, output)
    return (False, "Too long")

passed, value = short_enough(Result())
print("Passed:", passed)
print(value.raw)

Try it Yourself

Example explained

  • (True, output) — The check passed. Keep the result.
  • (False, message) — The check failed. The message says why.

On a real agent run

Attach the same style of function to a Task or agent guardrails field when you move from this demo to your own script.

See the Agents course guardrail lessons for a full AgentTeam example.

Reference: documentation