Skip to content
Open
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
2 changes: 1 addition & 1 deletion src/agents/run_internal/items.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions src/agents/util/_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
10 changes: 10 additions & 0 deletions tests/test_items_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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] = []
Expand Down
17 changes: 17 additions & 0 deletions tests/test_run_internal_items.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"] == "{}"
Loading