Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ async def _select_speaker(self, roles: str, participants: List[str], max_attempt
while num_attempts < max_attempts:
num_attempts += 1
if self._model_client_streaming:
chunk: CreateResult | str = ""
chunk: Optional[CreateResult | str] = None
async for _chunk in self._model_client.create_stream(messages=select_speaker_messages):
chunk = _chunk
if self._emit_team_events:
Expand All @@ -262,8 +262,8 @@ async def _select_speaker(self, roles: str, participants: List[str], max_attempt
await self._output_message_queue.put(
SelectorEvent(content=chunk.content, source=self._name)
)
# The last chunk must be CreateResult.
assert isinstance(chunk, CreateResult)
if not isinstance(chunk, CreateResult):
raise RuntimeError("No final model result in streaming mode while selecting the next speaker.")
response = chunk
else:
response = await self._model_client.create(messages=select_speaker_messages)
Expand Down
27 changes: 27 additions & 0 deletions python/packages/autogen-agentchat/tests/test_group_chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -1944,3 +1944,30 @@ async def test_selector_group_chat_streaming(runtime: AgentRuntime | None) -> No

# Content-based verification instead of index-based
# Note: The streaming test verifies the streaming behavior, not the final result content


@pytest.mark.asyncio
async def test_selector_group_chat_streaming_no_final_result(runtime: AgentRuntime | None) -> None:
"""Streaming speaker selection raises a clear error when the stream yields no final CreateResult."""
# Replay payload is unused: create_stream is overridden below to simulate an empty stream.
model_client = ReplayChatCompletionClient(["unused"])

async def mock_create_stream(*args: Any, **kwargs: Any) -> AsyncGenerator[str | CreateResult, None]:
yield "agent"
yield "2"
# No final CreateResult.

model_client.create_stream = mock_create_stream # type: ignore[method-assign]

agent1 = _EchoAgent("agent1", description="echo agent 1")
agent2 = _EchoAgent("agent2", description="echo agent 2")
team = SelectorGroupChat(
participants=[agent1, agent2],
model_client=model_client,
termination_condition=MaxMessageTermination(2),
runtime=runtime,
model_client_streaming=True,
)

with pytest.raises(RuntimeError, match="No final model result"):
await team.run(task="Say hello")