[KongchangAI]
· 2 min read· 1,250 words

How to Solve LangChain Agent Tool Schema Hallucinations with a Mock Gateway

How to Solve LangChain Agent Tool Schema Hallucinations with a Mock Gateway

Insert a Mock Gateway before LangChain tools to catch schema hallucinations with AJV validation and structured error feedback.

Multi-step agents frequently produce JSON Schema parameter hallucinations during tool calls — mismatched types or formats that cause real API failures, waste quota, and leave the model unable to self-correct from generic 400 errors. A LangChain community developer proposed a "virtual mock gateway" architecture: a validation layer that uses AJV to check payloads before any real code runs, returning clean mock responses on success or structured, field-specific errors on failure. This turns crashes into actionable feedback signals the LLM can read and act on, lowering dev costs and improving agent resilience. The approach is packaged as MockAgent and complements Pydantic validators and LangGraph fallback nodes.

Most developers building multi-step agent loops have run into this scenario: the model executes the first five steps flawlessly, then suddenly crashes on step six — hallucinating an incorrect parameter type, like passing the amount field as the string "$50" instead of the integer 50. This kind of tool schema drift in tool calls is becoming a subtle but surprisingly common pain point in agent development. A Reddit user in the r/LangChain community shared their team's architectural solution, and it's worth unpacking in detail.

reddit source: How we stopped our LangChain agents from hallucinating tool JSON schemas

The Root Cause: Parameter Hallucinations in Tool Calls

When agents use structured tool outputs or custom @tool functions, the call parameters generated by the LLM don't always conform to the tool's defined JSON Schema. The model may deviate in type, format, or even field naming — and these errors typically don't surface until they actually hit the API endpoint.

The original poster identified two core friction points:

The first is wasted cost and rate limit consumption. In development or staging environments, binding agents directly to real endpoints means every failed call burns real API quota or triggers rate limits — all just to discover that "the LLM passed an invalid parameter."

The second is silent state collapse. Third-party APIs typically return a generic 400 Bad Request, which gives the model nothing useful for self-correction. When an agent receives an error with no detail, it can't determine which parameter failed or how to adjust — so the entire agent loop stalls or spirals into repeated failures.

JSON Schema is a declarative specification for describing and validating JSON data structures. It defines field names, data types (string, integer, boolean, etc.), required fields, and format constraints to specify what valid data looks like. In agent frameworks like LangChain, a tool's parameter interface is exposed to the LLM as a JSON Schema — a "contract" the model must follow when generating call parameters. Schema drift refers to the model's generated parameters deviating from this contract. This isn't a misunderstanding of the tool's functionality; it's imprecise adherence to structural rules. Since LLMs are fundamentally probabilistic text generators, subtle type deviations (like integers becoming strings, or missing nested fields) become increasingly common as steps accumulate in long contexts or multi-turn conversations — which is the fundamental reason multi-step agent loops are more fragile than single calls.

The Architecture: Adding a Virtual Mock Gateway in Front of Tools

The author's core idea is this: during development, don't bind agents directly to real endpoints. Instead, introduce a virtual mock gateway in front of tool definitions. This gateway acts as a gatekeeper, with the following flow:

Validate First, Execute Later

  • Pre-define the expected JSON Schema for each tool inside the mock tool gateway;
  • When the agent calls a tool, the gateway first checks the payload with an AJV Schema validator — before any real code executes;
  • If validation passes, the gateway immediately returns a clean mock JSON response, letting the agent loop continue without delay;
  • If validation fails, the gateway returns a structured 400 error that explicitly tells the LLM what type mismatch occurred.

The cleverest part of this design is the "readability" of the error feedback. A generic 400 error is a black box to the model, but a structured error is like handing the model a correction cheat sheet — it can read the error message in the next turn and automatically fix the parameters. This transforms what would have been a crash into an opportunity for the agent to self-iterate.

AJV (Another JSON Validator) is the most widely used JSON Schema validation library in the JavaScript/Node.js ecosystem, known for its high performance and complete support for the JSON Schema specification. It compiles JSON Schemas into optimized validation functions that can perform comprehensive checks on any JSON payload — including types, formats, enum values, and required fields — in milliseconds, generating detailed error reports that pinpoint exactly which field violated which constraint. In the mock gateway context, AJV acts as a "type guard" between the agent and the real tool. Its error messages (e.g., "$.amount" should be integer, got string) are structured and semantic enough to be read directly by the LLM as context, enabling targeted parameter correction in the next reasoning step — without any human intervention.

From Architecture to Tooling

The author went further and packaged this setup into a web gateway tool called MockAgent, allowing teams to mock endpoints without writing Express servers from scratch, and to visually track agent trajectory errors. For teams frequently debugging multi-step agents, visual trajectory tracking can significantly reduce the cost of pinpointing issues.

Agent trajectory refers to the complete sequence of actions an agent loop takes from its initial state to its terminal state — including each step's reasoning output, tool call parameters, tool return values, and state changes. In multi-step agents, a single-step error can "contaminate" subsequent steps through state propagation, meaning the root cause of a final failure can be far removed from where it manifests. Visual trajectory tracking tools present this execution chain as a timeline or graph, letting developers immediately see at which step and which tool call the parameters started going wrong — reducing debugging time from "needle in a haystack" to precise localization. Community tools like LangSmith and LangFuse offer similar trajectory observability capabilities and are essential infrastructure for debugging production-grade agents.

The Value and Limits of This Approach

From an engineering standpoint, this solution essentially moves "Schema validation" from an implicit, post-hoc runtime check to an explicit, feedback-rich step during development. The immediate benefits are: lower API costs during development, faster agent loop debugging, and improved model self-healing capability.

That said, its scope should be viewed realistically. The mock gateway primarily addresses schema drift in development and staging environments. Since it returns simulated data rather than real business responses, it cannot cover the semantic correctness or business logic validation of real APIs. It's a complement to the debugging pipeline, not a replacement for production environments.

What Other Solutions Does the Community Use?

The original post also opened up a question to the community: what other approaches are people using to handle tool schema drift? The author outlined several common approaches:

  • Custom Pydantic validators: Using Pydantic models to enforce strong type constraints on tool parameters is a mainstream approach in the Python ecosystem;
  • Time-travel debuggers: Allow replaying an agent's execution trajectory to reproduce and locate errors;
  • Native LangGraph fallback nodes: Leveraging LangGraph's graph structure to route to degraded or retry logic when validation fails.

Each of these approaches has a different focus: Pydantic emphasizes static constraints at the type level, LangGraph fallback emphasizes fault-tolerant orchestration at the process level, and the mock gateway sits between the two — focused on the closed loop of "validation + structured feedback." In real projects, these approaches are not mutually exclusive and can absolutely be combined.

Summary

Tool schema hallucination is an unavoidable hurdle on the path to reliable agent systems. This Reddit developer's solution offers a clear approach: rather than trial-and-erroring against real endpoints, add a mock gateway in front of tools that can validate and provide feedback — letting the model learn to self-correct in a controlled environment. For teams building production-grade agents, regardless of whether they adopt MockAgent as a specific tool, the design pattern of "front-loaded validation + structured error feedback" is worth incorporating into your engineering practice.

#LangChain agent#tool schema hallucination#mock gateway#JSON Schema validation#AJV#agent loop debugging#LangGraph#Pydantic validator#schema drift#structured error feedback
Share:

Related articles