Repository navigation
Breaking: give google-genai calls a 10-second default heartbeat timeout - #551
Conversation
All 16 Gemini call activities now heartbeat on a timer at half the heartbeat timeout, starting half the timeout into the call. The interaction streams no longer heartbeat once per event. TemporalAsyncClient now gives them a 10-second heartbeat_timeout, also when activity_config leaves it out. A timeout you set wins, and None turns heartbeats off. The start-to-close default doesn't change. Part of #491.
Workers on an earlier version schedule these activities with no heartbeat timeout and don't heartbeat on a timer, so mixed versions fail calls longer than 10 seconds. The README now says how to upgrade and roll back, as the repository asks for history-changing behavior. The docs also say that timedelta(0) turns heartbeats off, that a cancelled call can send one more heartbeat as it stops, and that a call blocking the event loop longer than the timeout fails. The timer test gets an "off" case: a slow call with no heartbeat timeout sends no heartbeats.
Same defaults, costs, limits and upgrade steps in 33 lines instead of 49.
DABH
left a comment
There was a problem hiding this comment.
This PR looks good to me: the timer matches the Strands one, and the breaking-change notes are thorough. A few questions inline, mostly about the README's upgrade steps and what heartbeat_timeout=None now means for interaction streams. Thanks!
| - To upgrade, deploy this version to the workers that run the Gemini activities | ||
| first. If the same workers run workflows too, deploy it with | ||
| `heartbeat_timeout=None` in `activity_config` and remove that in a later | ||
| deploy. | ||
| - To roll back, first deploy with `heartbeat_timeout=None`, then wait for the | ||
| calls scheduled before that deploy to finish: they keep the 10-second timeout. |
There was a problem hiding this comment.
A config you pass replaces the default, so ActivityConfig(heartbeat_timeout=None) on its own has no start_to_close_timeout. Every Gemini call would then fail with a ValueError when it's scheduled. Should these steps show keeping start_to_close_timeout=timedelta(seconds=60) alongside it?
There was a problem hiding this comment.
Added an example with both start_to_close_timeout and heartbeat_timeout=None.
| @@ -138,7 +176,6 @@ async def _drain_interaction_stream( | |||
| events: list[dict[str, Any]] = [] | |||
| async with stream: | |||
| async for event in stream: | |||
There was a problem hiding this comment.
With heartbeat_timeout=None, the per-event heartbeat used to still carry a cancel to a stream (core sends heartbeats without a timeout, throttled to 30s). Now a cancelled stream runs to the end. Worth a README line, since the upgrade and rollback steps tell people to set None?
There was a problem hiding this comment.
Documented that disabling heartbeats also disables cancellation for running interaction streams.
| The timer runs on the worker's event loop, so a call that blocks the loop for | ||
| longer than the timeout fails that attempt. On Vertex AI, `interactions` and |
There was a problem hiding this comment.
Anything that blocks the loop stops every call's timer on the worker, so a slow Vertex refresh can fail other running Gemini calls too, not only the one refreshing. Should this say so? And could the timer log a warning when it fires late, like Strands does for a slow model factory?
There was a problem hiding this comment.
Clarified that other calls sharing the loop can fail too. Can we leave the warning for a follow-up PR?
| Temporal caps `heartbeat_timeout` at `start_to_close_timeout`, including one set | ||
| per call with `http_options.timeout`, so a shorter one makes beats come sooner. |
There was a problem hiding this comment.
This holds for model calls, but files and file search store uploads pass http_options.timeout to the HTTP client instead of start_to_close_timeout. Should this name model calls, plus the Interactions and Agents timeout argument?
There was a problem hiding this comment.
Fixed the wording to distinguish model and Interactions/Agents timeouts from upload HTTP timeouts.
| activity_config sets one, even to None. "heartbeat-1s" passes only if the | ||
| activities heartbeat: its API calls take 1.5 seconds and it doesn't retry. |
There was a problem hiding this comment.
Only fake_async_request sleeps, so heartbeat-1s checks heartbeats on a real worker for that one activity. Could the other mocks sleep too? And could a case cancel the workflow mid-call and check the activity stops, since that's the main reason for the change?
There was a problem hiding this comment.
All 10 mocks now sleep past the heartbeat timeout. Added cancellation tests for a model call and a quiet interaction stream too.
…beat timeout (#552) ## What was changed The Vercel AI SDK README now says how to upgrade from 1.0.0, and how to roll back, now that the model and embedding Activities get a 10-second heartbeat timeout by default (#510). ## Why? 1.0.0 schedules these Activities with no heartbeat timeout. Its `invokeModel` and `invokeEmbeddingModel` never heartbeat, and its `invokeModelStreaming` heartbeats only when a stream part arrives ([the only `heartbeat()` call in 1.0.0](https://github.com/temporalio/ai-integrations/blob/typescript/vercel-ai-sdk/v1.0.0/typescript/vercel-ai-sdk/src/activities.ts#L285)). But the timeout is recorded when a Workflow schedules the Activity, so whichever Worker runs it has to heartbeat. During a rolling upgrade, a call that a Workflow on the next version schedules and a 1.0.0 Worker runs fails with a heartbeat timeout after 10 seconds, and so does each retry. Does replay break? No: core only compares an Activity's ID and type. But a passing replay doesn't mean mixed versions work. So the README now gives the same steps that #520 and #551 give for Google ADK Python and google-genai: - Upgrade the Workers that run the model Activities first. If the same Workers run Workflows too, deploy with `heartbeatTimeout: 0` and remove it in a later deploy. - Before a rollback, deploy with `heartbeatTimeout: 0`, and wait for the calls scheduled before that deploy to finish. - Runs pinned with Worker Versioning avoid both. ## Checklist 1. No issue: a docs follow-up to #510, part of #491. 2. How was this tested: Docs only. The conventions check and `git diff --check` pass. To check the claims, I read 1.0.0's code: it sets no `heartbeatTimeout` default, and its only `heartbeat()` call is in `invokeModelStreaming`'s stream loop. On `main`, `TemporalProviderOptions.default` takes the `ActivityOptions` the example sets. #520 measured the failure itself on a local dev server: an Activity that doesn't heartbeat failed 10.02 seconds in, and both attempts of a retried one failed the same way. 3. Any docs updates needed? This is the docs update. The 2.0.0 release notes should carry the same upgrade step.
DABH
left a comment
There was a problem hiding this comment.
Thanks for the updates! The README fixes and the new cancellation tests cover everything from my first pass, and a follow-up PR for the late-timer warning works for me. A few new questions inline, mainly about shipping the timer before the default, plus some README details.
| if "heartbeat_timeout" not in result: | ||
| result["heartbeat_timeout"] = timedelta(seconds=10) |
There was a problem hiding this comment.
Strands had its timer before #504 turned on the default, so mixed versions there never hit the heartbeat timeout. Could this PR ship the timer alone (it does nothing without a timeout) and turn on the 10-second default in a later release? Then upgrading through that release skips the two-step deploy.
There was a problem hiding this comment.
I’d keep them together here, with the breaking-change guidance. A timer-only release helps users who upgrade through it, but those who skip it still need those steps. Let’s discuss staged rollouts separately.
| @@ -138,7 +176,6 @@ async def _drain_interaction_stream( | |||
| events: list[dict[str, Any]] = [] | |||
| async with stream: | |||
| async for event in stream: | |||
There was a problem hiding this comment.
With a heartbeat_timeout set, a stream that went quiet used to fail at that timeout and retry. Now the timer keeps a stalled stream alive until the HTTP timeout or start_to_close_timeout. Worth a README line?
There was a problem hiding this comment.
Clarified that heartbeats show the worker is alive, not that the stream is making progress. A quiet stream is still bounded by its HTTP and activity timeouts.
| `timeout` argument, so a shorter one makes beats come sooner. File and file | ||
| search store uploads pass `http_options.timeout` to the HTTP client instead. |
There was a problem hiding this comment.
files.download and files.register_files pass http_options.timeout to the HTTP client too. Should this say file calls and file search store uploads?
There was a problem hiding this comment.
Listed upload, download, and register explicitly. “File calls” would also include get/list/delete, which use the other path.
| that, or set it longer, if a credential refresh can take longer | ||
| than the timeout, since google-genai refreshes on the event |
There was a problem hiding this comment.
Other calls on the loop can fail with a shorter block: beats come every 5 seconds but core sends at most every 8, so about 6 seconds of blocking can miss the 10-second deadline. Should this say a refresh over about half the timeout?
There was a problem hiding this comment.
Removed the threshold. Shorter blocks can also cause heartbeat timeouts in other calls sharing the loop.
| timeouts in every activity sharing it. On Vertex AI, `interactions` and `agents` | ||
| calls refresh credentials on the loop: on the first call, then about once an | ||
| hour. A slow refresh can also fail other running Gemini calls. If that can be | ||
| slow, set a longer `heartbeat_timeout`, or `None`. |
There was a problem hiding this comment.
Is there a google-genai issue for the refresh on the event loop that we could link here? AGENTS.md asks for framework bugs to be fixed upstream, and a link tells users when they can drop the longer timeout.
There was a problem hiding this comment.
I’ll track this upstream in a follow-up issue and link it here. I’d keep the fix out of this PR.
| hour. A slow refresh can also fail other running Gemini calls. If that can be | ||
| slow, set a longer `heartbeat_timeout`, or `None`. | ||
|
|
||
| **Breaking change when you upgrade or roll back:** earlier versions don't |
There was a problem hiding this comment.
Could "earlier versions" name the release that adds the timer? The README outlives this release, so someone upgrading later can't tell which side of the change they're on.
There was a problem hiding this comment.
Updated the README and docstring to name 1.0.0 as the release that adds timer heartbeats and the 10-second default.
| _ALL_ACTIVITIES: list[tuple[str, Any]] = [ | ||
| ("gemini_api_client_async_request", _API_REQ), | ||
| ("gemini_api_client_async_request_streamed", _API_REQ), | ||
| *_DIRECT_CALL_ACTIVITIES, | ||
| ] |
There was a problem hiding this comment.
Could this assert the names match GeminiApiCaller.activities()? Then a new activity added without @_heartbeating fails here instead of timing out in production.
There was a problem hiding this comment.
Added an assertion in the existing helper that the heartbeat test cases cover every registered activity.
|
Filed #573 to track the delayed-heartbeat warning. |
What was changed
All 16 Gemini call activities (model calls, files, file search uploads, interactions and managed agents) now heartbeat on a timer, and
TemporalAsyncClientgives them a 10-secondheartbeat_timeoutby default.heartbeat_timeoutat the activity'sstart_to_close_timeout, and the timer uses the capped value. So a shorter timeout, including a per-model-callhttp_options.timeoutor the Interactions and Agentstimeoutargument, makes beats come sooner. Withhttp_options.timeout=2000, History records a 2-second heartbeat timeout, and a 1.7-second call sends one heartbeat.files.upload,files.download,files.register_files, and file search store uploads passhttp_options.timeoutto the HTTP client instead.loop.call_laterinstead of a task, as Strands (strands_agents: give model and sandbox activities a 10-second default heartbeat timeout #504) and Google ADK ([python/google_adk] breaking: give model calls a 10-second default heartbeat timeout #520) do. A task only starts its sleep at the call's firstawait, so anything that blocks the event loop before then pushes every beat back.heartbeat_timeout=Noneortimedelta(0), streams no longer heartbeat per event and cannot receive cancellation. With heartbeats enabled, a quiet stream stays alive until its HTTP or activity timeout; heartbeats no longer check stream progress.heartbeat_timeoutout, including your ownactivity_config. A timeout you set wins, andheartbeat_timeout=None(ortimedelta(0)) turns heartbeats off.activity_configdocstring say what the default does, what it costs, how to turn it off, and how to upgrade and roll back.activity_as_toolruns your own activities, so it stays as it is. MCP is out of scope for now.Does any of these activities block the event loop, so the timer can't run? Mostly no: google-genai reads upload files with
anyio, and model, files and file search store calls refresh credentials on a thread. But on Vertex AI,interactionsandagentsdon't. They get their token through a sync provider that callscredentials.refresh()on the event loop. That happens when the worker'sgenai.Clienthas no token yet, and again when the token expires, about once an hour.I checked it on a local dev server with a credential refresh that takes 11 seconds and HTTP 200 responses:
heartbeat_timeoutagents.list()activity Heartbeat timeoutinteractions.create()activity Heartbeat timeoutagents.list()Nonemodels.generate_content()With a 4-second refresh, both
agents.list()andinteractions.create()succeed. This is google-genai's behavior, so following AGENTS.md, the README documents it as a limit instead of working around it here: if your credential refresh can be slower than the heartbeat timeout, set a longer one, orNone. A blocked loop also stops the timers of other activities sharing it, so a slow refresh can fail other running Gemini calls even if it takes less than their heartbeat timeout. Moving that refresh off the event loop is a fix for google-genai, outside this PR.Why?
Without timer heartbeats, most of these activities never hear a cancel, so a running call goes on to the end. Earlier interaction streams could receive cancellation through their per-event heartbeats, but only once an event arrived. A lost worker also goes unnoticed until the 60-second start-to-close timeout. And you couldn't set a heartbeat timeout yourself, since the calls didn't heartbeat: most calls longer than it failed. #491 has the details, and the measurements behind 10 seconds.
Breaking change in 1.0.0
Replay still works: core only compares an activity's ID and type, and heartbeats aren't in History.
test_replay_history_recorded_with_text_bytes_in_the_inputreplays a History recorded with no heartbeat timeout, and it passes. But a passing replay doesn't mean mixed versions work.temporalio-google-genai1.0.0 adds timer heartbeats and the 10-second default. The timeout is recorded when the activity is scheduled. If a worker on a version before 1.0.0 runs that activity, it doesn't heartbeat on a timer (the interaction streams only heartbeat per event), so a call longer than 10 seconds fails with a heartbeat timeout. Retries keep the recorded timeout, so they fail the same way, and rolling back doesn't clear it either. #520 measured this on a local dev server: an activity that doesn't heartbeat failed 10.02 seconds in, and both attempts of a retried one failed the same way.The README has the guidance for in-flight runs:
heartbeat_timeout=Noneinactivity_config, and remove that setting in a later deploy.heartbeat_timeout=None, wait until the calls scheduled before that deploy finish, and then roll back.A config you pass replaces the defaults. Keep your activity timeouts when disabling heartbeats; the README now shows
ActivityConfig(start_to_close_timeout=timedelta(seconds=60), heartbeat_timeout=None)for these steps.There are more costs. A call that didn't set
heartbeat_timeoutnow heartbeats, which in Temporal Cloud is at most one Action every 8 seconds of a running call with the default, and more often when a shorter start-to-close timeout caps it. A cancelled call can send one more as it stops, since core sends the heartbeat it was holding back when an activity is cancelled. A call that blocks the worker's event loop for longer than the heartbeat timeout now fails with a heartbeat timeout. On Vertex AI, that includes a slow credential refresh ininteractionsandagents.Checklist
Closes [Feature Request] Give google-genai calls a 10-second default heartbeat timeout #550
How was this tested:
test_full_integration_with_mock_clientruns the real activities in a workflow with fouractivity_configs, and reads the heartbeat timeout of all 10 scheduled activities from History: no config and a config withoutheartbeat_timeoutget 10 seconds,Nonegets none. Inheartbeat-1s, all 10 calls take 1.5 seconds under a 1-second heartbeat timeout with no retries, so it passes only if they heartbeat. Without the change, the default cases fail withassert [None, None, ...ne, None, ...] == [datetime.tim...onds=10), ...]andheartbeat-1sfails withTimeoutError: activity Heartbeat timeout.test_activity_heartbeats_on_a_timerruns each of the 16 activities three ways. The activity helper also checks that the test cases cover every activity registered byGeminiApiCaller.activities(). A call that takes 0.5 seconds under a 0.2-second heartbeat timeout heartbeats; a fast call under the 10-second default sends none; and a 0.1-second call with no heartbeat timeout sends none. Without the change, all 16 slow cases fail withassert False == True. Without the timer's early return for a missing timeout, all 16offcases fail withAttributeError: 'NoneType' object has no attribute 'total_seconds'.test_workflow_cancellation_stops_gemini_callcancels a workflow during a model call and a quiet interaction stream. Both cases wait for the running SDK call to receive cancellation before the worker shuts down.test_interaction_stream_does_not_heartbeat_per_eventdrains a 4-event interaction stream and expects no heartbeats. Without the change it fails withassert [(), (), (), ()] == [].genai.Client(vertexai=True)whose credentials sleep inrefresh(), mocked HTTP responses, and no retries. It reads the heartbeat timeout from History and polls the workflow's pending activities for heartbeats the service received.make test(267 passed, 1 skipped),make lint,make build, the wheel check and the conventions check pass, on Python 3.12 with uv 0.13.5.Any docs updates needed?
The README and the docstring are updated here. Release notes should call this out as breaking, with the upgrade and rollback steps, how to turn heartbeats off, and the Vertex AI credential refresh limit.