-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Python: Fixes Issue 3206 where user input is echoed back as in AgentResponse when an agent emits user messages. #4482
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
d26fc25
8fac5f6
5749362
2ffaebc
642dc7e
fb4c61d
c53b201
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -32,6 +32,7 @@ | |
| from ..exceptions import AgentInvalidRequestException, AgentInvalidResponseException | ||
| from ._checkpoint import CheckpointStorage | ||
| from ._events import ( | ||
| OrchestrationComplete, | ||
| WorkflowEvent, | ||
| ) | ||
| from ._message_utils import normalize_messages_input | ||
|
|
@@ -449,6 +450,7 @@ def _convert_workflow_events_to_agent_response( | |
| raw_representations: list[object] = [] | ||
| merged_usage: UsageDetails | None = None | ||
| latest_created_at: str | None = None | ||
| orchestration_complete: OrchestrationComplete | None = None | ||
|
|
||
| for output_event in output_events: | ||
| if output_event.type == "request_info": | ||
|
|
@@ -465,6 +467,12 @@ def _convert_workflow_events_to_agent_response( | |
| raw_representations.append(output_event) | ||
| else: | ||
| data = output_event.data | ||
| if isinstance(data, OrchestrationComplete): | ||
| # Save for fallback: some orchestrations (e.g. group_chat) only emit | ||
| # OrchestrationComplete without separate per-agent output events. | ||
| orchestration_complete = data | ||
| continue | ||
|
|
||
| if isinstance(data, AgentResponseUpdate): | ||
| # We cannot support AgentResponseUpdate in non-streaming mode. This is because the message | ||
| # sequence cannot be guaranteed when there are streaming updates in between non-streaming | ||
|
|
@@ -475,8 +483,10 @@ def _convert_workflow_events_to_agent_response( | |
| ) | ||
|
|
||
| if isinstance(data, AgentResponse): | ||
| messages.extend(data.messages) | ||
| raw_representations.append(data.raw_representation) | ||
| non_user_messages = [msg for msg in data.messages if msg.role != "user"] | ||
| messages.extend(non_user_messages) | ||
| if non_user_messages: | ||
| raw_representations.append(data.raw_representation) | ||
| merged_usage = add_usage_details(merged_usage, data.usage_details) | ||
| latest_created_at = ( | ||
| data.created_at | ||
|
|
@@ -486,12 +496,15 @@ def _convert_workflow_events_to_agent_response( | |
| else latest_created_at | ||
| ) | ||
| elif isinstance(data, Message): | ||
| messages.append(data) | ||
| raw_representations.append(data.raw_representation) | ||
| if data.role != "user": | ||
| messages.append(data) | ||
| raw_representations.append(data.raw_representation) | ||
| elif is_instance_of(data, list[Message]): | ||
| chat_messages = cast(list[Message], data) | ||
| messages.extend(chat_messages) | ||
| raw_representations.append(data) | ||
| non_user_messages = [msg for msg in chat_messages if msg.role != "user"] | ||
| messages.extend(non_user_messages) | ||
| if non_user_messages: | ||
| raw_representations.append(data) | ||
| else: | ||
| contents = self._extract_contents(data) | ||
| if not contents: | ||
|
|
@@ -508,6 +521,15 @@ def _convert_workflow_events_to_agent_response( | |
| ) | ||
| raw_representations.append(data) | ||
|
|
||
| # Fallback: if no messages were collected from other output events but an | ||
| # OrchestrationComplete was emitted, use its messages. This covers orchestrations | ||
| # (e.g. group_chat) where the orchestrator runs agents internally and only | ||
| # emits OrchestrationComplete as a workflow output. | ||
| if not messages and orchestration_complete is not None: | ||
| messages.extend(orchestration_complete.messages) | ||
| if orchestration_complete.messages: | ||
| raw_representations.append(orchestration_complete) | ||
|
|
||
| return AgentResponse( | ||
| messages=messages, | ||
| response_id=response_id, | ||
|
|
@@ -570,15 +592,26 @@ def _convert_workflow_event_to_agent_response_updates( | |
| data = event.data | ||
| executor_id = event.executor_id | ||
|
|
||
| if isinstance(data, OrchestrationComplete): | ||
| # OrchestrationComplete carries the full conversation for direct | ||
| # workflow consumers but should not be included in streaming output. | ||
| return [] | ||
|
|
||
| if isinstance(data, AgentResponseUpdate): | ||
| # Pass through AgentResponseUpdate directly (streaming from AgentExecutor) | ||
| # Pass through AgentResponseUpdate directly (streaming from AgentExecutor). | ||
| # Filter user-role updates: orchestrations (e.g. HandoffBuilder) may emit the | ||
| # full conversation including user messages, which should not be echoed back. | ||
| if data.role == "user": | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. New early-return for
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Returning early for user-role |
||
| return [] | ||
| if not data.author_name: | ||
| data.author_name = executor_id | ||
| return [data] | ||
| if isinstance(data, AgentResponse): | ||
| # Convert each message in AgentResponse to an AgentResponseUpdate | ||
| updates: list[AgentResponseUpdate] = [] | ||
| for msg in data.messages: | ||
| if msg.role == "user": | ||
| continue | ||
alliscode marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| updates.append( | ||
| AgentResponseUpdate( | ||
| contents=list(msg.contents), | ||
|
|
@@ -593,6 +626,8 @@ def _convert_workflow_event_to_agent_response_updates( | |
| ) | ||
| return updates | ||
| if isinstance(data, Message): | ||
| if data.role == "user": | ||
| return [] | ||
| return [ | ||
| AgentResponseUpdate( | ||
| contents=list(data.contents), | ||
|
|
@@ -609,6 +644,8 @@ def _convert_workflow_event_to_agent_response_updates( | |
| chat_messages = cast(list[Message], data) | ||
| updates = [] | ||
| for msg in chat_messages: | ||
| if msg.role == "user": | ||
| continue | ||
| updates.append( | ||
| AgentResponseUpdate( | ||
| contents=list(msg.contents), | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.