From 46eb734acb5a2a8e0ac6645a0c725c0ac47173e7 Mon Sep 17 00:00:00 2001 From: Mark Stuart Date: Tue, 24 Nov 2026 20:32:52 -0800 Subject: [PATCH] Avoid rebuilding JSON arguments for tool invocation fingerprints --- src/agents/_tool_invocation.py | 8 ++++++-- tests/test_tool_approval_call_id_reuse.py | 12 ++++++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/src/agents/_tool_invocation.py b/src/agents/_tool_invocation.py index 8685e3ec6e..44be9b44a0 100644 --- a/src/agents/_tool_invocation.py +++ b/src/agents/_tool_invocation.py @@ -88,6 +88,9 @@ def _has_other_model_type(value: Any, types: Collection[str]) -> bool: def _normalize_value(value: Any, *, exclude_none: bool = False) -> Any: + kind = type(value) + if value is None or kind is str or kind is int or kind is float or kind is bool: + return value mapping = _as_mapping(value) if mapping is not None: return { @@ -97,7 +100,7 @@ def _normalize_value(value: Any, *, exclude_none: bool = False) -> Any: } if isinstance(value, Sequence) and not isinstance(value, str | bytes | bytearray): return [_normalize_value(item, exclude_none=exclude_none) for item in value] - if value is None or isinstance(value, str | int | float | bool): + if isinstance(value, str | int | float | bool): return value return str(value) @@ -114,7 +117,8 @@ def _normalize_arguments(value: Any) -> Any: ) except (TypeError, ValueError, json.JSONDecodeError): return value - return _normalize_value(parsed) + # The decoder produces plain JSON values. The fingerprint encoder sorts object keys. + return parsed def _unwrap_hosted_mcp_approval(raw_item: Any) -> Mapping[str, Any] | None: diff --git a/tests/test_tool_approval_call_id_reuse.py b/tests/test_tool_approval_call_id_reuse.py index 25267df7a6..75a77ed5d1 100644 --- a/tests/test_tool_approval_call_id_reuse.py +++ b/tests/test_tool_approval_call_id_reuse.py @@ -226,6 +226,18 @@ def test_canonical_shell_identity_treats_optional_nulls_as_omitted() -> None: assert tool_invocation_identity(provider_call) == tool_invocation_identity(normalized_call) +def test_nested_function_identity_matches_reordered_json_and_object_arguments() -> None: + call = {"type": "function_call", "call_id": "call_0", "name": "lookup"} + arguments = {"z": [{"b": None, "a": "été"}, 2], "a": True} + expected = tool_invocation_identity({**call, "arguments": arguments}) + assert expected is not None + assert tool_invocation_identity({**call, "arguments": json.dumps(arguments)}) == expected + reordered = '{"a":true,"z":[{"a":"été","b":null},2]}' + assert tool_invocation_identity({**call, "arguments": reordered}) == expected + changed = '{"a":true,"z":[2,{"a":"été","b":null}]}' + assert tool_invocation_identity({**call, "arguments": changed}) != expected + + def test_canonical_function_identity_preserves_null_argument_values() -> None: call_with_null = { "type": "function_call",