[PR #6002] feat(prebuilt): structured output error handling with configurable retry policy #4781

Closed
opened 2026-02-20 17:50:44 -05:00 by yindo · 0 comments
Owner

Original Pull Request: https://github.com/langchain-ai/langgraph/pull/6002

State: closed
Merged: Yes


This PR adds error handling and retry mechanisms for create_agent structured output via a handle_errors parameter in ToolOutput.

Changes:

  • Adds MultipleStructuredOutputsError exception for when models incorrectly call multiple structured output tools simultaneously
  • Adds StructuredOutputParsingError exception for when tool arguments fail to parse according to the schema
  • Implements automatic error handling logic that re-prompts the model with a configurable error message when structured output failures occur via handle_errors policy in ToolOutput:
class ToolOutput:
    ...
    handle_errors: Union[
        bool,                         # True: retry all, False: no retry
        str,                          # Custom static error message for all errors
        type[Exception],              # Retry only this exception type
        tuple[type[Exception], ...],  # Retry only these exception types
        Callable[[Exception], str],   # Custom callable returning error message
    ]
    """Error handling strategy. Default: True (retry on all error types with default error message)"""

Examples:

# Retry all errors
ToolOutput(WeatherReport)

# No retry
ToolOutput(WeatherReport, handle_errors=False)

# Custom message for all errors
ToolOutput(WeatherReport, handle_errors="Please provide valid data")

# Only retry specific error type
ToolOutput(WeatherReport, handle_errors=StructuredOutputParsingError)

# Multiple error types
ToolOutput(WeatherReport, handle_errors=(MultipleStructuredOutputsError, StructuredOutputParsingError))

# Custom logic
ToolOutput(
    Union[WeatherReport, LocationInfo],
    handle_errors=lambda e: "Only one response please" if isinstance(e, MultipleStructuredOutputsError) else "Invalid format"
)
**Original Pull Request:** https://github.com/langchain-ai/langgraph/pull/6002 **State:** closed **Merged:** Yes --- This PR adds error handling and retry mechanisms for create_agent structured output via a `handle_errors` parameter in `ToolOutput`. Changes: * Adds `MultipleStructuredOutputsError` exception for when models incorrectly call multiple structured output tools simultaneously * Adds `StructuredOutputParsingError` exception for when tool arguments fail to parse according to the schema * Implements automatic error handling logic that re-prompts the model with a configurable error message when structured output failures occur via `handle_errors` policy in `ToolOutput`: ```python class ToolOutput: ... handle_errors: Union[ bool, # True: retry all, False: no retry str, # Custom static error message for all errors type[Exception], # Retry only this exception type tuple[type[Exception], ...], # Retry only these exception types Callable[[Exception], str], # Custom callable returning error message ] """Error handling strategy. Default: True (retry on all error types with default error message)""" ``` Examples: ```python # Retry all errors ToolOutput(WeatherReport) # No retry ToolOutput(WeatherReport, handle_errors=False) # Custom message for all errors ToolOutput(WeatherReport, handle_errors="Please provide valid data") # Only retry specific error type ToolOutput(WeatherReport, handle_errors=StructuredOutputParsingError) # Multiple error types ToolOutput(WeatherReport, handle_errors=(MultipleStructuredOutputsError, StructuredOutputParsingError)) # Custom logic ToolOutput( Union[WeatherReport, LocationInfo], handle_errors=lambda e: "Only one response please" if isinstance(e, MultipleStructuredOutputsError) else "Invalid format" ) ```
yindo added the pull-request label 2026-02-20 17:50:44 -05:00
yindo closed this issue 2026-02-20 17:50:44 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langchain-ai/langgraph#4781