Skip to content

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

Closed

Description

@mengchen-atx

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

  1. assigned and unassigned on Nov 5, 2026
  2. sjrl commented on Nov 5, 2026

    @sjrl
    Contributor

    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:

    tool = create_tool_from_function(lookup_order, inputs_from_state={"tenant": "_tenant"})

    We agree the silent failure is a problem, though. Could we go with your alternative and raise a clear ValueError when the tool is created? It should only fire for required underscore parameters that aren't already covered by inputs_from_state. Defaulted ones can keep being skipped, since the function still works with its default. Something like this in the parameter loop of create_tool_from_function, after the existing inputs_from_state/State/Callable skips:

    # Pydantic treats leading-underscore names as private attributes and leaves them out of the schema
    if param_name.startswith("_") and param.default is param.empty:
        raise ValueError(
            f"Function '{function.__name__}': required parameter '{param_name}' starts with an underscore and "
            "can't be part of the tool schema. Provide it through `inputs_from_state` or give it a default."
        )

    Please also add a short note to the create_tool_from_function and @tool docstrings saying underscore-prefixed parameters aren't exposed to the LLM, plus a test covering:

    • a required _param raises
    • the same function with _param in inputs_from_state works
    • a defaulted _param is left out of the schema

    Happy to review a PR with that scope!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions