LangGraph middleware validates tool-call args before execution
u/Alternative_Cut_1604 released `ToolArgsValidationMiddleware`, an open-source middleware for LangChain/LangGraph that intercepts malformed LLM tool-call arguments inside the model node and triggers self-correction before any tool executes.
Score breakdown
The middleware moves schema validation to before tool execution and human approval, preventing malformed LLM-generated arguments from causing runtime errors or surfacing broken calls to human reviewers in LangGraph agent workflows.
- 01Validates LLM tool-call arguments inside the model node, before tool execution and before human-in-the-loop approval steps.
- 02On invalid args, appends error `ToolMessage`s and re-invokes the model to self-correct; only the final valid `AIMessage` enters graph state.
- 03Pydantic tools validated with `model_validate`; MCP and dict-schema tools validated with `jsonschema` (soft dependency).
u/Alternative_Cut_1604 released `ToolArgsValidationMiddleware`, a middleware for LangChain/LangGraph that addresses a recurring pain point: LLMs emitting malformed tool-call arguments that sail past the model node and blow up at runtime — or, worse, get surfaced to a human for approval in human-in-the-loop flows despite being obviously broken. The middleware intercepts tool-call arguments inside the model node, before execution and before any approval step, validating them against each tool's schema. On failure, it appends error `ToolMessage`s and re-invokes the model to self-correct, so only the final valid `AIMessage` enters graph state.
A `strip_empty_values` option removes the `null`, `{}`, and `[]` values that Gemini frequently emits for optional fields, replacing cleaned args in place so there is no gap between what is validated and what executes.
Several edge cases are handled explicitly. Batch partial failures are managed so every `tool_call` still receives a matching `ToolMessage` (required by Anthropic, Gemini, and OpenAI), and valid siblings in a batch receive a "not executed" notice so the model re-issues the whole batch. A `strip_empty_values` option removes the `null`, `{}`, and `[]` values that Gemini frequently emits for optional fields, replacing cleaned args in place so there is no gap between what is validated and what executes. Placeholder-string stripping is opt-in to avoid silent data loss (e.g., "NA" being misread as a null rather than the country code for Namibia). The middleware defaults to `on_failure="pass"` (fail-open) but supports `on_failure="raise"` for hard errors. It is positioned as a complement to `ToolRetryMiddleware` (which retries on tool exceptions) and `ModelRetryMiddleware` (which retries on model exceptions), targeting schema violations specifically.
Key facts
- 01Validates LLM tool-call arguments inside the model node, before tool execution and before human-in-the-loop approval steps.
- 02On invalid args, appends error `ToolMessage`s and re-invokes the model to self-correct; only the final valid `AIMessage` enters graph state.
- 03Pydantic tools validated with `model_validate`; MCP and dict-schema tools validated with `jsonschema` (soft dependency).
- 04`strip_empty_values` option removes `null`, `{}`, and `[]` that Gemini emits for optional fields, replacing originals with cleaned args.
- 05Placeholder-string stripping (e.g., dropping 'NA') is opt-in to prevent silent data loss.
- 06Defaults to `on_failure='pass'` (fail-open); `on_failure='raise'` is available for hard errors.
- 07Installable via `pip install langchain-tool-args-validation-middleware`; source at github.com/Serjbory/langchain-tool-args-validation-middleware.
Topics
Summary and scoring are generated automatically from the original article. We always link back to the publisher and never republish images or paywalled content. Last processed Jun 13, 2026 · 08:58 UTC. How this works →