From 03209d58de8441e09de91c145452e2a1098f284d Mon Sep 17 00:00:00 2001 From: Mark Stuart Date: Tue, 24 Nov 2026 20:55:10 -0800 Subject: [PATCH] Reduce redundant dictionary work when preparing conversation input --- src/agents/run_internal/items.py | 2 +- src/agents/util/_json.py | 4 ++-- tests/test_items_helpers.py | 10 ++++++++++ tests/test_run_internal_items.py | 17 +++++++++++++++++ 4 files changed, 30 insertions(+), 3 deletions(-) diff --git a/src/agents/run_internal/items.py b/src/agents/run_internal/items.py index a5ac48d6ec..7d15b4bbaf 100644 --- a/src/agents/run_internal/items.py +++ b/src/agents/run_internal/items.py @@ -739,7 +739,7 @@ def _peek_unused(candidates: deque[int] | None) -> int | None: def _dedupe_key(item: TResponseInputItem) -> str | None: """Return a stable identity key when items carry explicit identifiers.""" - payload = _coerce_to_dict(item) + payload = item if type(item) is dict else _coerce_to_dict(item) if payload is None: return None diff --git a/src/agents/util/_json.py b/src/agents/util/_json.py index 5f940cea54..3df40f687e 100644 --- a/src/agents/util/_json.py +++ b/src/agents/util/_json.py @@ -70,8 +70,8 @@ def _to_dump_compatible_internal(obj: Any) -> Any: if type(obj) is str: return obj - if isinstance(obj, Mapping): - return {k: _to_dump_compatible_internal(v) for k, v in obj.items()} + if type(obj) is dict or isinstance(obj, Mapping): + return {k: v if type(v) is str else _to_dump_compatible_internal(v) for k, v in obj.items()} if isinstance(obj, list | tuple): return [_to_dump_compatible_internal(x) for x in obj] diff --git a/tests/test_items_helpers.py b/tests/test_items_helpers.py index 4de08aa24c..640fa102b1 100644 --- a/tests/test_items_helpers.py +++ b/tests/test_items_helpers.py @@ -180,6 +180,16 @@ def test_input_to_new_input_list_deep_copies_lists() -> None: assert "content" in original[0] and original[0].get("content") == "abc" +def test_repeated_input_messages_keep_independent_nested_content() -> None: + message = {"role": "user", "content": [{"type": "input_text", "text": "original"}]} + original = cast(list[TResponseInputItem], [message, message]) + first, second = ItemHelpers.input_to_new_input_list(original) + assert first == second == message + cast(Any, first)["content"][0]["text"] = "changed" + assert second == message + assert cast(Any, message)["content"][0]["text"] == "original" + + def test_text_message_output_concatenates_text_segments() -> None: # Build a message with both text and refusal segments, only text segments are concatenated. pieces: list[ResponseOutputText | ResponseOutputRefusal] = [] diff --git a/tests/test_run_internal_items.py b/tests/test_run_internal_items.py index 1cdc793213..324cab0692 100644 --- a/tests/test_run_internal_items.py +++ b/tests/test_run_internal_items.py @@ -1191,3 +1191,20 @@ def test_deduplicate_input_items_preferring_latest_leaves_unique_items_untouched ] assert run_items.deduplicate_input_items_preferring_latest(items) == items + + +def test_latest_deduplication_keeps_mixed_message_and_tool_order() -> None: + user = {"role": "user", "content": "before"} + followup = {"role": "user", "content": "after"} + call = {"type": "function_call", "call_id": "c", "name": "lookup", "arguments": "{}"} + latest_call = {**call, "arguments": '{"x":1}'} + reasoning = {"type": "reasoning", "id": "r", "summary": []} + latest_reasoning = {**reasoning, "summary": [{"type": "summary_text", "text": "latest"}]} + output = {"type": "function_call_output", "call_id": "c", "output": "old"} + latest_output = {**output, "output": "new"} + items = [user, call, reasoning, output, followup, latest_call, latest_reasoning, latest_output] + result = run_items.deduplicate_input_items_preferring_latest(cast(Any, items)) + expected = [user, latest_call, latest_reasoning, followup, latest_output] + assert len(result) == len(expected) + assert all(actual is wanted for actual, wanted in zip(result, expected, strict=True)) + assert items[1] is call and call["arguments"] == "{}"