Skip to main content
Validates all tool requests from the last AIMessage without actually executing the tools. Useful for extraction and structured output use cases where you need to generate output that conforms to a complex schema without losing the original messages and tool IDs.
This class is deprecated. Please use create_agent from langchain.agents with custom tool error handling.
Defined in: langgraph/prebuilt/tool_validator.py:47

Class Definition

Parameters

Sequence[BaseTool | type[BaseModel] | Callable]
required
A list of schemas to validate the tool calls with. These can be any of the following:
  • A pydantic BaseModel class
  • A BaseTool instance (the args_schema will be used)
  • A function (a schema will be created from the function signature)
Callable[[BaseException, ToolCall, type[BaseModel]], str] | None
default:"None"
A function that takes an exception, a ToolCall, and a schema and returns a formatted error string. By default, it returns the exception repr and a message to respond after fixing validation errors.
str
default:"'validation'"
The name of the node.
list[str] | None
default:"None"
A list of tags to add to the node.

Input/Output

list[AnyMessage] | dict[str, Any]
Can be used either in StateGraph with a 'messages' key or with a list of messages.
dict[str, list[ToolMessage]] | list[ToolMessage]
A list of ToolMessage objects with the validated content or error messages.
  • If input is a dict: returns {"messages": [ToolMessage(...)]}
  • If input is a list: returns [ToolMessage(...)]

How It Works

The ValidationNode performs the following steps:
  1. Extracts the last AIMessage from the input
  2. Iterates through all tool calls in that message
  3. For each tool call, validates the arguments against the corresponding schema
  4. Returns ToolMessage objects with:
    • The validated content (as JSON) if validation succeeds
    • An error message if validation fails (with additional_kwargs={"is_error": True})
This node does not actually run the tools, it only validates the tool calls. This is useful for extraction and other use cases where you need to generate structured output that conforms to a complex schema without losing the original messages and tool IDs (for use in multi-turn conversations).

Usage Example

Re-prompting for Valid Response

Custom Error Formatting

With Multiple Schemas

Using Functions as Schemas

Integration with StateGraph

Properties

dict[str, type[BaseModel]]
Mapping from schema name to BaseModel class. This is populated during initialization and contains all the schemas that can be validated.

Validation Behavior

Successful Validation

When validation succeeds:
  • Returns a ToolMessage with the validated content serialized as JSON
  • The tool_call_id matches the original tool call ID
  • No additional_kwargs are set

Failed Validation

When validation fails:
  • Returns a ToolMessage with the error message from format_error
  • The tool_call_id matches the original tool call ID
  • Sets additional_kwargs={"is_error": True} to indicate an error

Common Use Cases

1. Structured Data Extraction

Validate that extracted information matches the expected schema before processing:

2. Form Input Validation

Ensure user inputs conform to required formats:

3. Multi-Step Extraction with Retry

Validate extraction and re-prompt on errors:

See Also