Skip to content

fix(llm): handle ConnectionClosedOK exception in _receive_from_model test #1752

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
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
2 changes: 1 addition & 1 deletion src/google/adk/flows/llm_flows/base_llm_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ async def _receive_from_model(
) -> AsyncGenerator[Event, None]:
"""Receive data from model and process events using BaseLlmConnection."""

def get_author_for_event(llm_response):
def get_author_for_event(llm_response: LlmResponse) -> str:
"""Get the author of the event.

When the model returns transcription, the author is "user". Otherwise, the
Expand Down
44 changes: 44 additions & 0 deletions tests/unittests/flows/llm_flows/test_base_llm_flow.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import pytest
from unittest.mock import MagicMock
from google.adk.flows.llm_flows.base_llm_flow import BaseLlmFlow, LlmResponse, Event, ConnectionClosedOK

@pytest.mark.asyncio
async def test_receive_from_model_yields_events():
flow = BaseLlmFlow()

fake_response_1 = LlmResponse(
content=MagicMock(role='assistant'),
error_code=None,
interrupted=False
)
fake_response_2 = LlmResponse(
content=MagicMock(role='user'),
error_code=None,
interrupted=False
)

async def fake_receive():
yield fake_response_1
yield fake_response_2
raise ConnectionClosedOK(rcvd=None, sent=None)


llm_connection = MagicMock()
llm_connection.receive = fake_receive

invocation_context = MagicMock()
invocation_context.agent.name = "TestAgent"
invocation_context.live_request_queue = MagicMock()
invocation_context.transcription_cache = []
invocation_context.invocation_id = "test_invocation_id_123"

events = []
async for event in flow._receive_from_model(
llm_connection, event_id="test_event", invocation_context=invocation_context, llm_request=MagicMock()
):
events.append(event)

# Add your assertions here, e.g.:
assert len(events) == 2
assert events[0].author == "TestAgent"
assert events[1].author == "user"