fix: handle 'args' parameter name conflict in StructuredTool schema generation#35815
fix: handle 'args' parameter name conflict in StructuredTool schema generation#35815Ethan T. (gambletan) wants to merge 1 commit intolangchain-ai:masterfrom
Conversation
…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>
|
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:
|
Merging this PR will improve performance by 34.39%
|
| 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)
Footnotes
-
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. ↩
Summary
StructuredTool.from_functioninjecting a spuriousv__argsfield and dropping the legitimateargsparameter when a function parameter is literally namedargsv__args/v__kwargssentinel fields that leak into the schema when parameter names conflict with Pydantic internals*args/**kwargs(VAR_POSITIONAL/VAR_KEYWORD) continue to work as beforeRoot cause
In
create_schema_from_function, the checknot has_args and field == "args"filters out any field namedargswhen the function has no*args. But when a function has a regular parameter namedargs(e.g.,def run_command(args: list[str])), Pydantic'sValidatedFunctionkeeps it asargsin the model while renaming its own internal sentinel tov__args. The old code:argsfieldv__argssentinel pass through into the schemaFix
args/kwargsfields, check whether the function actually has a regular parameter with that namev__argsandv__kwargs(Pydantic's renamed internal sentinels)Test plan
StructuredTool.from_functionwith a function havingargs: list[str]produces schema withargsand withoutv__args*args(VAR_POSITIONAL) still work correctlyargsparameter still work correctlykwargsparameter nameFixes #35796
🤖 Generated with Claude Code