From 98b317ac5d79ec98674a48b428a31ca059a47177 Mon Sep 17 00:00:00 2001 From: Wilson0511 <1795618152@qq.com> Date: Sun, 1 Nov 2026 17:39:34 +0800 Subject: [PATCH 1/2] Python: Attach Foundry reasoning replay data to the message that owns it MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit #9768 made multi-message responses possible (one Message per provider output message, trailing reasoning parsed into the last message), but the Foundry replay attachment only searched messages[0]: a reasoning item owned by a later message appended a duplicate empty reasoning content to the first message while the real reasoning content kept no replay payload, so stateless replay grouped two contents for the same provider id and the visible transcript gained a spurious empty reasoning item. Search every message (most recent first) and append to the last message when nothing matches. The streaming path already attaches per update and is unchanged. 🤖 Generated with ZCode Co-Authored-By: ZCode --- .../agent_framework_foundry/_chat_client.py | 46 +++++++++++-- .../tests/foundry/test_foundry_chat_client.py | 65 +++++++++++++++++++ 2 files changed, 107 insertions(+), 4 deletions(-) diff --git a/python/packages/foundry/agent_framework_foundry/_chat_client.py b/python/packages/foundry/agent_framework_foundry/_chat_client.py index 5299212e654..1a126cbaf35 100644 --- a/python/packages/foundry/agent_framework_foundry/_chat_client.py +++ b/python/packages/foundry/agent_framework_foundry/_chat_client.py @@ -380,15 +380,53 @@ def _parse_response_from_openai( ) -> ChatResponse: chat_response = super()._parse_response_from_openai(response, options) if chat_response.messages: + messages = chat_response.messages for item in getattr(response, "output", ()): if getattr(item, "type", None) != "reasoning": continue - self._attach_foundry_reasoning_replay_item(chat_response.messages[0].contents, item) - chat_response.messages[0].contents.extend( - parse_oauth_consent_output_items(getattr(response, "output", None)) - ) + self._attach_foundry_reasoning_replay_item_to_messages(messages, item) + messages[0].contents.extend(parse_oauth_consent_output_items(getattr(response, "output", None))) return chat_response + @classmethod + def _attach_foundry_reasoning_replay_item_to_messages(cls, messages: list[Message], item: Any) -> None: + """Attach replay data to the message that owns the reasoning content. + + Multi-message responses keep one message per provider output message, and a + trailing reasoning item is parsed into the last message, so the owning + content can live in any message. Search every message (most recent first) + and only append to the last message when no parsed content matches, instead + of always targeting the first message, which duplicated the reasoning + content there while the real one kept no replay data. + """ + payload = cls._prepare_foundry_reasoning_replay_item(item) + if payload is None: + return + reasoning_id = cast(str, payload["id"]) + for message in reversed(messages): + for content in reversed(message.contents): + if content.type == "text_reasoning" and content.id == reasoning_id: + updated = with_namespaced_value( + content.additional_properties, + "foundry", + "reasoning_item", + payload, + ) + updated[_FOUNDRY_REASONING_REPLAY_ITEM_KEY] = payload + content.additional_properties = updated + return + messages[-1].contents.append( + Content.from_text_reasoning( + id=reasoning_id, + text="", + raw_representation=item, + additional_properties={ + "foundry": {"reasoning_item": payload}, + _FOUNDRY_REASONING_REPLAY_ITEM_KEY: payload, + }, + ) + ) + @override def _check_model_presence(self, options: dict[str, Any]) -> None: if not options.get("model"): diff --git a/python/packages/foundry/tests/foundry/test_foundry_chat_client.py b/python/packages/foundry/tests/foundry/test_foundry_chat_client.py index 0a5defda458..7995edaa6a5 100644 --- a/python/packages/foundry/tests/foundry/test_foundry_chat_client.py +++ b/python/packages/foundry/tests/foundry/test_foundry_chat_client.py @@ -2327,3 +2327,68 @@ async def test_shared_chat_client_keeps_concurrent_agent_runs_isolated( assert first_session.service_session_id == "response-first" assert second_session.service_session_id == "response-second" assert transport.max_active_requests == 2 + + +async def test_multi_message_response_attaches_reasoning_replay_to_owning_message() -> None: + """Reasoning replay data must land on the message that owns the reasoning content. + + #9768 made multi-message responses possible (e.g. preamble + follow-up message). + A reasoning item that trails the last message is parsed into that message, but + the Foundry replay attachment only searched ``messages[0]``: it appended a + duplicate empty reasoning content there while the real reasoning content in the + later message kept no replay payload. + """ + from openai.types.responses.response_output_message import ResponseOutputMessage + from openai.types.responses.response_output_text import ResponseOutputText + from openai.types.responses.response_reasoning_item import ResponseReasoningItem + + def message_item(item_id: str, text: str) -> ResponseOutputMessage: + return ResponseOutputMessage.model_construct( + type="message", + id=item_id, + role="assistant", + status="completed", + content=[ResponseOutputText.model_construct(type="output_text", text=text, annotations=[])], + ) + + reasoning_item = ResponseReasoningItem.model_construct( + type="reasoning", + id="rs_trailing", + response_id="response_123", + summary=[], + content=[], + ) + + mock_response = MagicMock( + id="response_123", + model="test-model", + created_at=1000000000, + metadata={}, + output_parsed=None, + output=[message_item("message_0", "A"), message_item("message_1", "B"), reasoning_item], + usage=None, + finish_reason=None, + conversation=None, + status="completed", + ) + + async def create_response(**kwargs: Any) -> Any: + return _as_raw(mock_response) + + mock_openai_client = _make_mock_openai_client() + mock_openai_client.responses.with_raw_response.create.side_effect = create_response + project_client = MagicMock() + project_client.get_openai_client.return_value = mock_openai_client + client = FoundryChatClient(project_client=project_client, model="test-model") + + response = await client.get_response([Message(role="user", contents=["Hello"])]) + + assert [message.text for message in response.messages] == ["A", "B"] + reasoning_contents = [ + content for message in response.messages for content in message.contents if content.type == "text_reasoning" + ] + assert len(reasoning_contents) == 1, [ + (message.message_id, content.type, content.id) for message in response.messages for content in message.contents + ] + assert reasoning_contents[0].id == "rs_trailing" + assert reasoning_contents[0].additional_properties.get("__foundry_reasoning_replay_item__") is not None From 2f67e3873f1d22de9879d9da7a56049d2a714cf0 Mon Sep 17 00:00:00 2001 From: Wilson0511 <1795618152@qq.com> Date: Sun, 1 Nov 2026 18:01:45 +0800 Subject: [PATCH 2/2] Python: Pin the owning message in the Foundry reasoning replay regression test MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Review follow-up on #9931: also assert that the rs_trailing content lives in message_1, since the flattened checks would pass even if the reasoning (with its replay payload) were attached to message_0. 🤖 Generated with ZCode Co-Authored-By: ZCode --- .../foundry/tests/foundry/test_foundry_chat_client.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/python/packages/foundry/tests/foundry/test_foundry_chat_client.py b/python/packages/foundry/tests/foundry/test_foundry_chat_client.py index 7995edaa6a5..0e079c21726 100644 --- a/python/packages/foundry/tests/foundry/test_foundry_chat_client.py +++ b/python/packages/foundry/tests/foundry/test_foundry_chat_client.py @@ -2392,3 +2392,11 @@ async def create_response(**kwargs: Any) -> Any: ] assert reasoning_contents[0].id == "rs_trailing" assert reasoning_contents[0].additional_properties.get("__foundry_reasoning_replay_item__") is not None + # Pin the owning message: a flattened check would also pass if the reasoning + # content (with its replay payload) were attached to message_0 instead. + owning_message_ids = [ + message.message_id + for message in response.messages + if any(content.type == "text_reasoning" for content in message.contents) + ] + assert owning_message_ids == ["message_1"]