Skip to content

Commit 8f73970

Browse files
committed
fix(flows): stamp model response event when the response arrives
The model response event was created before the model call and kept its creation time as timestamp. When another branch appended events during a slow call, the SQL session services (ordered by timestamp) reloaded the response before them, causing replay divergence on resume. Restamp the event when the response is received. Fixes google#7695
1 parent 2084efb commit 8f73970

2 files changed

Lines changed: 37 additions & 0 deletions

File tree

‎src/google/adk/flows/llm_flows/base_llm_flow.py‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,11 @@ async def _run_one_step_async(
387387
yield cast(Event, llm_response)
388388
continue
389389

390+
# The event was created before the model call; restamp it so its
391+
# timestamp reflects when the response arrived, keeping timestamp
392+
# order consistent with append order.
393+
model_response_event.timestamp = platform_time.get_time()
394+
390395
# Postprocess after calling the LLM.
391396
async with Aclosing(
392397
self._postprocess_async(

‎tests/unittests/flows/llm_flows/test_base_llm_flow.py‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import asyncio
1818
import logging
1919
import ssl
20+
import time
2021
from typing import Optional
2122
from unittest import mock
2223
from unittest.mock import AsyncMock
@@ -3868,3 +3869,34 @@ async def mock_step(_ctx):
38683869

38693870
assert step_invocations == 1
38703871
assert yielded_events == [fc_event]
3872+
3873+
3874+
@pytest.mark.asyncio
3875+
async def test_model_response_event_timestamp_is_taken_when_response_arrives():
3876+
"""The first model-response event is stamped after the model call returns."""
3877+
call_finished_at = None
3878+
3879+
class _SlowLlm(BaseLlm):
3880+
model: str = 'slow'
3881+
3882+
async def generate_content_async(self, llm_request, stream=False):
3883+
nonlocal call_finished_at
3884+
await asyncio.sleep(0.2)
3885+
call_finished_at = time.time()
3886+
yield LlmResponse(
3887+
content=types.Content(
3888+
role='model', parts=[types.Part.from_text(text='hi')]
3889+
)
3890+
)
3891+
3892+
agent = Agent(name='test_agent', model=_SlowLlm())
3893+
invocation_context = await testing_utils.create_invocation_context(
3894+
agent=agent, user_content='test message'
3895+
)
3896+
flow = BaseLlmFlowForTesting()
3897+
3898+
events = [e async for e in flow._run_one_step_async(invocation_context)]
3899+
3900+
assert len(events) == 1
3901+
assert call_finished_at is not None
3902+
assert events[0].timestamp >= call_finished_at

0 commit comments

Comments
 (0)