Why Use Schemas?
Without schemas, agents return unstructured strings. With schemas:- Type safety: Get typed objects instead of strings
- Validation: Catch malformed outputs early
- LLM guidance: The model knows exactly what format to produce
- IDE support: Autocomplete and type checking
Output Schemas
Define what your agent should return:Input Schemas
Define the expected input structure:Schema Field Types
Basic Types
Optional Fields
Enums and Literals
Nested Objects
Field Descriptions
Always add descriptions to help the LLM understand the expected values:Validation
Schemas validate outputs automatically. Invalid outputs raise errors:Complex Example
A complete example with nested schemas:Best Practices
- Always add descriptions: Help the LLM understand what each field should contain
- Use appropriate types: Be specific (
intvsfloat,list[str]vslist) - Set sensible defaults: For optional fields that have common values
- Validate ranges: Add constraints for numeric fields
- Keep schemas focused: Don’t try to capture everything in one schema
Next Steps
Execution Loop
How agents think and act
Hooks
Monitor schema validation