Repository navigation
Commit a75e803
Python: Fix tool argument validation for datetime, set and tuple parameters (#8824)
* Python: Fix argument validation for non-JSON-native tool parameters
FunctionTool._prepare_arguments ran the lightweight JSON schema checks on
the output of the pydantic input model. Pydantic had already converted the
JSON values to the annotated Python types, so datetime, set and tuple
parameters were rejected against their string/array schema on every call.
Skip the lightweight checks when the input model validated the arguments;
they still run for tools declared with a JSON schema dictionary.
Fixes #8661
* Python: Bind converted tool arguments for approval and security middleware
Skipping the JSON type checks lets datetime, set and other converted values
reach the function, but approval and security middleware bind arguments with
a snapshot token, and those types had none, so an approved call failed with
MiddlewareFailure.
- Add value tokens for set and frozenset, and for the exact immutable types
date, datetime, time, timedelta, Decimal and UUID (with no tzinfo or a
fixed-offset one). Tokens use the string form, so changes that == misses
(same instant in another time zone, Decimal("1.0") vs "1.00") still
require a new approval. Subclasses stay opaque and fail closed.
- Check an input model instance against the schema in its JSON form instead
of skipping the checks, so a model_construct() instance with wrong types
is still rejected.
Fixes #8661
Co-Authored-By: Claude Opus 5.5 <[email protected]>
* Python: Build input model instance with model_validate in tool test
ty checks a call on the narrowed type[BaseModel], where keyword arguments
look like extra fields Pydantic discards, and failed the Test Typing Checks
job. model_validate takes a dict, so every checker accepts it without
suppressions, and it validates the values the same way.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
* Python: Build argument tokens from fields and revalidate input model instances
Authority tokens for date, datetime, time, timedelta, Decimal and UUID
values now list every field a tool can observe instead of using str(),
which left out a datetime's fold, a UUID's is_safe and a time zone's
name, so a changed value could keep the approved token.
An input model instance is now validated through the input model, like
mapping arguments, so the function receives the annotated types even
when the instance was built with model_construct().
* Python: Serialize replacement approval arguments and count shared tokens
A replacement approval request held the repaired arguments as the
Python values the input model converted them to (datetime, set,
Decimal, UUID), so a session holding it could not be saved as JSON,
Message.to_json() dropped some of those values, and the stored request
shared its set with the one returned to the caller. The arguments are
now converted to their JSON form first. Approving the request prepares
that JSON again, so a repair JSON cannot carry exactly (a datetime's
fold, a UUID's is_safe, a standard-library time zone) now fails closed
instead of asking for approval again on every round.
Argument tokens for sets, frozensets and dict entries now count items
that share a token. NaN is not equal to itself, so a set can hold
several NaNs with the same token, and removing one after approval left
the token unchanged.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
* Python: Fail closed on any replacement argument serialization error
to_jsonable_python does not only raise PydanticSerializationError: bytes
that are not UTF-8 raise UnicodeDecodeError, and other values raise
ValueError or any error raised while they are serialized. Those escaped
as raw exceptions while the replacement approval request was built.
Wrap every serialization failure in the same MiddlewareFailure so such
repairs fail closed consistently.
---------
Co-authored-by: Claude Opus 5.5 <[email protected]>1 parent e7ca6c2 commit a75e803
5 files changed
Lines changed: 735 additions & 15 deletions
File tree
- docs/specs
- python/packages/core
- agent_framework
- tests
- core
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
472 | 472 | | |
473 | 473 | | |
474 | 474 | | |
475 | | - | |
476 | | - | |
477 | | - | |
| 475 | + | |
| 476 | + | |
| 477 | + | |
| 478 | + | |
| 479 | + | |
478 | 480 | | |
479 | 481 | | |
480 | 482 | | |
| |||
701 | 703 | | |
702 | 704 | | |
703 | 705 | | |
704 | | - | |
| 706 | + | |
705 | 707 | | |
706 | 708 | | |
707 | 709 | | |
| |||
0 commit comments