Guardrail in code

Attach a check

The same function style works on a Task or agent guardrails field when you move from the demo to your own script.

Open the Agents guardrail lessons when you need AgentTeam and task-level examples.

Example

def no_empty(output):
    text = getattr(output, "raw", "") or ""
    if text.strip():
        return (True, output)
    return (False, "Empty answer")

class Out:
    raw = "ok"

print(no_empty(Out()))

Try it Yourself

Example explained

  • output.raw — The text the guardrail inspects.
  • (True, output) — Keep and continue.
  • (False, reason) — Stop and surface the reason.

Reference: documentation