> ## Documentation Index
> Fetch the complete documentation index at: https://docs.orxhestra.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Structured Output

> Force agents to return validated Pydantic objects instead of free-form text. Uses output_schema with automatic JSON parsing and fallback.

Force an agent to return a typed Pydantic object instead of free-form text. Pass `output_schema` to `LlmAgent`.

```python theme={null}
from pydantic import BaseModel, Field
from orxhestra import LlmAgent
from orxhestra.events.event import Event, EventType

class CompanyAnalysis(BaseModel):
    name: str = Field(description="Company name")
    industry: str = Field(description="Primary industry")
    strengths: list[str] = Field(description="Key strengths")
    risks: list[str] = Field(description="Key risks")
    recommendation: str = Field(description="Buy, Hold, or Sell")
    confidence: float = Field(description="Confidence score 0-1")

agent = LlmAgent(
    name="AnalystAgent",
    model=model,
    tools=[get_financials, get_news_sentiment],
    output_schema=CompanyAnalysis,
    instructions="You are a financial analyst.",
)
```

The parsed object is available on `event.data` when it's the final response:

```python theme={null}
async for event in agent.astream("Analyze Apple", ctx=ctx):
    if event.is_final_response():
        analysis = event.data  # CompanyAnalysis instance
        print(f"{analysis.name}: {analysis.recommendation} ({analysis.confidence:.0%})")
```

## How it works

1. `PydanticOutputParser.get_format_instructions()` is appended to the system prompt
2. When the LLM responds, `PydanticOutputParser.parse()` extracts and validates JSON
3. If direct parsing fails, `with_structured_output()` is used as a fallback
4. Works with streaming and multi-agent compositions
