Skip to content

Breaking: give google-genai calls a 10-second default heartbeat timeout - #551

Merged
campoy merged 8 commits into
mainfrom
feat/google-genai-default-heartbeat-timeout
Nov 16, 2026
Merged

campoy merged 8 commits into
mainfrom
feat/google-genai-default-heartbeat-timeout

Conversation

@campoy

@campoy campoy commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

What was changed

All 16 Gemini call activities (model calls, files, file search uploads, interactions and managed agents) now heartbeat on a timer, and TemporalAsyncClient gives them a 10-second heartbeat_timeout by default.

  • The timer beats at half the heartbeat timeout, and the first beat goes out half the timeout into the call. So with the default, a call shorter than 5 seconds sends none. Without a heartbeat timeout, the timer does nothing.
  • Temporal caps heartbeat_timeout at the activity's start_to_close_timeout, and the timer uses the capped value. So a shorter timeout, including a per-model-call http_options.timeout or the Interactions and Agents timeout argument, makes beats come sooner. With http_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 pass http_options.timeout to the HTTP client instead.
  • The timer uses loop.call_later instead 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 first await, so anything that blocks the event loop before then pushes every beat back.
  • The interaction streams used to heartbeat once per event. They now use the same timer, so a model that's quiet for a while before its first event doesn't time out. With heartbeat_timeout=None or timedelta(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.
  • The 10-second default is added to every config that leaves heartbeat_timeout out, including your own activity_config. A timeout you set wins, and heartbeat_timeout=None (or timedelta(0)) turns heartbeats off.
  • The README and the activity_config docstring say what the default does, what it costs, how to turn it off, and how to upgrade and roll back.

activity_as_tool runs 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, interactions and agents don't. They get their token through a sync provider that calls credentials.refresh() on the event loop. That happens when the worker's genai.Client has 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:

Call heartbeat_timeout Result
agents.list() default fails with activity Heartbeat timeout
interactions.create() default fails with activity Heartbeat timeout
agents.list() None succeeds
models.generate_content() default succeeds, and heartbeats

With a 4-second refresh, both agents.list() and interactions.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, or None. 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_input replays a History recorded with no heartbeat timeout, and it passes. But a passing replay doesn't mean mixed versions work.

temporalio-google-genai 1.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:

  • To upgrade to 1.0.0, put this version on every worker that runs the Gemini activities before any workflow worker. If the same workers run both, deploy with heartbeat_timeout=None in activity_config, and remove that setting in a later deploy.
  • To roll back from 1.0.0, first deploy this version with heartbeat_timeout=None, wait until the calls scheduled before that deploy finish, and then roll back.
  • Runs pinned with Worker Versioning avoid both cases when their Gemini activities use the workflow's task queue.

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_timeout now 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 in interactions and agents.

Checklist

  1. Closes [Feature Request] Give google-genai calls a 10-second default heartbeat timeout #550

  2. How was this tested:

    • test_full_integration_with_mock_client runs the real activities in a workflow with four activity_configs, and reads the heartbeat timeout of all 10 scheduled activities from History: no config and a config without heartbeat_timeout get 10 seconds, None gets none. In heartbeat-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 with assert [None, None, ...ne, None, ...] == [datetime.tim...onds=10), ...] and heartbeat-1s fails with TimeoutError: activity Heartbeat timeout.
    • test_activity_heartbeats_on_a_timer runs each of the 16 activities three ways. The activity helper also checks that the test cases cover every activity registered by GeminiApiCaller.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 with assert False == True. Without the timer's early return for a missing timeout, all 16 off cases fail with AttributeError: 'NoneType' object has no attribute 'total_seconds'.
    • test_workflow_cancellation_stops_gemini_call cancels 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_event drains a 4-event interaction stream and expects no heartbeats. Without the change it fails with assert [(), (), (), ()] == [].
    • The Vertex refresh table and the 2-second timeout case above come from a separate script on a local dev server: a real genai.Client(vertexai=True) whose credentials sleep in refresh(), 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.
    • Locally, 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.
  3. 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.

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.
@campoy campoy changed the title Give google-genai calls a 10-second default heartbeat timeout Breaking: give google-genai calls a 10-second default heartbeat timeout Nov 14, 2026
Same defaults, costs, limits and upgrade steps in 33 lines instead of 49.

@DABH DABH left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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!

Comment thread python/google_genai/README.md Outdated
Comment on lines +283 to +288
- 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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Documented that disabling heartbeats also disables cancellation for running interaction streams.

Comment thread python/google_genai/README.md Outdated
Comment on lines +273 to +274
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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Clarified that other calls sharing the loop can fail too. Can we leave the warning for a follow-up PR?

Comment thread python/google_genai/README.md Outdated
Comment on lines +270 to +271
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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed the wording to distinguish model and Interactions/Agents timeouts from upload HTTP timeouts.

Comment on lines +1598 to +1599
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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All 10 mocks now sleep past the heartbeat timeout. Added cancellation tests for a model call and a quiet interaction stream too.

campoy added a commit that referenced this pull request Nov 16, 2026
…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 DABH left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines +71 to +72
if "heartbeat_timeout" not in result:
result["heartbeat_timeout"] = timedelta(seconds=10)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread python/google_genai/README.md Outdated
Comment on lines +275 to +276
`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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Listed upload, download, and register explicitly. “File calls” would also include get/list/delete, which use the other path.

Comment on lines +242 to +243
that, or set it longer, if a credential refresh can take longer
than the timeout, since google-genai refreshes on the event

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed the threshold. Shorter blocks can also cause heartbeat timeouts in other calls sharing the loop.

Comment on lines +279 to +282
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`.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I’ll track this upstream in a follow-up issue and link it here. I’d keep the fix out of this PR.

Comment thread python/google_genai/README.md Outdated
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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@campoy campoy Nov 16, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated the README and docstring to name 1.0.0 as the release that adds timer heartbeats and the 10-second default.

Comment on lines +2483 to +2487
_ALL_ACTIVITIES: list[tuple[str, Any]] = [
("gemini_api_client_async_request", _API_REQ),
("gemini_api_client_async_request_streamed", _API_REQ),
*_DIRECT_CALL_ACTIVITIES,
]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could this assert the names match GeminiApiCaller.activities()? Then a new activity added without @_heartbeating fails here instead of timing out in production.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added an assertion in the existing helper that the heartbeat test cases cover every registered activity.

@campoy

campoy commented Nov 16, 2026

Copy link
Copy Markdown
Contributor Author

Filed #573 to track the delayed-heartbeat warning.

@campoy
campoy merged commit 3d3bb7e into main Nov 16, 2026
29 checks passed
@campoy
campoy deleted the feat/google-genai-default-heartbeat-timeout branch November 16, 2026 05:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature Request] Give google-genai calls a 10-second default heartbeat timeout

2 participants