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. output_type controls how the conversation transcript (agent.short_memory) is formatted when agent.run() returns — it is not a schema parser. In particular:
  • "str" / "string" — the full conversation as one string
  • "str-all-except-first" (default) — the conversation, excluding the first (system prompt) message, joined into one string
  • "final" / "last" — just the content of the last message (a plain string) — this is usually what you want when you also configure a list_base_models/tool_schema, since the model’s final response text is where schema-shaped JSON ends up
  • "json" — the full conversation history serialized as a JSON string (an array of {"role", "content", ...} message dicts, not a single schema-shaped object)
  • "dict" / "dictionary" — the full conversation history as a Python list of message dicts (despite the name, this is a list, not a single dict)
  • "list" — the conversation as a list of message dicts
  • "yaml" — the conversation history as a YAML string
  • "xml" — the conversation history as an XML string
  • "all" — the conversation as a string (same as "string")
  • "dict-all-except-first" — all messages except the first, as a list of message dicts. This is the default output_type used internally by several multi-agent harnesses (HeavySwarm, HierarchicalSwarm, SwarmRouter, LLMCouncil, PlannerWorkerSwarm, and others)
  • "list-final" — the content of the last message, wrapped in a single-item list
  • "dict-final" — the content of the last message, as a (content, content) tuple

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

output_type="json" serializes the entire conversation history (a list of {"role", "content", ...} message dicts) to a JSON string — it does not extract a single task-specific object with your own keys. For a task-specific structured payload, combine list_base_models/tool_schema with output_type="final" and parse the model’s final text yourself, as shown below.

Dictionary Output

output_type="dict" (or "dictionary") returns the full conversation history as a Python list of message dicts, not a single dict keyed by your schema’s fields. Index it as result[-1]["content"] to get the last message, then parse that content yourself.

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 formatting: swarms/utils/history_output_formatter.py
  • Pydantic integration: swarms/tools/pydantic_to_json.py
  • Tool/list-base-model schema handling: swarms/structs/agent.py:3059-3087 (handle_tool_schema_ops)