Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions agents/usage/agent-with-structured-output.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,14 @@ Use `output_schema` to get structured, typed responses you can trust. The agent
</Step>

</Steps>

## Key Concepts

- **Pydantic `BaseModel`:** Defines the output structure with typed fields
- **`output_schema=MyModel`:** Pass the Pydantic model class (not instance) to constrain agent output
- **`response.content`:** Returns a typed Pydantic object, not a string
- **Field names = JSON keys:** Field names like `ticker` become keys in the output JSON
- **`Field.description`:** Guides the LLM on what to generate for that field (not a key)
- **Field types → JSON Schema:** Python types (`str`, `float`, `List[str]`) are converted to JSON Schema types (`"string"`, `"number"`, `"array"`)
- **Conversion flow:** Pydantic model → JSON schema → Structured Output API → Validated Pydantic object
- **Works with tools:** Agent calls tools during execution, then formats the final response according to the schema