Skip to content

Commit 45f5b61

Browse files
CopilotCopilot
andcommitted
Address review: call_id fallback and raw_representation consistency (#4814)
- Add call_id fallback in response.output_item.done mcp_call handler to match the output_item.added handler pattern - Use done_item instead of event for raw_representation to keep consistent with other output_item branches and non-streaming path - Add test for call_id fallback when id attribute is missing - Add raw_representation assertions to existing done handler tests Co-authored-by: Copilot <[email protected]>
1 parent 866265a commit 45f5b61

2 files changed

Lines changed: 30 additions & 2 deletions

File tree

‎python/packages/core/agent_framework/openai/_responses_client.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2205,7 +2205,7 @@ def _get_ann_value(key: str) -> Any:
22052205
case "response.output_item.done":
22062206
done_item = event.item
22072207
if getattr(done_item, "type", None) == "mcp_call":
2208-
call_id = getattr(done_item, "id", None) or ""
2208+
call_id = getattr(done_item, "id", None) or getattr(done_item, "call_id", None) or ""
22092209
output_text = getattr(done_item, "output", None)
22102210
parsed_output: list[Content] | None = (
22112211
[Content.from_text(text=output_text)] if isinstance(output_text, str) else None
@@ -2214,7 +2214,7 @@ def _get_ann_value(key: str) -> Any:
22142214
Content.from_mcp_server_tool_result(
22152215
call_id=call_id,
22162216
output=parsed_output,
2217-
raw_representation=event,
2217+
raw_representation=done_item,
22182218
)
22192219
)
22202220
case _:

‎python/packages/core/tests/openai/test_openai_responses_client.py‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1250,6 +1250,7 @@ def test_parse_chunk_from_openai_with_mcp_output_item_done() -> None:
12501250
assert result_content.output is not None
12511251
assert len(result_content.output) == 1
12521252
assert result_content.output[0].text == "The weather in Seattle is 72F and sunny."
1253+
assert result_content.raw_representation is mock_item
12531254

12541255

12551256
def test_parse_chunk_from_openai_with_mcp_output_item_done_no_output() -> None:
@@ -1275,6 +1276,33 @@ def test_parse_chunk_from_openai_with_mcp_output_item_done_no_output() -> None:
12751276
assert result_content.type == "mcp_server_tool_result"
12761277
assert result_content.call_id == "mcp_call_789"
12771278
assert result_content.output is None
1279+
assert result_content.raw_representation is mock_item
1280+
1281+
1282+
def test_parse_chunk_from_openai_with_mcp_output_item_done_call_id_fallback() -> None:
1283+
"""Test that response.output_item.done for mcp_call falls back to call_id when id is missing."""
1284+
client = OpenAIResponsesClient(model_id="test-model", api_key="test-key")
1285+
1286+
mock_event = MagicMock()
1287+
mock_event.type = "response.output_item.done"
1288+
1289+
mock_item = MagicMock(spec=[])
1290+
mock_item.type = "mcp_call"
1291+
mock_item.call_id = "mcp_fallback_123"
1292+
mock_item.output = "fallback result"
1293+
mock_event.item = mock_item
1294+
1295+
function_call_ids: dict[int, tuple[str, str]] = {}
1296+
1297+
update = client._parse_chunk_from_openai(mock_event, options={}, function_call_ids=function_call_ids)
1298+
1299+
assert len(update.contents) == 1
1300+
result_content = update.contents[0]
1301+
1302+
assert result_content.type == "mcp_server_tool_result"
1303+
assert result_content.call_id == "mcp_fallback_123"
1304+
assert result_content.output is not None
1305+
assert result_content.output[0].text == "fallback result"
12781306

12791307

12801308
def test_prepare_message_for_openai_with_function_approval_response() -> None:

0 commit comments

Comments
 (0)