Skip to main content

Overview

StructuredOutputError is raised when the agent fails to produce valid structured output after exhausting all retry attempts. It provides detailed information about what went wrong, including validation errors and the last LLM response.

Class Definition

Properties

str
A human-readable description of the failure, including the number of attempts made.
List[Dict[str, Any]]
List of Pydantic validation errors or custom error dictionaries explaining what was invalid.For Pydantic validation errors, each dict typically contains:
  • type: The error type (e.g., "missing", "type_error")
  • loc: The location in the schema where the error occurred
  • msg: A human-readable error message
  • input: The invalid input value
Any
The raw content from the last LLM response before the error was raised. Useful for debugging what the LLM actually returned.

Basic Usage

Error Types

JSON Decode Errors

When the LLM returns invalid JSON:

Schema Validation Errors

When JSON is valid but doesn’t match the Pydantic schema:

Unexpected Errors

For other errors during structured output processing:

Handling Strategies

Graceful Fallback

Retry with Simpler Schema

Logging and Monitoring

Common Causes

Debugging Tips

  1. Check the last response: The last_response field shows exactly what the LLM returned:
  1. Inspect validation errors: The errors show exactly what failed:
  1. Test your schema manually:

See Also