groundy
Agents & Frameworks

Pydantic vs LangChain: Where Should Tool Arguments Be Validated?

Define tool arguments in Pydantic and expose schemas via LangChain. Assign validation to Pydantic and error handling to orchestration. Verify runtime behavior before claiming.

Published 6 references
A skeptical green resin dinosaur holds a yellow peg before a differently shaped opening in an ivory resin collar. A separate curved chute holds another yellow peg, with hard shadows on a warm ivory background.
On this page11 sections

Define tool arguments once as Pydantic models, choose an explicit strict or lax coercion policy, and expose that schema through LangChain’s tool interface. Handle malformed calls separately at the orchestration layer, where a validation error can be returned to the model for another attempt. This is a boundary decision, not a choice between rival frameworks. The practical risk is assuming that a LangChain-compatible tool interface proves your application’s validation rules ran, or that it determines how validation failures reach the model.

That separation matters because the documented responsibilities are different. Pydantic is a Python data-validation library with coercion controls and JSON Schema generation. LangChain supplies agent architecture and tool interfaces. Pydantic AI, despite its name, is a third thing: a separate Python agent SDK from the Pydantic team, with its own tool-argument validation and retry loop.

The comparison hides a dependency

Pydantic’s own documentation lists LangChain among the packages that use Pydantic. It reports roughly 8,000 PyPI packages in that ecosystem, alongside FastAPI, huggingface, Django Ninja and SQLModel. The count is a vendor-reported figure, but LangChain’s inclusion is enough to challenge the premise of a head-to-head choice: one project can consume the other rather than replace it. The Pydantic team makes the stronger claim that Pydantic Validation is the validation layer for LangChain and several other AI SDKs; that is also a vendor statement about its ecosystem, not independent compatibility testing.

The useful question is therefore not “Which framework should validate my arguments?” It is “Which component owns each part of the argument contract, and what happens when a call violates it?”

Three names correspond to three documented roles:

  • Pydantic owns data validation, including the choice between strict and lax treatment of supplied data, and can emit JSON Schema for integration with other tools.
  • LangChain owns an agent and integration framework. Its tool documentation describes an input or args schema supplied to a chat model’s tool-calling feature, followed by an action whose response can be returned as a ToolMessage.
  • Pydantic AI owns an agent loop. Its overview says Pydantic validates tool arguments and sends validation errors back to the LLM so it can retry.

Community usage reflects the same coexistence. One full-stack chatbot repository lists FastAPI, LangChain and Pydantic in a single stack, assigning Pydantic to data validation and structured output. That is an existence example, not evidence of reliability or best practice, but it illustrates why treating the libraries as substitutes leads to the wrong design exercise.

Assign each decision to a layer

DecisionDocumented homeRecommended responsibilityWhat the interface alone does not establish
Argument rules and coercionPydanticKeep the tool-argument contract in a Pydantic model and explicitly choose strict or lax mode.A model or schema existing somewhere does not prove the intended rules were applied to a particular call.
Model-facing schemaPydantic JSON Schema, exposed through the tool interfaceDerive the schema the chat model sees from the same argument contract rather than maintaining an unrelated description.A schema supplied for tool calling does not guarantee that generated arguments satisfy it.
Tool interface and executionLangChain BaseToolImplement the args-schema interface and the action that returns a response to the chat model.Interface conformance does not document LangChain’s runtime behavior for malformed arguments.
Validation-error propagationAgent orchestrationReturn an error the model can act on when retry is the desired behavior. Pydantic AI documents this pattern directly.LangChain’s cited pages do not document that it automatically provides equivalent propagation or retries.
Independent contract boundaryA separate application or contract layerConsider this when validation policy should remain independent of the agent framework.A community implementation does not establish measured reliability or a universal default.

This map also prevents a common duplication trap. If one schema is written for the model, another is written for the tool interface, and application checks live elsewhere, the three can drift. Keeping the Pydantic model as the source of the argument contract reduces that duplication. It does not remove the need to verify how the surrounding framework invokes it.

Strict or lax is part of the tool contract

Pydantic’s documentation defines the choice plainly: strict mode does not convert data, while lax mode tries to coerce data to the correct type where appropriate. For tool calls, that choice belongs with the argument rules rather than being left as an incidental framework setting.

The tradeoff follows from those semantics. Strict mode avoids accepting a call through type conversion, making a mismatch a validation failure. Lax mode can accept arguments whose representation differs but whose values Pydantic can coerce. That tolerance may be useful when conversion is an intended part of the contract; it is less attractive when accepting the conversion would conceal a malformed call.

I would not treat strict mode as a universal default merely because it sounds safer. I would use it where the supplied representation must match the contract, and use lax mode only where coercion is an accepted behavior of the tool. The important requirement is that the choice be deliberate and attached to the model that defines the arguments.

Neither mode answers the later question of error handling. A rejected argument still needs a path through the agent loop: exception, returned validation message, refusal, or some other application-defined outcome. Validation policy and propagation policy are separate decisions.

JSON Schema describes the call; it does not finish validation

Pydantic models can emit JSON Schema, which its documentation presents as a way to integrate with other tools. LangChain’s tool documentation supplies the receiving side of that boundary: a tool defines an input or args schema that is passed with a text request to the chat model’s tool-calling feature, allowing the model to generate a tool call and its parameters.

That creates a useful sequence:

  1. The Pydantic model defines the expected arguments and validation policy.
  2. JSON Schema generation provides a model-facing representation of that contract.
  3. LangChain’s tool interface exposes the args schema to the chat model.
  4. A resulting tool call still needs validation before the tool’s application logic relies on it.
  5. The orchestration layer determines what the model receives if that validation fails.

The first three steps describe an interface path, not a guarantee that every generated call will conform. Nor does inheriting from a framework base class prove that application-specific constraints were written or executed. That distinction is the main reason to keep responsibility explicit.

There is also a scope limitation. The LangChain integration guide is contributor-facing documentation for Python integration packages. It establishes the documented tool-interface role, including BaseTool and ToolMessage, but it does not document exception propagation or the runtime handling of malformed arguments, and it is not a compatibility statement for a particular combination of LangChain and Pydantic releases.

Malformed calls need an orchestration decision

Pydantic AI provides the clearest documented failure pattern. Its overview states that Pydantic validates tool arguments and that errors are passed back to the LLM so it can retry. When validation fails, the agent is sent back for another attempt. This places the validation rule in Pydantic and the recovery behavior in Pydantic AI’s agent loop.

That is stronger evidence than interface existence, but it applies to Pydantic AI. It should not be silently transferred to LangChain. LangChain’s cited pages do not document what its runtime does with malformed tool arguments, which exception may surface, or whether a validation error becomes model-visible content.

A community project shows why this remains a genuine design choice. agent-airlock describes itself as an in-process, Pydantic-only contract layer for agent tool calls, with strict argument validation, ghost-argument stripping and self-healing retries. For a blocked call, it returns a dictionary through AirlockResponse.to_dict() rather than raising, with the stated aim of giving the model a refusal it can act on instead of a traceback.

That is a useful counter-example to framework-led design: the author chose a separate contract boundary rather than treating an agent interface as the whole validation solution. It is not proof that a separate layer is always preferable. The project’s capabilities are self-described, and there is no independent comparison against the frameworks’ own validation. Its return-instead-of-raise pattern is best read as one concrete way to make malformed calls recoverable.

For a LangChain-based application, the defensible recommendation is therefore conditional: design an orchestration path that returns actionable validation failures when you want model-driven retry, but verify the actual runtime wiring before claiming LangChain supplies it. If the application instead raises, that behavior should be an intentional choice with a known caller, not an accidental consequence of an undocumented interface path.

A practical boundary checklist

For a new tool, or one whose failures are difficult to diagnose, assign responsibility in this order:

  1. Write the argument contract in Pydantic. Include the application’s validation rules there, and record whether strict or lax coercion is intended.
  2. Generate the integration schema from that contract. Use Pydantic’s JSON Schema emission rather than treating a separately maintained model-facing description as authoritative.
  3. Expose the schema through the framework tool interface. In LangChain, the documented integration point is the tool’s args schema, with the tool action returning a response for the chat model.
  4. Specify malformed-call behavior independently. Decide whether a failed call returns something the model can act on or raises to the caller. Pydantic AI documents errors passed back to the LLM for retry; agent-airlock returns a structured refusal, with fix hints the model can retry against, rather than raising.
  5. Test the boundary in the exact stack being deployed. A bounded check should send a deliberately malformed call and record whether validation runs before the tool action, what exception or response is produced, and whether anything actionable reaches the model. This is a recommended verification step, not behavior asserted by the cited LangChain guide.

The last step is necessary because documentation of an interface is not documentation of every runtime path. It also gives a team evidence about its own pinned versions without pretending that a result from one release establishes universal framework behavior.

What remains unverified

The cited documentation establishes roles and one explicit retry pattern, not release-level compatibility. It does not verify current method signatures, exception classes, supported Python versions, or runtime compatibility between specific LangChain and Pydantic versions. Even the integration guide’s statement that BaseTool has three properties and two methods is a version-sensitive detail from a contributor guide, not a reason to skip checking the version you install.

Those limits change the decision in one important way: they justify a small deployment test, but they do not justify avoiding the layer separation. Keep validation rules and coercion in the Pydantic contract, use LangChain’s interface to expose that contract to tool calling, and assign malformed-call recovery explicitly to orchestration. If you choose Pydantic AI, its documentation supports validate-and-retry as the agent-loop behavior. If you stay with LangChain, do not claim equivalent behavior until your runtime check demonstrates it.

Frequently Asked Questions

What is the difference between strict and lax mode in Pydantic for tool arguments?

Pydantic’s documentation defines the choice plainly: strict mode does not convert data, while lax mode tries to coerce data to the correct type where appropriate. For tool calls, that choice belongs with the argument rules rather than being left as an incidental framework setting.

How does Pydantic AI handle validation errors for tool arguments?

Pydantic AI provides the clearest documented failure pattern. Its overview states that Pydantic validates tool arguments and that errors are passed back to the LLM so it can retry. When validation fails, the agent is sent back for another attempt. This places the validation rule in Pydantic and the recovery behavior in Pydantic AI’s agent loop.

What should a LangChain-based application do for malformed tool calls?

For a LangChain-based application, the defensible recommendation is therefore conditional: design an orchestration path that returns actionable validation failures when you want model-driven retry, but verify the actual runtime wiring before claiming LangChain supplies it. If the application instead raises, that behavior should be an intentional choice with a known caller, not an accidental consequence of an undocumented interface path.

References

Follow the links in the article for context. The supporting material is collected here for further reading.

  1. Pydantic is a Python data-validation librarypydantic.devAccessed
  2. LangChain supplies agent architecture and tool interfaceslangchain.comAccessed
  3. Pydantic AI overviewpydantic.devAccessed
  4. Full-stack chatbot repositorygithub.comAccessed
  5. LangChain integration guidedocs.langchain.comAccessed
  6. agent-airlockgithub.comAccessed

Join the discussion

Share a useful perspective or ask a question about this article.

Discussion guidelinesComments privacy