From 15560d61c68d1ee08d839b6621df1619222dddf7 Mon Sep 17 00:00:00 2001 From: Akshay Shah Date: Sun, 15 Nov 2026 11:13:05 -0600 Subject: [PATCH] fix(live): give each support_cfc model call a fresh request queue call_llm_async closes the LiveRequestQueue on turn_complete, then reused that closed queue for the next CFC model call. A tool that finished after turn_complete had its response sent into the closed queue, so the next connection received it again as a live tool response, on top of the copy already in its history. The reused queue also reported closed=True, which run_live treats as a client close and so would not reconnect. Create a new queue when the existing one is closed. --- .../adk/flows/llm_flows/core/_model_call.py | 7 +- .../flows/llm_flows/core/test_model_call.py | 64 +++++++++++++++++++ 2 files changed, 70 insertions(+), 1 deletion(-) diff --git a/src/google/adk/flows/llm_flows/core/_model_call.py b/src/google/adk/flows/llm_flows/core/_model_call.py index 896f89bffe4..ad45ce5b135 100644 --- a/src/google/adk/flows/llm_flows/core/_model_call.py +++ b/src/google/adk/flows/llm_flows/core/_model_call.py @@ -235,7 +235,12 @@ async def _call_llm_with_tracing() -> AsyncGenerator[LlmResponse, None]: invocation_context.increment_llm_call_count() if run_config.support_cfc: - if invocation_context.live_request_queue is None: + # Each CFC call opens a new connection, and the previous call's queue + # was closed on turn_complete. Anything sent to it after that (a tool + # that finished late) is already in the session history the new + # connection gets, so start from a fresh queue. + queue = invocation_context.live_request_queue + if queue is None or queue.closed: invocation_context.live_request_queue = LiveRequestQueue() async with Aclosing( flow._run_and_handle_error( diff --git a/tests/unittests/flows/llm_flows/core/test_model_call.py b/tests/unittests/flows/llm_flows/core/test_model_call.py index 8b0d4fe3f31..b5018e23214 100644 --- a/tests/unittests/flows/llm_flows/core/test_model_call.py +++ b/tests/unittests/flows/llm_flows/core/test_model_call.py @@ -340,6 +340,70 @@ async def test_cfc_llm_calls_are_counted_against_max_llm_calls(): await _drive_one_llm_call(flow, invocation_context) +class _LateFunctionResponseCfcFlow(BaseLlmFlow): + """Sends a function response after the turn completes, as a slow tool does. + + Records, for each call, whether the queue it was handed was already closed + and which requests were waiting in it. + """ + + def __init__(self): + super().__init__() + self.queues_seen: list[tuple[bool, list[Any]]] = [] + + async def run_live(self, invocation_context): + queue = invocation_context.live_request_queue + pending = [] + while not queue._queue.empty(): + pending.append(queue._queue.get_nowait()) + self.queues_seen.append((queue.closed, pending)) + yield Event( + author='root_agent', + content=types.Content( + role='model', parts=[types.Part.from_text(text='done')] + ), + turn_complete=True, + ) + # The run_live merge loop forwards a tool's response to the model when the + # tool finishes, which can be after the turn_complete event was handled. + queue.send_content( + types.Content( + role='user', + parts=[ + types.Part( + function_response=types.FunctionResponse( + id='fc1', name='lookup', response={'answer': 42} + ) + ) + ], + ) + ) + + +@pytest.mark.asyncio +async def test_cfc_call_does_not_reuse_the_previous_calls_closed_queue(): + """Each CFC model call opens a new connection with an open, empty queue. + + The previous call's queue was closed on turn_complete. Anything sent to it + afterwards, such as a late function response, is already in the session + history the next connection is given, so it must not be delivered again. + """ + agent = Agent( + name='root_agent', model=testing_utils.MockModel.create(responses=[]) + ) + flow = _LateFunctionResponseCfcFlow() + invocation_context = await testing_utils.create_invocation_context( + agent=agent, + user_content='test', + run_config=RunConfig(support_cfc=True), + ) + + await _drive_one_llm_call(flow, invocation_context) + await _drive_one_llm_call(flow, invocation_context) + + assert flow.queues_seen == [(False, []), (False, [])] + + # --- Tests for tracing a streamed call ---