Skip to content

fix(tools): unwrap FieldInfo default to avoid pydantic warning#2955

Open
chaynabors wants to merge 1 commit into
strands-agents:mainfrom
chaynabors:agent-tasks/1914-tool-fieldinfo-default
Open

fix(tools): unwrap FieldInfo default to avoid pydantic warning#2955
chaynabors wants to merge 1 commit into
strands-agents:mainfrom
chaynabors:agent-tasks/1914-tool-fieldinfo-default

Conversation

@chaynabors

Copy link
Copy Markdown
Member

The @tool decorator emitted a PydanticJsonSchemaWarning whenever a parameter used Field(default_factory=...) because _extract_annotated_metadata rebuilt the field with Field(default=param_default, ...), nesting the original FieldInfo as the default value. The fix detects when param_default is already a FieldInfo and rebuilds from its default, default_factory, and validation kwargs so constraints like ge, le, and pattern are preserved.

Fixes #1914.

@github-actions github-actions Bot added size/xs python Pull requests that update python code area-tool Tool behavior/api area-devx Developer experience improvements bug Something isn't working labels Jun 24, 2026
@codecov

codecov Bot commented Jun 24, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@awsarron awsarron 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.

please add test coverage

@chaynabors chaynabors force-pushed the agent-tasks/1914-tool-fieldinfo-default branch 2 times, most recently from 615a22f to c72df41 Compare June 25, 2026 17:44
@github-actions github-actions Bot added size/s and removed size/xs labels Jun 25, 2026
@chaynabors chaynabors force-pushed the agent-tasks/1914-tool-fieldinfo-default branch from c72df41 to 2ba90ca Compare June 25, 2026 17:52
@mkmeral

mkmeral commented Jun 25, 2026

Copy link
Copy Markdown
Contributor

/strands review

Comment thread strands-py/src/strands/tools/decorator.py
Comment thread strands-py/tests/strands/tools/test_decorator.py Outdated
@github-actions

Copy link
Copy Markdown
Contributor

Assessment: Comment (minor changes suggested)

The fix is correct and well-targeted. I verified that main nests the FieldInfo as a default (producing the PydanticJsonSchemaWarning), while this branch reuses/merges it cleanly — ge/le/pattern constraints survive in both the generated JSON schema and at runtime validation, and the docstring Args description is still honored. The regression test correctly fails on main and passes here. Small, focused change that does one thing.

Review themes
  • Test coverage: The new test exercises only the no-description merge branch; the description-already-present branch is uncovered (Codecov's missing lines). The test also asserts a single field rather than the full schema shape, and never asserts on items — the very default_factory parameter from [BUG] @tool emits PydanticJsonSchemaWarning for Field(default_factory=list) #1914.
  • API consistency: param = Field(ge=0) now works, but Annotated[T, Field(ge=0)] still raises NotImplementedError with a note claiming constraints can't be reliably preserved — a premise this PR contradicts. Worth a decision on unifying the two paths or updating the note.

Nice, minimal fix that cleanly resolves the warning without changing the happy path.

@chaynabors chaynabors force-pushed the agent-tasks/1914-tool-fieldinfo-default branch from 2ba90ca to 828ceef Compare June 25, 2026 18:27
@mkmeral

mkmeral commented Jun 25, 2026

Copy link
Copy Markdown
Contributor

/strands review

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

Labels

area-devx Developer experience improvements area-tool Tool behavior/api bug Something isn't working python Pull requests that update python code size/s strands-running

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] @tool emits PydanticJsonSchemaWarning for Field(default_factory=list)

3 participants