Skip to main content
Structured outputs let agents return data in a predefined format instead of natural language. This is essential for building reliable data extraction workflows, APIs, and integrations.

Defining structured outputs

Use Pydantic models to define the output schema:

Using with agent.run()

The result field contains a fully validated Pydantic model instance:
Output:
The result is type-safe: you get autocomplete, validation, and runtime type checking.

Using with agent.stream()

When streaming, the output is JSON that you can parse into your Pydantic model:
Output:

Complex nested structures

Structured outputs support nested models, lists, and enums:

Provider compatibility

Not all model providers support structured output.
  • Some models don’t support it at all
  • Some models support structured outputs only when no tools are present

Fallback behavior

When a model doesn’t natively support structured outputs, Polos automatically:
  1. Generates the natural language response
  2. Makes an additional LLM call to structure the output according to your schema
  3. Returns the validated Pydantic model
This ensures your code works consistently across all providers, though it may consume extra tokens for models without native support.

Use cases

Data extraction

Form processing

API responses

Content categorization