Skip to main content
Structured output ensures agents return responses in a defined, validated format. Rather than receiving free-form text that you need to parse and validate yourself, you get type-safe data objects (Pydantic models, dataclasses, or TypedDict) that are immediately usable in your application. This eliminates the need for manual parsing, reduces errors, and provides type safety throughout your codebase. Common use cases include extracting structured data from documents, parsing user input, generating API responses, and building data pipelines.
Structured output works with most models. DeepSeek and Qwen do not support structured output. Only Anthropic, OpenAI, xAI, and Cohere support tools + structured output together.

Schema Types

Define your response schema using Pydantic models, dataclasses, or TypedDict. Each has different strengths:
from pydantic import BaseModel
from typing import Optional

class Product(BaseModel):
    name: str
    price: float
    category: str
    in_stock: bool
    description: Optional[str] = None

Non-Streaming

Get structured responses using run() or arun() with the output_type parameter:
from pydantic import BaseModel
from hypertic.agents import Agent
from hypertic.models import OpenAIChat

class Product(BaseModel):
    name: str
    price: float
    category: str
    in_stock: bool

model = OpenAIChat(model="gpt-5.2")

agent = Agent(
    model=model,
    output_type=Product
)

# Non-streaming structured output
response = agent.run(
    query="Wireless headphones, priced at $99.99, in the Electronics category, currently in stock. Great sound quality and noise cancellation."
)
print(response.content)
print(response.metadata)

Streaming

Stream structured responses using stream() or astream():
from pydantic import BaseModel
from hypertic.agents import Agent
from hypertic.models import OpenAIChat

class Product(BaseModel):
    name: str
    price: float
    category: str
    in_stock: bool

model = OpenAIChat(model="gpt-5.2")

agent = Agent(
    model=model,
    output_type=Product
)

# Streaming structured output
for event in agent.stream(
    query="Wireless headphones, priced at $99.99, in the Electronics category, currently in stock. Great sound quality and noise cancellation."
):
    if event.type == "content":
        print(event.content, end="", flush=True)
Response Format: When using structured output, response.content is an instance of your schema:
from pydantic import BaseModel
from hypertic.agents import Agent
from hypertic.models import OpenAIChat

class Product(BaseModel):
    name: str
    price: float
    category: str
    in_stock: bool

model = OpenAIChat(model="gpt-5.2")
agent = Agent(model=model, output_type=Product)

response = agent.run("Extract product info: Wireless headphones, $99.99, Electronics, in stock")

# response.content is a Product instance
product = response.content
print(product.name)        # "Wireless headphones"
print(product.price)       # 99.99
print(product.category)   # "Electronics"
print(product.in_stock)   # True