mirror of
https://github.com/langchain-ai/langgraph.git
synced 2026-08-24 16:42:24 +02:00
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"
)
```
---------
Co-authored-by: Sydney Runkle <sydneymarierunkle@gmail.com>