Skip to content

fix: use active agent name for streaming message sender after handoff - #105

Open
Osamaali313 wants to merge 1 commit into
openai:mainfrom
Osamaali313:fix/streaming-sender-after-handoff
Open

fix: use active agent name for streaming message sender after handoff#105
Osamaali313 wants to merge 1 commit into
openai:mainfrom
Osamaali313:fix/streaming-sender-after-handoff

Conversation

@Osamaali313

Copy link
Copy Markdown

Problem

In Swarm.run_and_stream (swarm/core.py), the per-turn assistant message dict is initialized with the wrong sender:

active_agent = agent
...
while len(history) - init_len < max_turns:
    message = {
        "content": "",
        "sender": agent.name,       # <-- starting agent, not the active one
        "role": "assistant",
        ...
    }

agent is the original starting agent, bound once at entry. During the loop, handoffs reassign active_agent (active_agent = partial_response.agent) but never agent. So every assistant message stored in streaming mode after a handoff is stamped with the starting agent's name.

This is a wrong-variable bug — the same method already uses the correct variable everywhere else:

  • The streamed delta: delta["sender"] = active_agent.name
  • The non-streaming twin run: message.sender = active_agent.name

Only the streaming message dict uses agent.name.

Impact

Consumers reading Response.messages[*]["sender"] in streaming mode get the wrong agent for any assistant turn after a handoff — including the shipped repl.pretty_print_messages, which prints message["sender"]. Within a single response the streamed delta says one agent while the stored message says another.

Reproduction

Driving run(..., stream=True) through a handoff (agent1 → agent2):

  • Streamed delta senders: ['Agent 1', 'Agent 2']
  • Before: stored assistant messages: ['Agent 1', 'Agent 1'] — the agent2 turn is mislabeled
  • After: stored assistant messages: ['Agent 1', 'Agent 2'] — matches the deltas

Fix

"sender": active_agent.name,

Test

Added test_streaming_message_sender_after_handoff to tests/test_core.py (there was no streaming test). It fails before the fix (stored sender 'Agent 1') and passes after; the full suite is green (7 passed).

Note: I see Swarm is in maintenance mode / superseded by the OpenAI Agents SDK, so feel free to close if out of scope — sharing it as a small, self-contained correctness fix in case it's useful.

In run_and_stream, the per-turn assistant message dict was initialized
with "sender": agent.name -- the original starting agent, bound once at
entry. Handoffs reassign active_agent but never agent, so every assistant
message stored in streaming mode after a handoff is stamped with the
starting agent's name. This contradicts the streamed delta on the next
line (delta["sender"] = active_agent.name) and the non-streaming run()
(message.sender = active_agent.name). Use active_agent.name. Add a
streaming-handoff regression test.
Copilot AI review requested due to automatic review settings July 10, 2026 20:50

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants