Repository navigation
fix: keep underscore-prefixed parameters in tool schemas - #13473
mengchen-atx wants to merge 1 commit into
Conversation
|
@cursoragent is attempting to deploy a commit to the deepset Team on Vercel. A member of the Team first needs to authorize it. |
|
Hi @mengchen-atx, thanks for your interest in contributing to Haystack! 🙏 This is an automated message to help us keep the review queue healthy. |
|
Hi @mengchen-atx, thanks a lot for your contribution! 🙏 We noticed that the Contributor License Agreement (CLA) check ( To get your PR reviewed, please sign the CLA via the link in the |
Pydantic treats leading-underscore names as private attributes, so create_tool_from_function dropped them and ComponentTool failed while building the schema. Store those parameters under a safe field name with Field(alias=...) so the JSON schema keeps the real name.
a79cb2c to
af3e73d
Compare
|
Thanks for signing the CLA, @mengchen-atx! 🎉 This PR is now ready for review again and the reviewer has been re-assigned. |
|
Hi @davidsbatista, friendly ping on this one when you get a chance. It still merges cleanly with main after #13211, and |
|
Reopened from a renamed branch as #13567, same commits. |
Related Issues
create_tool_from_function/@toolsilently drops parameters whose name starts with_, making the tool uncallable #13472 (create_tool_from_function/@toolsilently drops parameters whose name starts with_, making the tool uncallable #13472)Proposed Changes:
create_tool_from_functionand@toolbuilt a Pydantic model with(annotation, default)fields. Pydantic treats a name that starts with_as a private attribute, so parameters such as_tenantnever appeared inmodel_json_schema(). A required parameter was then impossible for the model to supply, andTool.invokefailed with a missing argument. A defaulted parameter was dropped the same way.ComponentToolhit the same Pydantic rule. It passedField(...)under the real input name, so a component input such as_tenantraisedSchemaGenerationErrorinstead of being omitted. Dataclass fields used by parameter schemas did the same, exceptChatMessage, which already strips the leading underscore so the schema matchesChatMessage.from_dict.Underscore-prefixed names now get a safe internal field name and
Field(alias=param_name).model_json_schema()uses the alias, so the schema exposes_tenant,requiredstays correct, and defaults and descriptions are unchanged. Descriptions fromAnnotatedmetadata are still applied after schema generation.tool.invoke(**args)passes_tenant=...through to the function.ChatMessageschema keys are unchanged.How did you test it?
Reproduced with the script from the issue, on main, before the fix:
Added tests, then ran them against that same code. All six failed:
test_from_function_keeps_underscore_prefixed_parameters— schema had onlyorder_idtest_tool_decorator_keeps_underscore_prefixed_parameters— same droptest_from_function_underscore_parameter_does_not_replace_unprefixed_name—_tenantmissing besidetenanttest_component_tool_keeps_underscore_prefixed_inputs—SchemaGenerationError/Fields must not use names with leading underscorestest_component_tool_keeps_underscore_dataclass_fields— same Pydantic error while converting the dataclasstest_resolve_type_keeps_underscore_dataclass_fields— same errorAfter the fix, those tests pass. They check that a required
_tenantand a defaulted_noteare in the schema,requiredlists only the required names,Annotatedand docstring descriptions are kept,jsonschemarejects a payload that omits_tenant,invoke(_tenant=..., order_id=...)returns the function result, andto_dict/from_dictround-trips the schema.The issue script now prints a schema with both
_tenantandorder_idrequired. Itsinvoke(order_id="A-1")call still fails, because that call leaves out the required_tenant. Calling with_tenantsucceeds, which the tests cover.Also ran:
hatch -e test run pytest -m "not integration" test/tools/test_from_function.py test/tools/test_component_tool.py test/tools/test_parameters_schema_utils.py test/tools/test_tool.py— 140 passed, 2 skippedhatch run fmt/hatch run fmt-check— ruff cleanhatch run test:types— mypy clean (566 files)reno lintNotes for the reviewer
The internal Pydantic name is not part of the schema. If the stripped name would collide with another parameter (
tenantand_tenant) or is not a usable identifier (_or_class), the helper picks another internal name and still aliases the real one.ChatMessagestill drops the underscore on_role,_content,_name, and_meta. That path is separate from this alias fix and the existing schema tests still match.Checklist
fix:,feat:,build:,chore:,ci:,docs:,style:,refactor:,perf:,test:and added!in case the PR includes breaking changes.This PR was generated with an AI assistant. I have reviewed the changes and run the relevant tests.