Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/agents/_tool_invocation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)

Expand All @@ -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:
Expand Down
12 changes: 12 additions & 0 deletions tests/test_tool_approval_call_id_reuse.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Loading