fix: support Pydantic Field in Annotated tool parameters - #12982
MohammadHijjawi97 wants to merge 2 commits into
Conversation
create_tool_from_function and the @tool decorator wrote the first Annotated metadata item into the schema as the parameter description, even when it was not a string. A parameter annotated with a Pydantic Field, e.g. Annotated[int, Field(ge=1)], therefore put a FieldInfo object into the schema and Tool creation failed with "The provided parameters do not define a valid JSON schema". Use only a string metadata item as the description and leave other metadata to Pydantic, so Field constraints and descriptions end up in the tool schema.
|
@MohammadHijjawi97 is attempting to deploy a commit to the deepset Team on Vercel. A member of the Team first needs to authorize it. |
|
Hi @MohammadHijjawi97, thanks for your interest in contributing to Haystack! 🙏 This is an automated message to help us keep the review queue healthy. |
Coverage reportClick to see where and how coverage changed
This report was generated by python-coverage-comment-action |
||||||||||||||||||||||||
|
@MohammadHijjawi97 thanks for looking into this! Could I ask you if this is something you ran into while building tools with Haystack? |
|
Not in a production project, no. I came across it while going through how |
|
@sjrl following up on your question with a concrete case where this comes up: functions written for MCP servers. FastMCP documents parameters as |
Related Issues
Annotated+Fieldin tools, no match)Proposed Changes:
create_tool_from_function(and therefore@tool) took the firsttyping.Annotatedmetadata item and wrote it into the JSON schema as the parameterdescription, whatever its type. With the common Pydantic patternAnnotated[int, Field(ge=1)](orAnnotated[str, Field(description="...")]), aFieldInfoobject ended up in the schema and creating the tool failed:Now only a string metadata item is used as the description; other metadata is left to Pydantic, so
Fieldconstraints (minimum,minLength, ...) and aField(description=...)show up in the tool schema. The plainAnnotated[str, "description"]behavior is unchanged, and the docstring is updated accordingly.How did you test it?
test_from_function_annotated_with_pydantic_field(aFieldplus a string description, aFieldwith its own description, and aFieldwithout description). It fails onmainwith the error above and passes with the fix.pytest test/tools -m "not integration": 272 passed.ruff check/ruff format --checkandmypyon the changed files.Notes for the reviewer
When both a string and a
Field(description=...)are present, the string wins, consistent with the documentedAnnotated[T, "description"]convention.I used an AI coding assistant (Claude Code) while writing the fix and tests; I reviewed the changes and ran the checks listed above.
Checklist