Structured Output
Type-safe agent responses with Pydantic
Tool Context
Inject dependencies into tools
Structured Output
Structured output lets you define a Pydantic model as the expected response format from your agent. The LLM will return data that exactly matches your schema, giving you type-safe, validated data directly.Basic Usage
You can setresponse_type on the agent (as a default for all runs) or pass it per-run:
- Agent-level default
- Per-run override
Per-run
response_type always overrides the agent’s default. This lets you have a sensible default while retaining flexibility.How It Works
When you provide aresponse_type, Slide uses the output-tool pattern:
- Schema as Tool: Your Pydantic model is registered as a special “output tool”
- Forced Tool Call: The LLM is called with
tool_choice="required", ensuring it must call a tool - Validation: When the LLM calls the output tool, its arguments are validated against your schema
- Retry on Failure: If validation fails, an error message is added and the LLM retries
- Tools + Structured Output: Regular tools work alongside structured output in the same conversation
- Reliable Output:
tool_choice="required"forces the model to respond with structured data - Cross-Provider Support: LiteLLM translates
tool_choicefor each provider (OpenAI, Anthropic, Gemini, Bedrock, etc.)
Azure OpenAI Limitation: Azure does not currently support
tool_choice="required".
If using Azure, set drop_params=True on your agent to gracefully fall back.Complex Schemas
Structured output supports all Pydantic features:Default Response Type
You can set a defaultresponse_type on the agent itself:
Automatic Retry on Validation Failure
Sometimes the LLM might return invalid JSON or data that doesn’t match your schema. UseRetryConfig to automatically retry with feedback:
How Retry Works
When validation fails and retry is enabled:- Slide catches the
ValidationErrororJSONDecodeErrorfrom the output tool arguments - A tool result message with the error details is added to the thread
- The LLM is called again with
tool_choice="required"to try again - This repeats until validation succeeds or
max_retriesis exhausted - If all retries fail,
StructuredOutputErroris raised with the validation history
RetryConfig Options
Tool Context (Dependency Injection)
Tool context lets you inject runtime dependencies (databases, API clients, user info) into your tools without hardcoding them.Basic Usage
Context Parameter Convention
Tools receive context through a special first parameter. Use either:ctx: ToolContext(recommended)ctx: Dict[str, Any]context: ToolContextcontext: Dict[str, Any]
Backward Compatibility
Tools without a context parameter continue to work normally:Error Handling
If a tool expects context but none is provided:Use Cases for Tool Context
Database Access
Database Access
User Context
User Context
API Clients
API Clients
Feature Flags
Feature Flags
Combining Features
Structured output and tool context work together seamlessly:Streaming with Structured Output
If you need streaming with structured output, you can:- Stream for real-time feedback, then parse the final response:
- Or use non-streaming mode for structured data:
Best Practices
Schema Design
1
Keep schemas focused
Define schemas for specific use cases rather than trying to capture everything:
2
Use Field constraints
Pydantic validators help the LLM produce correct output:
3
Provide descriptions
Field descriptions help the LLM understand what you want:
Tool Context
1
Use type hints
Even though context is a dict, document expected keys:
2
Validate early
Check for required keys at the start of your tool:
3
Keep context minimal
Only pass what’s needed:
Error Reference
Next Steps
Testing Agents
Test structured output and tool context
Agent API Reference
Complete Agent documentation
RetryConfig API
Retry configuration options
Advanced Patterns
More advanced usage patterns