Repository navigation
openai_agents: give model calls a 10-second default heartbeat timeout - #496
Merged
Merged
Conversation
ModelActivityParameters.heartbeat_timeout defaulted to None, and the model activities heartbeat only when it's set. So a cancel never reached a running model call, and a lost worker went unnoticed until the start-to-close timeout. Default it to 10 seconds. The model activities already heartbeat on a timer at half the timeout. Setting it to None still turns heartbeats off. The docstring no longer says it detects a stuck model call: the timer keeps beating while a call hangs. Part of #491.
campoy
marked this pull request as ready for review
November 12, 2026 22:34
JasonSteving99
approved these changes
Nov 12, 2026
This was referenced Nov 13, 2026
campoy
added a commit
that referenced
this pull request
Nov 17, 2026
…beat timeout (#514) ## What was changed The model Activities (`invokeModelActivity` and `invokeModelStreamActivity`) now get a 10-second `heartbeatTimeout` by default. It sits in `DEFAULT_MODEL_ACTIVITY_OPTIONS`, next to the 60-second `startToCloseTimeout`. - The Activities already heartbeat on a timer (`startAdaptiveHeartbeat`) at half the timeout. - That timer now sends its first heartbeat half the timeout into the call, instead of right away, like the Vercel AI SDK, Google ADK and Strands plugins. So a call that ends sooner sends none. - A `heartbeatTimeout` you set wins, in the plugin's `modelParams` or in the runner's `defaultModelParams`. `heartbeatTimeout: 0` turns heartbeats off. - `ModelActivityOptions` now documents `heartbeatTimeout`: what the default does, how to turn it off, and how much time the Worker's startup and heartbeat encoding get. The README says the same, next to the `modelParams` example. Local Activities (`useLocalActivity: true`) don't take a heartbeat timeout, and still don't get one. The stateful MCP session uses the same timer. It sends its own heartbeat when it starts, so it still heartbeats right away. Only the timer's first beat, a repeat with the same details, moves later. Its tests pass, including the slow connect and heartbeat timeout ones. ## Why? Without a heartbeat timeout, the model Activities never heartbeat. So a cancel never reaches a running model call, and a lost Worker goes unnoticed until the 60-second start-to-close timeout. #491 has the details, and the measurements behind 10 seconds. #496 makes the same change in Python. Why wait before the first heartbeat? The heartbeat timeout already counts from the start of the attempt, so a heartbeat right away adds nothing. Waiting means a call shorter than 5 seconds sends no heartbeat, and doesn't pay for one. What's the catch? The first heartbeat has to reach the server within the timeout. It goes out half the timeout after the Activity starts, and the Worker encodes it with your payload codecs first. So the Worker's startup work for the attempt (interceptors and payload decoding) plus that encoding must fit in the other half: under 5 seconds with the default. A slower setup, like a codec that calls a remote key service, needs a `heartbeatTimeout` of more than twice that time, or `0`. The docs say so. So does a cancel stop the model call now? Not yet. The cancel reaches the Activity, but the Activity doesn't pass it to the model, so the call still runs to the end. That's #503. A fix for #428 (the TypeScript side of #227, which #228 fixes in Python) needs both. Does this break anything? Not replay: when core replays a scheduled Activity, it only compares its ID and type. I recorded a model call on `main`, with no heartbeat timeout, and it replays fine with this change. But a model call that didn't set `heartbeatTimeout` now heartbeats once it runs past 5 seconds. In Temporal Cloud, that's at most one Action every 8 seconds from then on. A Worker whose event loop is blocked for about 10 seconds now fails the call with a heartbeat timeout, and retries it. If interceptors, payload decoding or a payload codec take a large part of the timeout, raise `heartbeatTimeout`. ## Checklist 1. Closes #513 2. How was this tested: - Two tests now read the model Activities' heartbeat timeout from History, and expect 10 seconds: `agent.asTool() runs take the run model, model settings and runner defaults` (four `invokeModelActivity` calls, with a runner `defaultModelParams` that doesn't set one) and `Streaming: external subscriber and in-workflow result see the same events` (`invokeModelStreamActivity`, with a client `modelParams` that doesn't set one). Without the default, they fail with `- 0 + 10`. - The `layerModelParams` tests check the 10-second default, and that a `0` from the runner or the client wins over it. - The credentials test that checked the timer already runs while the callback waits is now `the Activity heartbeats while the callback is asked for credentials, and a quick call sends none`. With a 200 ms timeout, a quick call sends no heartbeat, and a call whose callback takes 350 ms sends at least two. With the old timer, the quick call fails with `- 1 + 0`. - The new `A model call slower than its heartbeat timeout completes on its first attempt` runs a 2.5-second call with a 1-second heartbeat timeout, and it finishes on its first attempt. - I also ran a 6-second model call with `heartbeatTimeout` unset, `0`, `'0s'` and `'3s'`, and read History, `activityInfo().heartbeatTimeoutMs` and, 5.5 seconds in, the pending Activity's last heartbeat: ``` PROBE modelParams=null history.heartbeatTimeout=10s+0ns activityInfo.heartbeatTimeoutMs=10000 pending.lastHeartbeatTime=set PROBE modelParams={"heartbeatTimeout":0} history.heartbeatTimeout=0s+0ns activityInfo.heartbeatTimeoutMs=0 pending.lastHeartbeatTime=none PROBE modelParams={"heartbeatTimeout":"0s"} history.heartbeatTimeout=0s+0ns activityInfo.heartbeatTimeoutMs=0 pending.lastHeartbeatTime=none PROBE modelParams={"heartbeatTimeout":"3s"} history.heartbeatTimeout=3s+0ns activityInfo.heartbeatTimeoutMs=3000 pending.lastHeartbeatTime=set ``` - With the scheduled-time math gone, in a Docker image with Node 24: `pnpm run lint` (0 errors; the 8 warnings are existing ones), and the credentials and model test files (22 passed). CI runs the full suite. 3. Any docs updates needed? The `heartbeatTimeout` JSDoc and the README's `modelParams` section are updated here, including the time budget for startup and heartbeat encoding. Release notes should mention the new default, how to turn it off, and that slow codecs need a larger timeout.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What was changed
ModelActivityParameters.heartbeat_timeoutnow defaults to 10 seconds instead ofNone, so both model activities (invoke_model_activityandinvoke_model_activity_streaming) heartbeat by default.heartbeat_timeout=Noneturns heartbeats off, as before.heartbeat_timeoutisn't set by default, so the cancel needs you to set it. It now says the default is 10 seconds, and thatNoneturns the cancel off.heartbeat_timeoutdocstring said it detects "a stuck model call". It doesn't: the timer keeps beating while a model call hangs. It now says what the heartbeats do. A comment in_TemporalModelStub.stream_responsemade the same claim, so I fixed it too.Local activities (
use_local_activity=True) don't take a heartbeat timeout, and still don't get one.Why?
Without a heartbeat timeout, the model activities never heartbeat. So a cancel never reaches a running model call, and the model keeps generating until it's done. A lost worker also goes unnoticed until the 60-second start-to-close timeout. Jason pointed out on #228 that this makes the guardrail fix there do nothing with the defaults: the Workflow records the cancel, but the activity never hears it.
Why 10 seconds? #491 has the measurements.
Does this break anything? Not replay: when core replays a scheduled Activity, it only compares its ID and type (#491 has the check), and the replay tests pass. But a model call that didn't set
heartbeat_timeoutnow heartbeats, which in Temporal Cloud is at most one Action every 8 seconds of a running call. And a Worker whose event loop blocks for about 10 seconds now fails the call with a heartbeat timeout, and retries it.Checklist
Closes [Feature Request] openai_agents: give model calls a 10-second default heartbeat timeout #495
How was this tested:
test_heartbeatnow runs three cases and reads the model activity's heartbeat timeout from History:set(0.5 seconds, which passes only if the activity heartbeats, since the model takes a second),default(10 seconds) andoff(None, no heartbeat timeout). Without the change,defaultfails withassert [None] == [datetime.timedelta(seconds=10)].make test(626 passed, 2 skipped),make lintand the conventions check pass, on Python 3.12 with uv 0.12.24.Any docs updates needed?
The docstring and the README are updated here. Release notes should mention the new default, and how to turn it off.