Skip to content

fix: resolve postponed annotations when creating a tool from a function - #12739

Merged
julian-risch merged 2 commits into
deepset-ai:mainfrom
alanhuangyoo:fix/tool-from-function-postponed-annotations
Sep 22, 2026
Merged

julian-risch merged 2 commits into
deepset-ai:mainfrom
alanhuangyoo:fix/tool-from-function-postponed-annotations

Conversation

@alanhuangyoo

@alanhuangyoo alanhuangyoo commented Sep 13, 2026 •

Copy link
Copy Markdown
Contributor

Related Issues

Proposed Changes:

In a module with from __future__ import annotations, inspect.signature returns every annotation as a string. create_tool_from_function passes those strings straight to pydantic's create_model, which cannot resolve names like Annotated or Document, and its State check compares a string with the class. So @tool fails at import time for any parameter that isn't typed with a builtin:

f_str       OK
f_literal   SchemaGenerationError
f_optional  SchemaGenerationError
f_document  SchemaGenerationError
f_annotated SchemaGenerationError
f_state     SchemaGenerationError

Annotated[str, "..."] is the documented way to add parameter descriptions, so this hits the common case.

The fix resolves the annotations with _resolve_parameter_types (added in #12185) before the loop. That helper calls get_type_hints without include_extras, which would strip the Annotated descriptions, so it gets a keyword-only include_extras flag. Its default keeps the current behavior for the other callers.

How did you test it?

  • test_from_function_with_postponed_annotations: string annotations with Annotated, Literal and an optional State give the same schema as the eager version. It fails without the fix.
  • TestResolveParameterTypes::test_keeps_annotated_metadata_with_include_extras for the new flag.
  • With a real module using from __future__ import annotations, a @tool with a State parameter now builds, and an Agent with a scripted chat generator injects the state and the tool writes to it. On main, creating that tool raises.
  • pytest -m "not integration" test/tools test/core/test_type_utils.py test/core/component test/components/agents test/hooks: 2064 passed.
  • ruff check, ruff format --check, and mypy on the four changed Python files pass.

Checklist

This PR was fully generated with an AI assistant, including the fix, the tests and this description. The checks above are the ones that were actually run.

With `from __future__ import annotations`, inspect.signature returns
string annotations. create_tool_from_function passed them to pydantic,
which cannot resolve names like Annotated, and the State check never
matched. deepset-ai#12185 resolved them for the Agent and component paths but not
here. Resolve them with _resolve_parameter_types, which now takes
include_extras so Annotated descriptions survive.
@alanhuangyoo
alanhuangyoo requested a review from a team as a code owner September 13, 2026 22:05
@alanhuangyoo
alanhuangyoo requested review from julian-risch and removed request for a team September 13, 2026 22:05
@vercel

vercel Bot commented Sep 13, 2026

Copy link
Copy Markdown
Contributor

@alanhuangyoo 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 @alanhuangyoo, thanks for your interest in contributing to Haystack! 🙏

⚠️ You currently have 4 open pull requests in this repository (#12738, #12737, #12736 and this one). Our review capacity is limited, so please hold off opening more PRs until we've had a chance to review your first 2 open PRs. This helps us give each contribution the attention it deserves. Thank you!

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

@github-actions github-actions Bot added the type:documentation Improvements on the docs label Sep 21, 2026
@github-actions

github-actions Bot commented Sep 21, 2026 •

Copy link
Copy Markdown
Contributor

Coverage report

Click to see where and how coverage changed

FileStatementsMissingCoverageCoverage
(new stmts)
Lines missing
  haystack/core
  type_utils.py
  haystack/tools
  from_function.py
Project Total  

This report was generated by python-coverage-comment-action

@alanhuangyoo
alanhuangyoo force-pushed the fix/tool-from-function-postponed-annotations branch from 3b47803 to 6bc7dcf Compare September 21, 2026 17:22

@julian-risch julian-risch left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Thank you @alanhuangyoo !

@julian-risch
julian-risch merged commit 3c0dc45 into deepset-ai:main Sep 22, 2026
22 of 23 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants