Skip to content

fix: keep underscore-prefixed parameters in tool schemas - #13473

Closed
mengchen-atx wants to merge 1 commit into
deepset-ai:mainfrom
mengchen-atx:cursor/keep-underscore-tool-parameters-d140
Closed

mengchen-atx wants to merge 1 commit into
deepset-ai:mainfrom
mengchen-atx:cursor/keep-underscore-tool-parameters-d140

Conversation

@mengchen-atx

@mengchen-atx mengchen-atx commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor

Related Issues

Proposed Changes:

create_tool_from_function and @tool built a Pydantic model with (annotation, default) fields. Pydantic treats a name that starts with _ as a private attribute, so parameters such as _tenant never appeared in model_json_schema(). A required parameter was then impossible for the model to supply, and Tool.invoke failed with a missing argument. A defaulted parameter was dropped the same way.

ComponentTool hit the same Pydantic rule. It passed Field(...) under the real input name, so a component input such as _tenant raised SchemaGenerationError instead of being omitted. Dataclass fields used by parameter schemas did the same, except ChatMessage, which already strips the leading underscore so the schema matches ChatMessage.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, required stays correct, and defaults and descriptions are unchanged. Descriptions from Annotated metadata are still applied after schema generation. tool.invoke(**args) passes _tenant=... through to the function. ChatMessage schema keys are unchanged.

How did you test it?

Reproduced with the script from the issue, on main, before the fix:

schema: {'properties': {'order_id': {'type': 'string'}}, 'required': ['order_id'], 'type': 'object'}
invoke FAILED: ToolInvocationError ... missing 1 required positional argument: '_tenant'

Added tests, then ran them against that same code. All six failed:

  • test_from_function_keeps_underscore_prefixed_parameters — schema had only order_id
  • test_tool_decorator_keeps_underscore_prefixed_parameters — same drop
  • test_from_function_underscore_parameter_does_not_replace_unprefixed_name — _tenant missing beside tenant
  • test_component_tool_keeps_underscore_prefixed_inputs — SchemaGenerationError / Fields must not use names with leading underscores
  • test_component_tool_keeps_underscore_dataclass_fields — same Pydantic error while converting the dataclass
  • test_resolve_type_keeps_underscore_dataclass_fields — same error

After the fix, those tests pass. They check that a required _tenant and a defaulted _note are in the schema, required lists only the required names, Annotated and docstring descriptions are kept, jsonschema rejects a payload that omits _tenant, invoke(_tenant=..., order_id=...) returns the function result, and to_dict / from_dict round-trips the schema.

The issue script now prints a schema with both _tenant and order_id required. Its invoke(order_id="A-1") call still fails, because that call leaves out the required _tenant. Calling with _tenant succeeds, 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 skipped
  • hatch run fmt / hatch run fmt-check — ruff clean
  • hatch run test:types — mypy clean (566 files)
  • reno lint

Notes for the reviewer

The internal Pydantic name is not part of the schema. If the stripped name would collide with another parameter (tenant and _tenant) or is not a usable identifier (_ or _class), the helper picks another internal name and still aliases the real one.

ChatMessage still 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

  • I have read the contributors guidelines and the code of conduct.
  • I have updated the related issue with new insights and changes.
  • I have added unit tests and updated the docstrings.
  • I've used one of the conventional commit types for my PR title: fix:, feat:, build:, chore:, ci:, docs:, style:, refactor:, perf:, test: and added ! in case the PR includes breaking changes.
  • I have documented my code.
  • I have added a release note file, following the contributors guidelines.
  • I have run pre-commit hooks and fixed any issue.

This PR was generated with an AI assistant. I have reviewed the changes and run the relevant tests.

@mengchen-atx
mengchen-atx requested a review from a team as a code owner October 29, 2026 00:02
@mengchen-atx
mengchen-atx requested review from davidsbatista and removed request for a team October 29, 2026 00:02
@vercel

vercel Bot commented Oct 29, 2026

Copy link
Copy Markdown
Contributor

@cursoragent is attempting to deploy a commit to the deepset Team on Vercel.

A member of the Team first needs to authorize it.

@github-actions

Copy link
Copy Markdown
Contributor

Hi @mengchen-atx, thanks for your interest in contributing to Haystack! 🙏

⚠️ Issue #13472 is already being addressed by open pull request(s) #1. Before opening a PR for an issue, please check whether a PR is already linked to it, and consider contributing to the existing PR instead. We may close duplicate PRs to keep the review queue manageable.

This is an automated message to help us keep the review queue healthy.

@CLAassistant

CLAassistant commented Oct 29, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@HaystackBot

Copy link
Copy Markdown
Contributor

Hi @mengchen-atx, thanks a lot for your contribution! 🙏

We noticed that the Contributor License Agreement (CLA) check (license/cla) hasn't passed yet, so we've temporarily moved this PR to draft and paused the review assignment.

To get your PR reviewed, please sign the CLA via the link in the license/cla check below (or in the CLA bot comment). As soon as the check turns green, this PR will automatically be marked ready for review again and a reviewer will be re-assigned.

@HaystackBot
HaystackBot removed the request for review from davidsbatista October 29, 2026 01:23
@HaystackBot HaystackBot added the cla-pending PR is in draft until the contributor signs the CLA label Oct 29, 2026
@HaystackBot
HaystackBot marked this pull request as draft October 29, 2026 01:23
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.
@cursor
cursor Bot force-pushed the cursor/keep-underscore-tool-parameters-d140 branch from a79cb2c to af3e73d Compare October 29, 2026 02:11
@HaystackBot
HaystackBot marked this pull request as ready for review October 29, 2026 02:12
@HaystackBot HaystackBot removed the cla-pending PR is in draft until the contributor signs the CLA label Oct 29, 2026
@HaystackBot

Copy link
Copy Markdown
Contributor

Thanks for signing the CLA, @mengchen-atx! 🎉 This PR is now ready for review again and the reviewer has been re-assigned.

@mengchen-atx

Copy link
Copy Markdown
Contributor Author

Hi @davidsbatista, friendly ping on this one when you get a chance. It still merges cleanly with main after #13211, and test/tools passes locally on the merged result. The workflows are waiting for approval to run; the Vercel check is just the docs preview needing team authorization. Thanks!

@mengchen-atx

Copy link
Copy Markdown
Contributor Author

Reopened from a renamed branch as #13567, same commits.

@mengchen-atx
mengchen-atx deleted the cursor/keep-underscore-tool-parameters-d140 branch November 5, 2026 08:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

create_tool_from_function / @tool silently drops parameters whose name starts with _, making the tool uncallable

3 participants