Skip to content

fix: handle 'args' parameter name conflict in StructuredTool schema generation#35815

Closed
Ethan T. (gambletan) wants to merge 1 commit intolangchain-ai:masterfrom
gambletan:fix/structured-tool-args-field
Closed

fix: handle 'args' parameter name conflict in StructuredTool schema generation#35815
Ethan T. (gambletan) wants to merge 1 commit intolangchain-ai:masterfrom
gambletan:fix/structured-tool-args-field

Conversation

@gambletan
Copy link
Contributor

Summary

  • Fixes StructuredTool.from_function injecting a spurious v__args field and dropping the legitimate args parameter when a function parameter is literally named args
  • Adds filtering for Pydantic's internal v__args/v__kwargs sentinel fields that leak into the schema when parameter names conflict with Pydantic internals
  • Preserves backward compatibility: functions with *args/**kwargs (VAR_POSITIONAL/VAR_KEYWORD) continue to work as before

Root cause

In create_schema_from_function, the check not has_args and field == "args" filters out any field named args when the function has no *args. But when a function has a regular parameter named args (e.g., def run_command(args: list[str])), Pydantic's ValidatedFunction keeps it as args in the model while renaming its own internal sentinel to v__args. The old code:

  1. Incorrectly filtered out the legitimate args field
  2. Let the spurious v__args sentinel pass through into the schema

Fix

  1. Before filtering args/kwargs fields, check whether the function actually has a regular parameter with that name
  2. Always filter out v__args and v__kwargs (Pydantic's renamed internal sentinels)

Test plan

  • Verify StructuredTool.from_function with a function having args: list[str] produces schema with args and without v__args
  • Verify functions with *args (VAR_POSITIONAL) still work correctly
  • Verify functions without any args parameter still work correctly
  • Verify same behavior for kwargs parameter name

Fixes #35796

🤖 Generated with Claude Code

…eneration

When a function has a parameter literally named `args`, Pydantic's
ValidatedFunction renames its internal sentinel field to `v__args`,
which leaks into the generated JSON schema. Meanwhile, the legitimate
`args` parameter gets incorrectly filtered out.

This fix:
- Preserves function parameters named `args`/`kwargs` in the schema
- Filters out Pydantic's internal `v__args`/`v__kwargs` sentinel fields

Fixes langchain-ai#35796

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

This PR has been automatically closed because you are not assigned to the linked issue.

External contributors must be assigned to an issue before opening a PR for it. Please:

  1. Comment on the linked issue to request assignment from a maintainer
  2. Once assigned, edit your PR description and the PR will be reopened automatically

@github-actions github-actions bot closed this Mar 13, 2026
@codspeed-hq
Copy link

codspeed-hq bot commented Mar 13, 2026

Merging this PR will improve performance by 34.39%

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

⚠️ Different runtime environments detected

Some benchmarks with significant performance changes were compared across different runtime environments,
which may affect the accuracy of the results.

Open the report in CodSpeed to investigate

⚡ 13 improved benchmarks
⏩ 23 skipped benchmarks1

Performance Changes

Mode Benchmark BASE HEAD Efficiency
WallTime test_import_time[tool] 578.6 ms 484.4 ms +19.46%
WallTime test_import_time[RunnableLambda] 534.4 ms 459.2 ms +16.39%
WallTime test_import_time[Runnable] 527.8 ms 455.1 ms +15.98%
WallTime test_import_time[ChatPromptTemplate] 704 ms 568 ms +23.93%
WallTime test_import_time[LangChainTracer] 486.7 ms 432.3 ms +12.59%
WallTime test_import_time[PydanticOutputParser] 567.6 ms 491.2 ms +15.57%
WallTime test_async_callbacks_in_sync 25.1 ms 18.7 ms +34.39%
WallTime test_import_time[InMemoryVectorStore] 659.5 ms 555.9 ms +18.63%
WallTime test_import_time[BaseChatModel] 567.4 ms 495.4 ms +14.55%
WallTime test_import_time[CallbackManager] 331.9 ms 296.6 ms +11.92%
WallTime test_import_time[Document] 196.1 ms 173.4 ms +13.12%
WallTime test_import_time[InMemoryRateLimiter] 186.5 ms 162.6 ms +14.72%
WallTime test_import_time[HumanMessage] 274.8 ms 242 ms +13.58%

Comparing gambletan:fix/structured-tool-args-field (9e1c215) with master (307cdca)

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.

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 missing-issue-link size: XS < 50 LOC

Projects

None yet

Development

Successfully merging this pull request may close these issues.

StructuredTool.from_function injects spurious v__args field into JSON schema when function parameter is named args

1 participant