haystack icon indicating copy to clipboard operation
haystack copied to clipboard

Annotated callback parameters cause tool schema generation to fail

Open SuhrudhC opened this issue 1 month ago • 1 comments

Describe the bug

Tool creation fails when a callback parameter is wrapped in typing.Annotated. Both create_tool_from_function and ComponentTool already omit ordinary callable parameters from LLM-visible schemas, but the callable detector does not look through Annotated.

This makes adding annotation metadata to an otherwise working callback change tool construction into a SchemaGenerationError.

To reproduce

from collections.abc import Callable
from typing import Annotated
from haystack.tools import create_tool_from_function

def format_query(
    query: str,
    callback: Annotated[Callable[[str], str] | None, "Format the output"] = str.upper,
) -> str:
    return callback(query) if callback else query

tool = create_tool_from_function(format_query)
print(tool.parameters)
print(tool.invoke(query="hello"))

Actual behavior

SchemaGenerationError: Failed to create JSON schema for function 'format_query', caused by Pydantic's Cannot generate a JsonSchema for core_schema.CallableSchema.

Expected behavior

Only query appears in the schema, as with an unannotated callback. Invoking the tool with query="hello" still uses the default callback and returns "HELLO".

Scope and verification

The same failure occurs when wrapping a component whose input callback has this annotation. I prepared a small change to unwrap Annotated in the existing callable detector, with six regression cases covering both public creation paths and optional callbacks on either side of the annotation wrapper. All six fail on the original source and pass with the change. The three affected test modules pass (83 passed, 7 live-API tests skipped); targeted mypy and the repository pre-commit checks pass.

Verified locally on upstream ff134376, Python 3.12, macOS arm64. I searched existing issues and PRs; the open Ellipsis-callable compatibility and Pydantic Field metadata fixes concern different paths.

SuhrudhC avatar Oct 06 '26 14:10 SuhrudhC

The tested patch is open in #13138. The six regressions cover both create_tool_from_function and ComponentTool, including optional callbacks inside/outside Annotated; they fail on the original implementation and pass with the change. The fix is confined to the shared callable detector, with a release note and passing local type/pre-commit checks.

SuhrudhC avatar Oct 06 '26 15:10 SuhrudhC