Skip to content

Response Types

Response types define structured output formats for agents. They can be defined directly in YAML or imported from Python code.

Type Safety

While YAML configuration is convenient, defining response types as Pydantic models in Python code provides better type safety, IDE support, and reusability:

from pydantic import BaseModel

class AnalysisResult(BaseModel):
    success: bool
    issues: list[str]
    severity: str

Response Configuration

Each response definition includes:

responses:
  MyResponse:
    response_schema:  # Schema definition (required)
      type: "inline"  # or "import"
      fields:
        answer:
          type: str
    description: "Optional description of the response"
    result_tool_name: "final_result"  # Tool name for result creation
    result_tool_description: "Create the final result"  # Tool description
    output_retries: 3  # Number of validation retries

Inline Responses

Define response structure directly in YAML:

# yaml-language-server: $schema=https://raw.githubusercontent.com/phil65/agentpool/refs/heads/main/schema/config-schema.json
responses:
  WebResult:
    response_schema:
      type: inline
      fields:
        success:
          type: bool
          description: "Whether operation succeeded"
        url:
          type: str
          description: "URL that was processed"
        attempts:
          type: int
          description: "Number of attempts made"
          constraints:
            ge: 1
            le: 5
  CodeAnalysis:
    response_schema:
      type: "inline"
      description: "Code analysis results with issues"
      fields:
        issues:
          type: "list[str]"
          description: "List of found issues"
        severity:
          type: "str"
          description: "Overall severity level"
    result_tool_name: "create_analysis"
    result_tool_description: "Create code analysis result"
  ### Complex Response
  DataProcessingResult:
    response_schema:
      type: "inline"
      description: "Complex data processing result"
      fields:
        records_processed:
          type: "int"
          description: "Number of processed records"
        errors:
          type: "list[str]"
          description: "List of errors if any"
        metrics:
          type: "dict[str, float]"
          description: "Processing metrics"

Imported Responses

Import response types from Python code:

Python Type Import

responses:
  AdvancedAnalysis:
    response_schema:
      type: "import"
      import_path: "myapp.types:AnalysisResult"
  MetricsResult:
    response_schema:
      type: "import"
      import_path: "myapp.analysis:MetricsResponse"

Using Response Types

Assign to Agent

agents:
  analyzer:
    model: "openai:gpt-5"
    output_type: "CodeAnalysis"  # Reference response by name

Inline Definition

An agent can also define its response inline, with the same fields as an entry of responses (a response_schema without type is an inline schema):

agents:
  classifier:
    model: "openai:gpt-5-mini"
    output_type:
      response_schema:
        fields:
          label:
            type: str
            description: "Assigned category"
          confidence:
            type: float
      result_tool_name: "submit_label"

Available Field Types

  • str: Text strings
  • int: Integer numbers
  • float: Floating point numbers
  • bool: Boolean values
  • list[type]: Lists of values (e.g., list[str], list[int])
  • dict[key_type, value_type]: Dictionaries
  • datetime: Date and time values
  • Custom types through imports

Configuration Reference

StructuredResponseConfig

Base class for response definitions.

Docs: https://phil65.github.io/agentpool/configuration/responses/

StructuredResponseConfig (YAML)
1
2
3
4
5
6
7
8
9
MyResponse:
  response_schema:  # A model describing the response schema.
    type: import  # Inline response definition.
    import_path: pydantic_ai:BinaryImage
    description: null  # A description for this response definition.
  description: null  # A description for this response definition.
  result_tool_name: final_result  # The tool name for the Agent tool to create the structured response.
  result_tool_description: null  # The tool description for the Agent tool to create the structured response.
  output_retries: null  # Retry override. How often the Agent should try to validate the response.

InlineSchemaDef

Inline definition of schema.

Allows defining response types directly in the configuration using: - Field definitions with types and descriptions - Optional validation constraints - Custom field descriptions - Field dependencies and relationships

Example: type: inline fields: success: {type: bool, description: "Operation success"} message: {type: str, description: "Result details"} payment_method: {type: str, description: "Payment method used"} card_number: { type: str, description: "Credit card number", dependent_required: { "payment_method": ["card_expiry", "card_cvc"] } }

InlineSchemaDef (YAML)
response_schema:
  type: inline  # Inline response definition.
  fields:  # A dictionary containing all fields.
    age:
      type: int
      description: User age
      values: null
      default: null
      title: null
      pattern: null
      min_length: null
      max_length: null
      gt: null
      ge: 0.0
      lt: null
      le: null
      multiple_of: null
      literal_value: null
      examples: null
      optional: false
      json_schema_extra: null
      field_config: null
      dependent_required: {}
      dependent_schema: {}
      constraints: {}
    name:
      type: str
      description: User name
      values: null
      default: null
      title: null
      pattern: null
      min_length: null
      max_length: null
      gt: null
      ge: null
      lt: null
      le: null
      multiple_of: null
      literal_value: null
      examples: null
      optional: false
      json_schema_extra: null
      field_config: null
      dependent_required: {}
      dependent_schema: {}
      constraints: {}
  description: null  # A description for this response definition.

ImportedSchemaDef

Response definition that imports an existing Pydantic model.

Allows using externally defined Pydantic models as response types. Benefits: - Reuse existing model definitions - Full Python type support - Complex validation logic - IDE support for imported types

Example: responses: AnalysisResult: type: import import_path: myapp.models.AnalysisResult

ImportedSchemaDef (YAML)
1
2
3
4
response_schema:
  type: import  # Import-path based response definition.
  import_path: myapp.models.User  # The path to the pydantic model to use as the response type.
  description: null  # A description for this response definition.