Skip to main content
For the full guide, see Structured output.
This page works best after Examples: Sessions. Start there if you have not already seen the basic create/turn/result flow.

Basic extraction

Provide an output_schema and the agent will extract validated JSON after the agentic loop completes.

Next step

Schema from file

The CLI can load a schema from a JSON file instead of inline.
The CLI detects files by checking if the value is an existing path. The file can contain a raw JSON Schema or the wrapper format with explicit name, strict, compat, and format fields.

Retries

When validation fails, the agent retries the extraction turn with error feedback. The default is 2 retries (3 total attempts).

Compatibility mode

Schemas are normalized across providers. The compat setting controls how unsupported JSON Schema features are handled during provider-specific lowering.
Warnings are included in the response as schema_warnings. The same schema works with Anthropic, OpenAI, and Gemini — provider-specific lowering is handled transparently.

Read the result

The structured output appears in the response alongside the committed main-turn text. The extraction JSON does not replace text.
Once provider schema compilation succeeds, event subscribers see run_completed for the main output with extraction_required: true, followed by exactly one extraction_succeeded or extraction_failed event. Extraction attempts do not stream as ordinary assistant text and do not publish extra turn_completed events. A schema-compilation failure emits extraction_failed directly, without a preceding success-shaped turn_completed or run_completed event.