Skip to main content
Structured outputs enable agents to return responses in a specific format, such as JSON objects, lists, or Pydantic models. This is essential for integrating agents into applications and workflows.

Output Types

Swarms supports multiple output formats through the output_type parameter:

JSON Schema Output

Basic JSON Schema

Use Pydantic models to define structured output schemas:

Multiple Output Schemas

Define multiple possible output formats:

Tool Schema

Using tool_schema Parameter

Define output structure using tool_schema:

Complex Output Structures

Nested Models

Lists and Arrays

Validation and Constraints

Field Validation

Enums and Choices

Output Processing

JSON Output

Dictionary Output

Working with Responses

Pydantic Model Response

Function Calling Response

When tools are used, responses include function calls:

Best Practices

1. Use Descriptive Field Names

2. Add Field Descriptions

3. Use Appropriate Constraints

4. Provide Examples in Descriptions

Next Steps

Agent Tools

Add tools to extend agent capabilities

Creating Agents

Learn how to create agents

Reference

  • Output type handling: swarms/structs/agent.py:396-500
  • Pydantic integration: swarms/tools/pydantic_to_json.py
  • Tool schema: swarms/structs/agent.py:624-628