mirror of
https://github.com/microsoft/agent-framework.git
synced 2026-06-16 21:04:09 +08:00
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>
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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"
|
||||
|
||||
Reference in New Issue
Block a user