fix: suppress PydanticJsonSchemaWarning for @tool parameters using Field(default_factory=...)#2065
Open
Zelys-DFKH wants to merge 1 commit intostrands-agents:mainfrom
Open
Conversation
…y=...) When a @tool parameter uses Field(default_factory=list) as its default, _extract_annotated_metadata passed the FieldInfo object itself as the `default=` argument to a new Field(), which triggered a PydanticJsonSchemaWarning because FieldInfo is not JSON serializable. Now when param_default is a FieldInfo, the default_factory or scalar default is unwrapped and forwarded correctly. The description from the Field() call is also surfaced in the tool schema. Fixes strands-agents#1914
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
When a
@toolparameter usesField(default_factory=list, description="items")as its default,_extract_annotated_metadataindecorator.pypasses theFieldInfoobject itself as thedefault=argument when constructing a newField(). SinceFieldInfois not JSON serializable,Pydantic emits a
PydanticJsonSchemaWarningduring schema generation:The fix checks whether
param_defaultis aFieldInfoinstance before building the outputfield. When it is,
default_factoryor the scalardefaultis unwrapped from theFieldInfoand forwarded to the new
Field()call. Thedescriptionattribute on theFieldInfois alsonow included in the tool schema when no docstring description is available.
Related Issues
Fixes #1914
Documentation PR
N/A
Type of Change
Bug fix
Testing
Added three tests to
tests/strands/tools/test_decorator.py:test_tool_field_default_factory_no_warning— verifies noPydanticJsonSchemaWarningis emitted forField(default_factory=list, ...)test_tool_field_default_factory_description_used— verifies theField(description=...)value appears in the tool schema and the parameter is not requiredtest_tool_field_default_value_no_warning— verifies no warning forField(default=5, ...)I ran
hatch run prepareChecklist
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.