Validates that output is valid JSON matching an optional schema.
This guardrail ensures that LLM output is properly formatted JSON, which is useful when requesting structured data from the agent.
The schema is a deliberately small subset of JSON Schema:
required: the listed fields must be present on the top-level object.
properties.<field>.type: when a top-level field is present, its value must have that type. The types are string, number, integer (a number with no fractional part), boolean, object, array and null; a list such as ["string", "null"] accepts any of its types. A field the document does not contain is not checked (list it under required to demand it).
Everything else in a schema is ignored: nested properties, items, enum, format, and a type that is not one of the names above, so a schema this validator cannot judge never rejects output. Every field whose type does not match is reported, in the order the schema lists them.
Value parameters
schema
Optional JSON schema to validate against (minimal subset)
Validates value inside an agent's hook, as validate(value) does, with the hook's context. A guardrail that calls a model overrides this to call through context.accounted, so the call is counted in the thread's usage and on the run's terminal event even when the task fails (design §4.22); one that combines guardrails passes context on to each. GuardrailMiddleware and GuardrailReviewMiddleware call this form.
Validates value inside an agent's hook, as validate(value) does, with the hook's context. A guardrail that calls a model overrides this to call through context.accounted, so the call is counted in the thread's usage and on the run's terminal event even when the task fails (design §4.22); one that combines guardrails passes context on to each. GuardrailMiddleware and GuardrailReviewMiddleware call this form.