Repository navigation
fix(tools): return validated model instances from validate_input - #5546
Open
xiong-wei1 wants to merge 1 commit into
Open
xiong-wei1 wants to merge 1 commit into
xiong-wei1 wants to merge 1 commit into
Conversation
model_dump() recursively converts nested BaseModel values to dicts, so a tool parameter annotated with a model type reached the function as a dict instead of the validated instance. Read the validated values from the input model instance, keyed like model_dump(). Fixes strands-agents#5545
This branch has not been deployed
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.
Human Overview
I use @tool functions with pydantic model parameters; they work when called directly but crash under an agent because the parameter arrives as a dict. This PR makes the agent path pass the validated model instance, like direct calls do.
Description
Tools whose parameters are annotated with a pydantic model class crash on every agent invocation: validation succeeds, but the function receives a plain dict instead of the model instance its annotation promises, so the first attribute access raises AttributeError. Direct Python calls work, which makes the failure specific to the agent path and hard to diagnose. This change makes agent invocation deliver the validated model instances, matching direct-call behavior for plain, optional, and list-of-model parameters.
Related Issues
#5545
Documentation PR
Type of Change
Bug fix
Testing
Added three regression tests covering model-typed, optional model-typed, and list-of-model tool parameters through the agent-facing stream path. Locally: the new tests fail on main and pass with this change; the full tests/strands/tools/test_decorator.py suite shows no new failures (78 passed; the 10 pre-existing failures are environment-related Bedrock/botocore tests that fail identically on unmodified main). Ruff check and format, and mypy on the changed source file, are clean. Equivalent checks were run directly with pytest/ruff/mypy rather than via hatch.
hatch run prepareAdditional Details
Extended context (optional)
Checklist
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.