Structured outputs use a Pydantic BaseModel or JSON schema to enforce a response format on your agent, giving you typed, integration-ready data without extra parsing or post-processing. Use them when agent responses feed directly into external systems like APIs or databases, or when extracting structured information from unstructured content like documents and emails.
Define a schema with Pydantic
Define a Pydantic model in the response_format parameter:
Endpoint: POST /v1/threads/{thread_id}/runs
Define nested Pydantic objects
You can also define multiple levels of Pydantic classes:
Endpoint: POST /v1/threads/{thread_id}/runs
Handle validation errors
When the agent cannot map its response to the defined schema, a validation error is returned.
Endpoint: POST /v1/threads/{thread_id}/runs
If you receive a validation error, handle the error as shown:
Improve schema accuracy
When using structured outputs, we recommend the following:
- Keep schemas simple and focused. Complex structures with excessive fields or deep nesting can reduce model accuracy. Limit nesting to one level deep. While multiple levels are supported, single-level nesting produces the most consistent results.
- Use clear field names. Choose descriptive field names that help the model accurately map response content to your schema. For example,
customer_email is clearer than email, and total_price_usd is clearer than price.
- Use few-shot prompting. Include examples in your agent’s instructions to guide accurate field mapping. Sample inputs and expected outputs help the model understand how to structure its response.
- Test your schemas thoroughly. Validate your schemas against real queries and edge cases before deploying. Test with a range of input types to ensure consistent results.
Last modified on June 26, 2026