Repository navigation
Description
Describe the bug
A function parameter whose name starts with an underscore (e.g. _tenant) is missing from the schema that create_tool_from_function (and the @tool decorator) generates. No error, no warning. If the parameter is required, the model can never supply it, so every call the model makes from the schema fails with missing 1 required positional argument.
The cause is in haystack/tools/from_function.py: the fields are passed to pydantic.create_model(function.__name__, **fields). Pydantic treats names with a leading underscore as private attributes, not fields, so they quietly drop out of model_json_schema(). (When you pass a FieldInfo, Pydantic raises "Fields must not use names with leading underscores", but with the (annotation, ...) tuple used here it just skips the name.)
Error message
schema: {'properties': {'order_id': {'type': 'string'}}, 'required': ['order_id'], 'type': 'object'}
ToolInvocationError: Failed to invoke Tool `lookup_order` with parameters {'order_id': 'A-1'}.
Error: lookup_order() missing 1 required positional argument: '_tenant'
Expected behavior
Either _tenant appears in the schema (and is listed in required), or tool creation fails loudly with a clear ValueError/SchemaGenerationError naming the parameter. A tool shouldn't be created with a schema that can't produce a valid call.
Additional context
Leading-underscore names show up a lot in real code ("internal" args, _session, _ctx, wrappers generated by other libraries). Parameters with a default are dropped too, silently, so the model can't set them.
Proposed fix: in create_tool_from_function, give underscore-prefixed parameters a safe internal field name with Field(alias=param_name). model_json_schema() uses aliases by default, so the schema exposes the real name _tenant, and invocation keeps passing _tenant=... to the function. If maintainers would rather not expose such names to the LLM, the alternative is to raise a clear error at tool-creation time. I'd also check ComponentTool, which builds its schema the same way. Tests: required and defaulted underscore params both end up in the schema with the right required list, and tool.invoke(**args) succeeds.
To Reproduce
from haystack.tools import create_tool_from_function
def lookup_order(_tenant: str, order_id: str) -> str:
"""Look up an order for a tenant."""
return f"{_tenant}/{order_id}"
tool = create_tool_from_function(lookup_order)
print(tool.parameters) # '_tenant' is missing, only 'order_id' is required
tool.invoke(order_id="A-1") # ToolInvocationError: missing '_tenant'FAQ Check
- Have you had a look at our new FAQ page?
System:
- OS: Linux
- Haystack version: 3.5.0-rc0 (main @ 3f6eb75, 2026-10-28), pydantic 2.x, Python 3.12
I'd like to work on a fix for this and will open a PR.
Activity
sjrl commentedon Nov 5, 2026
Thanks for the detailed report and the repro!
We'd rather not expose underscore-prefixed parameters to the LLM. A leading underscore usually means a value the caller supplies, not the model. For that case we already have
inputs_from_state, which keeps the parameter out of the schema and injects it at runtime:We agree the silent failure is a problem, though. Could we go with your alternative and raise a clear
ValueErrorwhen the tool is created? It should only fire for required underscore parameters that aren't already covered byinputs_from_state. Defaulted ones can keep being skipped, since the function still works with its default. Something like this in the parameter loop ofcreate_tool_from_function, after the existinginputs_from_state/State/Callable skips:Please also add a short note to the
create_tool_from_functionand@tooldocstrings saying underscore-prefixed parameters aren't exposed to the LLM, plus a test covering:_paramraises_paramininputs_from_stateworks_paramis left out of the schemaHappy to review a PR with that scope!