From 3b9dc74aff61f9e04c35e97189f193cef48e0c43 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Tue, 10 Feb 2026 11:05:24 +0000 Subject: [PATCH] Preserve system/developer messages when filtering with previous_response_id - Updated filtering logic to preserve system/developer messages (API accepts these roles) - System messages are collected from before last assistant and combined with new user messages - Updated test to verify system message preservation - All 92 tests pass Co-authored-by: markwallace-microsoft <127216156+markwallace-microsoft@users.noreply.github.com> --- .../agent_framework/openai/_responses_client.py | 14 +++++++++++--- .../tests/openai/test_openai_responses_client.py | 11 ++++++----- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/python/packages/core/agent_framework/openai/_responses_client.py b/python/packages/core/agent_framework/openai/_responses_client.py index 5aa9cc1a15..3cc69d2aaf 100644 --- a/python/packages/core/agent_framework/openai/_responses_client.py +++ b/python/packages/core/agent_framework/openai/_responses_client.py @@ -673,10 +673,18 @@ class RawOpenAIResponsesClient( # type: ignore[misc] if message.role == "assistant": last_assistant_idx = idx - # Only include messages after the last assistant message - # This ensures we only send NEW user messages, not the full history + # When using previous_response_id, filter out assistant and function result messages + # but keep system/developer/user messages (the API accepts these roles) if last_assistant_idx >= 0: - chat_messages = chat_messages[last_assistant_idx + 1 :] + # Collect system/developer messages from before the last assistant + system_messages = [ + msg for msg in chat_messages[:last_assistant_idx] + if msg.role in ("system", "developer") + ] + # Get all messages after the last assistant (new user messages) + new_messages = chat_messages[last_assistant_idx + 1 :] + # Combine: system messages + new messages + chat_messages = system_messages + list(new_messages) list_of_list = [self._prepare_message_for_openai(message, call_id_to_id) for message in chat_messages] # Flatten the list of lists into a single list diff --git a/python/packages/core/tests/openai/test_openai_responses_client.py b/python/packages/core/tests/openai/test_openai_responses_client.py index f9bf1c3dfb..066882d91e 100644 --- a/python/packages/core/tests/openai/test_openai_responses_client.py +++ b/python/packages/core/tests/openai/test_openai_responses_client.py @@ -2117,18 +2117,19 @@ async def test_message_filtering_with_previous_response_id() -> None: ChatMessage(role="user", text="What's my name?"), ] - # When using previous_response_id, only new messages after last assistant should be included + # When using previous_response_id, assistant messages should be filtered but system messages preserved options = await client._prepare_options( messages, {"conversation_id": "resp_12345"}, # Using resp_ prefix ) # type: ignore - # Should only include the last user message + # Should include: system message + last user message assert "input" in options input_messages = options["input"] - assert len(input_messages) == 1 - assert input_messages[0]["role"] == "user" - assert "What's my name?" in str(input_messages[0]) + assert len(input_messages) == 2, f"Expected 2 messages (system + user), got {len(input_messages)}" + assert input_messages[0]["role"] == "system" + assert input_messages[1]["role"] == "user" + assert "What's my name?" in str(input_messages[1]) # Verify previous_response_id is set assert options["previous_response_id"] == "resp_12345"