Repository navigation
fix(openai): replay function calls and their outputs on a realtime reconnection - #2634
Merged
Merged
Conversation
🦋 Changeset detectedLatest commit: 68a0ba1 The changes in this PR will be included in the next version bump. This PR includes changesets to release 39 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
davidzhao
approved these changes
Oct 4, 2026
Merged
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.
Ports livekit/agents#7576.
The OpenAI realtime session now replays function calls and outputs after reconnecting, preserves completed function-call arguments in its remote chat-context mirror, and resends chat-context events that the lost connection never confirmed.
No source behavior or required infrastructure was omitted.
Source diff coverage
Source diff coverage
livekit-agents/livekit/agents/llm/_realtime/openai.py: adapted toplugins/openai/src/realtime/realtime_model.ts. Ported function-call/output replay, completed argument mirroring, and unconfirmed chat-context event resend using the target's TypeScriptFutureand websocket event tracking. The target-specific reconnect cleanup was adapted to retain pending item futures so resent confirmations can settle the original waiter.tests/test_realtime/test_openai_realtime_model.py: adapted toplugins/openai/src/realtime/realtime_model.test.ts. Ported both source tests and their mock realtime server using Vitest andws, including target lifecycle cleanup.Testing
pnpm build(40/40 workspace tasks passed)pnpm --filter @livekit/agents-plugin-openai buildpnpm exec vitest run plugins/openai/src/realtime/realtime_model.test.ts(55 passed)pnpm exec eslint -f unix plugins/openai/src/realtime/realtime_model.test.tspnpm exec vitest run plugins/openai(210 passed, 2 skipped; existing STT suite cannot load the LFS-placeholder Silero ONNX file, and the unlocked OpenAI 6.49 dependency makes an existing TTS mock return 404)pnpm lint(blocked by existing repository lint/format errors under the available Node 20/pnpm 9 toolchain; the repository declares pnpm 11, which requires Node >=22.13)cue-clivalidation attempted after a successful build, but the configured LiveKit credentials were rejected with HTTP 401 before the worker could registerChangeset
@livekit/agents-plugin-openaiPorted from livekit/agents#7576
Original PR description
The OpenAI realtime session replays the conversation into the new websocket with
exclude_function_call=True. That flag drops function calls and their outputs, so the new conversation is missing every tool exchange.Two failures follow:
invalid_tool_call_id("Tool call ID '...' not found in conversation."),update_chat_ctxtimes out after 5 s, and the model never gets the result. This is the error in Tools throw error when working with OpenAI Realtime API agents#3344.max_session_duration, 20 min by default) and after every retried connection drop.This PR makes three changes to the reconnection:
exclude_function_call=Truefrom the replay.update_chat_ctxalready sends function calls during a session, and the Azure Voice Live plugin already replays them on reconnection (test_reconnect_replays_tool_calls_and_system_messages).conversation.item.addedcarries a model call witharguments: "", and onlyresponse.output_item.donecarries the generated arguments._handle_response_output_item_donenow copies them into the mirroredFunctionCall, so the replayed call matches the one the tool ran with.conversation.item.addedorconversation.item.deletedarrived was missing from the new conversation, andupdate_chat_ctxtimed out waiting for it. The session now keeps the in-flight chat context events it sent, and the reconnection sends the unconfirmed ones again after the replay. The new connection's confirmation settles the waiter. The Azure Voice Live plugin does the same (test_unconfirmed_items_are_created_again_after_a_reconnection).Against the real OpenAI Realtime API with the first two changes applied to 1.8.2 (
gpt-realtime,max_session_duration=10, a tool that took 15 s):Tool call ID 'call_VMA0sMY2zSuVjwMt' not found in conversation.conversation.item.addedfor the output ofcall_52xmy0WGEOqYIP3Q, after two reconnections while the tool ranupdate_chat_ctxupdate_chat_ctx timed out.A later run on the same setup hit the third case. The output was sent 0.5 s before the reconnection and was not confirmed, the replay carried only the call, and
update_chat_ctxtimed out.Tests:
test_reconnect_replays_function_calls_with_their_outputsfails before the first two changes (only the user message is replayed, and the call has empty arguments without the second change) and passes after them.test_reconnect_creates_an_item_the_lost_connection_never_confirmedcloses the connection when the output arrives, before confirming it. It fails before the third change withupdate_chat_ctx timed out.and passes after it.