Skip to content

Conversation

@thromel
Copy link

@thromel thromel commented Dec 8, 2025

Summary

  • Fix StructuredTool with strict args_schema (extra="forbid") rejecting injected arguments

Problem

When using StructuredTool with a Pydantic args_schema that has model_config = {"extra": "forbid"}, injected arguments (annotated with InjectedToolArg) cause a validation error:

pydantic_core._pydantic_core.ValidationError: 1 validation error for ArgsSchema
runtime
  Extra inputs are not permitted

This happens because injected args are passed to model_validate() before being stripped, and Pydantic's strict mode rejects them as unknown fields.

Solution

Extract injected args that are NOT declared in the schema before validation, then re-add them after validation completes. This:

  • Allows injected args to work with strict Pydantic schemas
  • Maintains backward compatibility for schemas where injected args ARE declared as fields
  • Follows the documented behavior that injected args should be "hidden from the model" and "injected during execution"

Test Plan

  • Added regression test test_tool_injected_arg_with_strict_schema
  • All existing tool tests pass (149 passed, 1 skipped)
  • Ruff lint passes

Fixes #34246

@thromel thromel requested a review from eyurtsev as a code owner December 8, 2025 09:14
@github-actions github-actions bot added core Related to the package `langchain-core` fix labels Dec 8, 2025
@codspeed-hq
Copy link

codspeed-hq bot commented Dec 8, 2025

CodSpeed Performance Report

Merging #34248 will not alter performance

Comparing thromel:fix/injected-args-strict-schema (d8f2351) with master (dff229d)1

⚠️ Unknown Walltime execution environment detected

Using the Walltime instrument on standard Hosted Runners will lead to inconsistent data.

For the most accurate results, we recommend using CodSpeed Macro Runners: bare-metal machines fine-tuned for performance measurement consistency.

Summary

✅ 13 untouched
⏩ 21 skipped2

Footnotes

  1. No successful run was found on master (2aa0555) during the generation of this report, so dff229d was used instead as the comparison base. There might be some changes unrelated to this pull request in this report.

  2. 21 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

@thromel thromel force-pushed the fix/injected-args-strict-schema branch 2 times, most recently from e51f4d7 to 401a21c Compare December 9, 2025 03:41
When using StructuredTool with an args_schema that has extra="forbid",
injected arguments (annotated with InjectedToolArg) caused Pydantic
validation errors because they were passed to model_validate() before
being stripped.

This fix extracts injected args that are NOT in the schema before
validation, then re-adds them after validation completes. This allows
injected args to work correctly with strict Pydantic schemas while
maintaining backward compatibility for schemas where injected args
are declared as fields.

Fixes langchain-ai#34246
@thromel thromel force-pushed the fix/injected-args-strict-schema branch from 401a21c to d8f2351 Compare December 9, 2025 06:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

core Related to the package `langchain-core` fix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Structured Tool with strict args_schema prevents injected arguments

1 participant