Skip to content

Structured output ​

structured_output asks the model to fill a Pydantic class. The value is a module:Class import path. The module must be importable from where you run Python. A JSON schema, a ${block} reference, and an inline schema are rejected.

structured_output_prompt is an extra instruction used when the schema is filled.


The schema ​

python
# my_app/schemas.py
from pydantic import BaseModel, Field

class LineItem(BaseModel):
    description: str
    amount: float

class Invoice(BaseModel):
    vendor: str
    total: float
    currency: str = Field(default="USD")
    items: list[LineItem] = Field(default_factory=list)

The agent ​

yaml
openai:
  type: OpenAIModel
  model_id: gpt-4o-mini

extractor:
  type: Agent
  name: Invoice extractor
  system_prompt: Extract the invoice. Do not invent line items.
  model: ${openai}
  tools: []
  structured_output: my_app.schemas:Invoice
  structured_output_prompt: |
    Format the previous response as structured output matching the Invoice schema.
python
from elsai.config import yaml_to_agent

agent = yaml_to_agent("agent.yaml", entry="extractor")
result = agent(
    "Acme Supplies billed $42.50 for 2 notebooks at $12.00 and one pen at $18.50."
)
print(result)
print(result.structured_output)

Set OPENAI_API_KEY before running. result.structured_output is None when the model did not fill the schema.

The schema is filled through a hidden tool named after the class. Do not list that name under tools or under tool authorization.


Copyright © 2026 elsai foundry.