Skip to content

fix(core): handle Pydantic-incompatible types in injected tool args#35636

Open
Alon Naor (AlonNaor22) wants to merge 1 commit intolangchain-ai:masterfrom
AlonNaor22:fix/filter-injected-args-from-schema
Open

fix(core): handle Pydantic-incompatible types in injected tool args#35636
Alon Naor (AlonNaor22) wants to merge 1 commit intolangchain-ai:masterfrom
AlonNaor22:fix/filter-injected-args-from-schema

Conversation

@AlonNaor22
Copy link

Summary

  • Fixes tool creation crash when a parameter is annotated with InjectedToolArg and typed as a Pydantic-incompatible type (e.g. a Protocol union like StateLike)
  • Catches PydanticSchemaError during validate_arguments and retries with injected arg annotations replaced by Annotated[Any, InjectedToolArg()], preserving the injected marker while using a Pydantic-safe type
  • Original annotations are restored after schema creation

Closes #32067

Files changed

Test plan

  • test_injected_arg_with_protocol_union_type — verifies tool with Annotated[StateLike, InjectedToolArg] can be created and invoked
  • test_injected_arg_filtered_from_schema_at_creation — verifies both InjectedToolArg and _DirectlyInjectedToolArg types work correctly
  • All 163 existing tests in test_tools.py pass (no regressions)

🤖 Generated with Claude Code

When a tool parameter is annotated with InjectedToolArg and typed as a
Protocol union (e.g. StateLike), Pydantic's validate_arguments crashes
with SchemaError because Protocol types are not valid for isinstance
checks. This catches PydanticSchemaError and retries with the injected
arg annotations replaced by Annotated[Any, InjectedToolArg()], which
preserves the injected marker while using a Pydantic-safe type.

Closes langchain-ai#32067

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@github-actions github-actions bot added core `langchain-core` package issues & PRs external fix For PRs that implement a fix labels Mar 7, 2026
@codspeed-hq
Copy link

codspeed-hq bot commented Mar 7, 2026

Merging this PR will not alter performance

⚠️ 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.

✅ 13 untouched benchmarks
⏩ 23 skipped benchmarks1


Comparing AlonNaor22:fix/filter-injected-args-from-schema (f51f100) with master (29134dc)

Open in CodSpeed

Footnotes

  1. 23 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.

@github-actions github-actions bot added the size: S 50-199 LOC label Mar 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

core `langchain-core` package issues & PRs external fix For PRs that implement a fix size: S 50-199 LOC

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Potential validation issue when using StateLike in Annotation for a tool

1 participant