Skip to content

[python/google_adk] breaking: give model calls a 10-second default heartbeat timeout - #520

Merged
campoy merged 3 commits into
mainfrom
feat/google-adk-python-default-heartbeat-timeout
Nov 17, 2026
Merged

campoy merged 3 commits into
mainfrom
feat/google-adk-python-default-heartbeat-timeout

Conversation

@campoy

@campoy campoy commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

What was changed

TemporalModel now gives both model Activities, invoke_model and invoke_model_streaming, a 10-second heartbeat_timeout when activity_config doesn't set one.

  • A timeout you set wins, and heartbeat_timeout=None turns heartbeats off.
  • Both Activities heartbeat on a timer at half the timeout. Before, invoke_model never heartbeat, and invoke_model_streaming heartbeat once per streamed response. The timer waits before its first beat, so a call shorter than 5 seconds sends none.
  • The timer uses loop.call_later, not a task like temporalio-mcp does. Why? A task only starts its sleep at the block's first await, so anything that blocks the event loop before then pushes every beat back. Building the model runs before that await, and so does LiteLlm's import of litellm on its first call, which took 3.4 seconds when I measured it. With a task, a 6-second block would miss the first heartbeat.
  • The TemporalModel docstring and the README say what the default does, what it costs, how to turn it off, and how to upgrade and roll back.

activity_as_tool and activity_node run your own Activities, so they don't change. MCP isn't in this PR either.

Why?

Without a heartbeat timeout, a cancel never reaches a running model call, so it runs to the end. And a lost worker goes unnoticed until the 60-second start-to-close timeout. #491 has the details, and the measurements behind 10 seconds.

Couldn't you set heartbeat_timeout yourself? Well, not really: invoke_model never heartbeat. A 2-second model call with a 1-second heartbeat timeout failed with:

ActivityError: Activity task timed out
TimeoutError: activity Heartbeat timeout

With this change, it returns.

Breaking change

Replay isn't the problem. When core replays a scheduled Activity, it only compares its ID and type. #565 removed the test that replays the four 0.x histories, since 1.0.0 is next, so I ran it once against this branch: all four still replay.

But a passing replay doesn't mean mixed versions work. The timeout is recorded when the model Activity is scheduled. What happens when a Worker on an earlier version runs it? That Worker never heartbeats from invoke_model (and from invoke_model_streaming only when a response arrives), 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.

I checked this on a local dev server, with a 12-second call scheduled the way TemporalModel does it:

Activity body heartbeat_timeout Result
no heartbeat, as before 10 s TimeoutError: activity Heartbeat timeout after 10.02 s
no heartbeat, 2 attempts 10 s both attempts saw 10 s and timed out, 21.04 s in total
this PR's timer 10 s returned after 12.02 s
no heartbeat, as before None returned after 12.03 s

The README has the steps for in-flight runs:

  • To upgrade, put this version on every Worker that runs model 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, first deploy this version with heartbeat_timeout=None, wait until the model calls scheduled before that deploy finish, and then roll back.
  • Runs pinned with Worker Versioning avoid both cases when their model Activities use the Workflow's task queue.

There are two more costs:

  • A call that didn't set heartbeat_timeout now heartbeats. In Temporal Cloud, that's at most one Action every 8 seconds of a running call.
  • A model that blocks the worker's event loop for longer than 10 seconds now fails with a heartbeat timeout. I read the model classes ADK 2.12 registers (Gemini, Gemma, Apigee, Claude, OpenAI, LiteLlm and OCI), and none blocks the loop that long. The longest block I found is that first litellm import.

Checklist

  1. Closes [Feature Request] Give Google ADK Python model calls a 10-second default heartbeat timeout #519

  2. How was this tested:

    • test_single_agent now reads the heartbeat timeouts from History: 10 seconds for both model calls, and none for the activity_as_tool call. Without the change it fails with assert [None, None, None] == [datetime.timedelta(seconds=10), None, datetime.timedelta(seconds=10)].
    • In test_multi_agent, the writer agent now sets heartbeat_timeout=None. It gets no heartbeat timeout, and the other two agents get 10 seconds. Without the change it fails with assert [None, None, None] == [datetime.timedelta(seconds=10), datetime.timedelta(seconds=10), None].
    • A new test_model_activities_heartbeat_on_a_timer runs both model Activities with a 1-second heartbeat timeout. A fast call that streams three responses sends no heartbeat, and a slow call, whose model waits for a heartbeat, returns. Without the change, the fast streaming call fails with assert (3 > 0) == False (one heartbeat per response), and both slow calls time out waiting for a heartbeat.
    • The mixed-version table comes from a one-off script against a local dev server (Temporal CLI 1.10.0). It isn't part of the suite.
    • After rebasing on main, make lint and make test (321 passed, 6 skipped) pass locally, on Python 3.12 with uv 0.13.5.
  3. Any docs updates needed?
    The docstring and the README are updated here. The release notes should call this out as breaking, with the upgrade and rollback steps and how to turn heartbeats off.

@campoy
campoy marked this pull request as ready for review November 13, 2026 19:32
@campoy
campoy requested a review from a team as a code owner November 13, 2026 19:32
@campoy campoy changed the title Give Google ADK Python model calls a 10-second default heartbeat timeout Breaking: give Google ADK Python model calls a 10-second default heartbeat timeout Nov 14, 2026
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.
campoy added a commit that referenced this pull request Nov 16, 2026
…ut (#551)

## 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 (#504)
and Google ADK (#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 #550

2. How was this tested:
- `test_full_integration_with_mock_client` runs the real activities in a
workflow with four `activity_config`s, 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.
@campoy campoy changed the title Breaking: give Google ADK Python model calls a 10-second default heartbeat timeout [google-adk-python] breaking: give model calls a 10-second default heartbeat timeout Nov 16, 2026
@campoy campoy changed the title [google-adk-python] breaking: give model calls a 10-second default heartbeat timeout [python/google_adk] breaking: give model calls a 10-second default heartbeat timeout Nov 16, 2026
TemporalModel now gives both model Activities a 10-second heartbeat
timeout unless activity_config sets one, and heartbeat_timeout=None turns
heartbeats off. The Activities heartbeat on a timer at half the timeout
instead of once per streamed response, so a call shorter than 5 seconds
sends none.

Part of #491.
@campoy
campoy force-pushed the feat/google-adk-python-default-heartbeat-timeout branch from f9356bb to 86f9b8a Compare November 17, 2026 01:07

@patbeqo patbeqo 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.

Could these activities use an @auto_heartbeater decorator like the other integrations?

Like the other Python plugins. This is a copy of Strands' decorator,
which uses the same call_later timer as before, so the behavior
doesn't change.
@campoy

campoy commented Nov 17, 2026

Copy link
Copy Markdown
Contributor Author

Yes, good call. Six of the Python plugins use one: Strands, OpenAI Agents, Claude Agent SDK, Codex, Microsoft Agent Framework and Deep Agents. Each plugin keeps its own copy.

I copied Strands' _heartbeat_decorator.py into this plugin and put @auto_heartbeater on invoke_model and invoke_model_streaming. Strands' copy already uses loop.call_later, the same timer this PR had, so the behavior doesn't change.

@campoy
campoy merged commit 84f7e84 into main Nov 17, 2026
16 checks passed
@campoy
campoy deleted the feat/google-adk-python-default-heartbeat-timeout branch November 17, 2026 02:13
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 ADK Python model calls a 10-second default heartbeat timeout

2 participants