fix: strip non-JSON-serializable defaults from @tool schema to prevent PydanticJsonSchemaWarning#1923
Open
frankgoldfish wants to merge 1 commit intostrands-agents:mainfrom
Conversation
3 tasks
…revent PydanticJsonSchemaWarning When a @tool parameter uses Field(default_factory=...) or Field(default=...), param.default is a FieldInfo object. Previously this was passed as: Field(default=<FieldInfo>, description=...) Since FieldInfo is not JSON-serializable, Pydantic emitted: PydanticJsonSchemaWarning: Default value ... is not JSON serializable Fix: in _extract_annotated_metadata(), detect when param_default is a FieldInfo and forward its default_factory or default to the new Field() correctly. Also inherit the FieldInfo's description when no higher-priority description exists. Adds regression test covering default_factory, plain default, required, and description forwarding cases. Fixes strands-agents#1914
d52f2e8 to
6999aaf
Compare
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.
Summary
Fixes #1914.
When a
@toolfunction parameter usesField(default_factory=...), Pydantic includes theFieldInfoobject as thedefaultvalue in the generated JSON schema. SinceFieldInfois not JSON-serializable, Pydantic emits:Reproduction
Root Cause
_clean_pydantic_schema()indecorator.pyremoves known Pydantic metadata keys but does not validate whetherdefaultvalues are JSON-serializable before they reach Pydantic's schema serializer.Fix
After processing each property in
_clean_pydantic_schema(), attemptjson.dumps()on anydefaultvalue. If it raisesTypeErrororValueError, remove thedefaultkey.The field remains optional and retains its type and description — only the non-serializable default sentinel is dropped, which is exactly what Pydantic was going to do anyway (with a warning).
Tests
Added
test_tool_field_default_factory_no_pydantic_warningregression test that asserts noUserWarningis raised when accessingtool_specfor a tool withField(default_factory=list).