haystack icon indicating copy to clipboard operation
haystack copied to clipboard

Annotated State parameters break tool creation

Open SuhrudhC opened this issue 1 month ago • 0 comments

Describe the bug

create_tool_from_function() and ComponentTool fail to build a tool when an injected agent State parameter is wrapped in typing.Annotated. Plain State and State | None parameters are intentionally omitted from the model's JSON schema, but the same type with annotation metadata is sent to Pydantic, which cannot generate a State schema.

Error message

SchemaGenerationError: Failed to create JSON schema for function 'inspect_state', caused by Pydantic being unable to generate a schema for State. ComponentTool raises the analogous component-run schema error.

Expected behavior

Exclude an Annotated[State, ...] parameter from the generated schema and inject the live State during agent tool execution, consistently with unwrapped State. Optional annotation wrappers should work too. State-dependent calls must retain their existing dependency ordering.

To Reproduce

from typing import Annotated
from haystack.components.agents import State
from haystack.tools import create_tool_from_function

def inspect_state(query: str, state: Annotated[State, "Live agent state"]) -> str:
    return query

print(create_tool_from_function(function=inspect_state).parameters)

This fails during public tool creation without model credentials or network requests. Replacing the annotation with plain State succeeds and yields a schema containing only query.

Additional context

A prepared fix branch unwraps annotation and optional wrappers in the existing runtime-type helper and includes a release note. Sixteen regression cases fail on current main and pass with the fix. The affected schema, function-tool, component-tool, and tool-calling modules pass 158 tests, with seven live-API tests skipped. Targeted mypy and applicable repository pre-commit checks pass. The full suite and live integrations were not run; tests used a focused Hatch environment on Python 3.12.

This concerns State recognition and injection, separately from callback schema filtering in #13137/#13138. I already have #13138 open, so I am keeping the fix on a branch until that PR is accepted or closed, following the one-open-community-PR rule. Prepared and validated with Codex.

FAQ Check

  • [ ] Have you had a look at the FAQ page?

System:

  • OS: macOS arm64
  • Python: 3.12
  • Haystack version: current main at 8d8f3e50

SuhrudhC avatar Oct 07 '26 14:10 SuhrudhC