Repository navigation
[typescript/openai-agents] give model calls a 10-second default heartbeat timeout - #514
Conversation
…at timeout The model activities' default options set no heartbeatTimeout, and they heartbeat only when one is set. So a cancel never reached a running model call, and a lost worker went unnoticed until the 60-second start-to-close timeout. Default it to 10 seconds. The model activities already heartbeat on a timer at half the timeout. A timeout set in the plugin's modelParams or the runner's defaultModelParams wins, and 0 turns heartbeats off. Local activities still don't get one. The timer now sends its first heartbeat half the timeout in, not at once, so a call that ends sooner sends none. The stateful MCP session sends its own heartbeat when it starts, so it still heartbeats at once. Part of #491.
xumaple
left a comment
There was a problem hiding this comment.
I'm not convinced that we need to do some extra timestamp math to avoid the case where codec takes up more than half of the heartbeat time. that seems like something the user should choose to configure.
In general, in Temporal, we should just go with the default timeout calculations because that's what we do everywhere else.
The first heartbeat now goes out half the timeout after the Activity starts, like the other plugins' timers. A slow interceptor or codec is left to heartbeatTimeout, so the codec notes and the tests for that case go too.
|
Agreed, and done. The timer now heartbeats every half timeout from when the Activity starts, like the Vercel AI SDK, Google ADK and Strands plugins, with no scheduled-time math. A slow interceptor or codec is left to Could you take another look? |
xumaple
left a comment
There was a problem hiding this comment.
lgtm, thanks for addressing
What was changed
The model Activities (
invokeModelActivityandinvokeModelStreamActivity) now get a 10-secondheartbeatTimeoutby default. It sits inDEFAULT_MODEL_ACTIVITY_OPTIONS, next to the 60-secondstartToCloseTimeout.startAdaptiveHeartbeat) at half the timeout.heartbeatTimeoutyou set wins, in the plugin'smodelParamsor in the runner'sdefaultModelParams.heartbeatTimeout: 0turns heartbeats off.ModelActivityOptionsnow documentsheartbeatTimeout: 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 themodelParamsexample.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
heartbeatTimeoutof more than twice that time, or0. 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 setheartbeatTimeoutnow 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, raiseheartbeatTimeout.Checklist
Closes [Feature Request] Give OpenAI Agents TypeScript model calls a 10-second default heartbeat timeout #513
How was this tested:
agent.asTool() runs take the run model, model settings and runner defaults(fourinvokeModelActivitycalls, with a runnerdefaultModelParamsthat doesn't set one) andStreaming: external subscriber and in-workflow result see the same events(invokeModelStreamActivity, with a clientmodelParamsthat doesn't set one). Without the default, they fail with- 0 + 10.layerModelParamstests check the 10-second default, and that a0from the runner or the client wins over it.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.A model call slower than its heartbeat timeout completes on its first attemptruns a 2.5-second call with a 1-second heartbeat timeout, and it finishes on its first attempt.heartbeatTimeoutunset,0,'0s'and'3s', and read History,activityInfo().heartbeatTimeoutMsand, 5.5 seconds in, the pending Activity's last heartbeat: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.Any docs updates needed?
The
heartbeatTimeoutJSDoc and the README'smodelParamssection 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.