Skip to content
Merged
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
46 changes: 42 additions & 4 deletions python/packages/foundry/agent_framework_foundry/_chat_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2327,3 +2327,76 @@ 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
# 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"]
Loading