-
Notifications
You must be signed in to change notification settings - Fork 311
fix(openai): drop orphaned realtime tool outputs #1843
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
mykmelez
wants to merge
1
commit into
livekit:main
Choose a base branch
from
mykmelez:fix/openai-reconnect-tool-call-anchors
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| --- | ||
| '@livekit/agents': patch | ||
| '@livekit/agents-plugin-openai': patch | ||
| --- | ||
|
|
||
| Discard stale OpenAI Realtime tool outputs after reconnect when their original function-call anchor is no longer present in the active realtime session. |
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
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
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
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🚩 Orphaned function calls in agent/session chat context after stale output discard
When tools execute in the realtime path,
onToolExecutionStartedatagents/src/voice/agent_activity.ts:3537-3540eagerly adds FunctionCall items to bothagent._chatCtxandagentSession._chatCtx(via_toolItemsAdded). If a reconnect subsequently makes those tool outputs stale, the new filter at line 3667-3694 correctly prevents the stale FunctionCallOutput items from being sent to the realtime session and from being added toagentSession._chatCtx. However, the corresponding FunctionCall entries remain in bothagent._chatCtxandagentSession._chatCtxwithout matching outputs — creating orphaned function call items in the local chat histories. This is unlikely to cause immediate failures since these contexts are rebuilt on the next reconnect (function calls are excluded viachatCtxForReconnect), but it could affect downstream consumers ofagentSession.historythat expect function calls to always have corresponding outputs.Was this helpful? React with 👍 or 👎 to provide feedback.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch, but I think we should leave local history cleanup out of this PR.
The function call did occur and may already have been emitted to observers, while the completed output is intentionally discarded because the reconnected realtime session no longer has the matching call anchor. Removing the local FunctionCall would be a broader history-mutation behavior change, and adding the stale output locally would risk reintroducing confusing context for future syncs.
I also checked LiveKit's Python SDK for parity: its OpenAI realtime reconnect path excludes function calls/outputs from the rebuilt provider context, while the voice layer records tool items locally rather than removing the original function call on reconnect. This PR keeps the fix focused on preventing stale outputs from being sent to a provider session that cannot know about the original call.