Skip to content

Commit a75e803

Browse files
Aditya-XRclaude
andauthored
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/004-python-function-calling-loop.md‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -472,9 +472,11 @@ that manually replay messages own the equivalent rule: do not resend an approval
472472
nothing and yields a caller-visible, session-persisted replacement request for the same occurrence; execution
473473
requires a second approval and happens exactly once.
474474
- If function middleware repairs an approval-bound call, the old response likewise executes nothing and yields a
475-
caller-visible, session-persisted replacement request containing the repaired approval-visible arguments. The
476-
replacement retains the call occurrence identity, rotates request-generation identity, and requires a second
477-
approval before exactly-once execution. Security middleware transformations that preserve an approval-visible
475+
caller-visible, session-persisted replacement request containing the repaired approval-visible arguments in
476+
JSON-native form. The replacement retains the call occurrence identity, rotates request-generation identity, and
477+
requires a second approval before exactly-once execution. A repair whose JSON form would not prepare back to the
478+
same values (for example a datetime `fold` or a standard-library time zone) fails closed instead, because no
479+
approval of that request could settle. Security middleware transformations that preserve an approval-visible
478480
placeholder do not disclose the resolved value or trigger a spurious replacement.
479481
- If session-bound middleware no longer holds the reviewed authority because it expired or was evicted, the matched
480482
response executes nothing and produces a replacement approval request with the same occurrence identity and a fresh
@@ -701,7 +703,7 @@ that manually replay messages own the equivalent rule: do not resend an approval
701703
| Custom result-parser exception | Failed custom conversion produces one tool-error result rather than the raw return value, in both response modes and with observability enabled or disabled; detailed-error configuration is preserved. | `packages/core/tests/core/test_function_invocation_logic.py::test_function_invocation_result_parser_failure`, `packages/core/tests/core/test_tools.py::test_invoke_result_parser_exception_propagates` |
702704
| Approved validation error | Validation failure returns one result without invoking the function body. | `test_approved_function_call_with_validation_error` |
703705
| Pre-validation middleware repair | Schema-compatible calls retain normalized middleware arguments without duplicate validation. Prepared-value reuse does not require validator outputs to be copyable, and unchanged NaNs remain stable. Identity-only opaque values may be reused for ordinary execution, but security authority rejects them before downstream middleware or the tool body because in-place mutation cannot be detected safely. When provisional validation fails, function middleware observes raw parsed arguments, may repair them before final validation, and the body receives normalized values; short-circuiting skips final validation and execution. Repair after security middleware fails closed using recursive type-aware, float-bit-exact comparison, including invalid and short-circuited mutations. Security inspects exact normalized values, and validation errors after hidden-value resolution do not disclose resolved values or mapping keys, including validator `TypeError` paths. | `test_function_middleware_keeps_normalized_arguments_for_valid_calls`, `test_prepared_arguments_support_noncopyable_validator_output`, `test_nan_prepared_and_approval_snapshots_are_stable`, `test_function_middleware_repairs_raw_arguments_before_validation`, `test_function_middleware_can_short_circuit_before_argument_validation`, `test_invalid_arguments_produced_by_middleware_keep_argument_error_contract`, `packages/core/tests/test_security.py::TestVariableArgumentPolicy::test_security_rejects_opaque_mutable_validator_output`, `test_argument_mutation_after_security_middleware_fails_closed`, `test_security_snapshot_accepts_unchanged_nan`, `test_argument_mutation_after_security_short_circuit_fails_closed`, `test_security_policy_observes_custom_validator_transform_once`, `test_hidden_argument_validation_error_does_not_disclose_resolved_value`, `test_hidden_mapping_key_is_not_disclosed_by_validation_error`, `test_hidden_value_is_not_disclosed_by_validator_type_error`, `test_hidden_argument_can_be_normalized_after_security_check` |
704-
| Approved middleware repair | Approval binds to the normalized middleware-entry representation, so ordinary Pydantic coercion still completes in one approval round. A changed approval-bound call executes zero times under the old grant, returns a persisted occurrence-bound replacement request in both response modes, and executes once only after the replacement is approved. Recursive type-aware, float-bit-exact comparison treats booleans and numbers, and positive and negative zero, as distinct while keeping unchanged NaNs stable. Opaque mutable normalized values fail closed before approval authority is established. The same replacement rule applies when middleware short-circuits instead of calling the tool. Security expansion preserves approval-visible placeholders. | `test_approved_coercing_arguments_execute_without_replacement`, `test_approved_argument_repair_requires_replacement_approval`, `test_approved_argument_repair_short_circuit_requires_replacement_approval`, `test_approval_snapshot_distinguishes_exact_values`, `test_approval_rejects_opaque_mutable_validator_output`, `packages/core/tests/test_security.py::TestVariableArgumentPolicy::test_hidden_argument_resolution_does_not_require_reapproval` |
706+
| Approved middleware repair | Approval binds to the normalized middleware-entry representation, so ordinary Pydantic coercion still completes in one approval round. A changed approval-bound call executes zero times under the old grant, returns a persisted occurrence-bound replacement request in both response modes, and executes once only after the replacement is approved. Recursive type-aware, float-bit-exact comparison treats booleans and numbers, and positive and negative zero, as distinct while keeping unchanged NaNs stable; set elements and mapping entries are counted, so removing one of several NaNs is detected. Opaque mutable normalized values fail closed before approval authority is established. The replacement holds JSON-native arguments that survive session serialization, and a repair whose JSON form would prepare to different values fails closed. The same replacement rule applies when middleware short-circuits instead of calling the tool. Security expansion preserves approval-visible placeholders. | `test_approved_coercing_arguments_execute_without_replacement`, `test_approved_argument_repair_requires_replacement_approval`, `test_approved_argument_repair_short_circuit_requires_replacement_approval`, `test_approval_snapshot_distinguishes_exact_values`, `test_approval_rejects_opaque_mutable_validator_output`, `test_approval_detects_changes_to_converted_values`, `test_approval_change_that_json_cannot_represent_fails_closed`, `test_argument_tokens_count_items_that_share_a_token`, `test_approved_repair_replacement_round_trips_through_json`, `packages/core/tests/test_security.py::TestVariableArgumentPolicy::test_hidden_argument_resolution_does_not_require_reapproval` |
705707
| Approved success | Successful approved execution returns one result. | `test_approved_function_call_successful_execution` |
706708
| Consecutive error cap | Error threshold stops repeated failures, submits collected results, and makes only the required final no-tool model call. | `test_function_invocation_config_max_consecutive_errors`, `test_streaming_function_invocation_config_max_consecutive_errors`, `test_approval_resume_error_limit_forces_final_no_tool_response` |
707709
| Unknown call handling | Configured false returns an error result; configured true raises before any call executes. A service-managed continuation settles every call in the aborted batch and advances to the settlement response before the `KeyError` propagates; without one, including local per-service-call history persistence, no extra request is made. | `test_function_invocation_config_terminate_on_unknown_calls_false`, `test_function_invocation_config_terminate_on_unknown_calls_true`, streaming equivalents, `test_fatal_unknown_settles_service_conversation`, `test_fatal_unknown_with_local_history_makes_no_settlement_request` |

0 commit comments

Comments
 (0)