Skip to content

Fix: Preserve OpenAI item ID on FunctionCall in Realtime sessions#4876

Merged
longcw merged 2 commits intolivekit:mainfrom
StianHanssen:fix-gpt-realtime-tool-context-sync
Feb 18, 2026
Merged

Fix: Preserve OpenAI item ID on FunctionCall in Realtime sessions#4876
longcw merged 2 commits intolivekit:mainfrom
StianHanssen:fix-gpt-realtime-tool-context-sync

Conversation

@StianHanssen
Copy link
Contributor

@StianHanssen StianHanssen commented Feb 17, 2026

Problem

In _handle_response_output_item_done, the FunctionCall emitted through function_ch does not preserve the OpenAI-assigned item ID. It creates a new local UUID instead:

self._current_generation.function_ch.send_nowait(
    llm.FunctionCall(
        call_id=item.call_id,
        name=item.name,
        arguments=item.arguments,
    )
)

This means agent._chat_ctx and _remote_chat_ctx end up with different IDs for the same function_call item. Every other item type (assistant messages, user messages) correctly preserves the OpenAI ID, this is the only one that doesn't.

This causes update_chat_ctx(agent.chat_ctx) to produce delete+recreate diffs for function_call items, since compute_chat_ctx_diff matches by ID. In our case, this leads to previous_item_id not found errors when update_chat_ctx is called during active tool execution even if you are supposed to only change items furthest back in the context (we call it as part of a context summarization strategy).

Fix

Pass id=item_id to the FunctionCall constructor.

@CLAassistant
Copy link

CLAassistant commented Feb 17, 2026

CLA assistant check
All committers have signed the CLA.

Copy link
Contributor

@devin-ai-integration devin-ai-integration bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Devin Review found 1 potential issue.

🐛 1 issue in files not directly in the diff

🐛 Same missing id=item_id bug exists in realtime_model_beta.py but was not fixed (livekit-plugins/livekit-plugins-openai/livekit/plugins/openai/realtime/realtime_model_beta.py:1412-1416)

The PR fixes the missing id=item_id in realtime_model.py but the identical bug exists in realtime_model_beta.py. The FunctionCall constructor at this location does not pass id=item_id, so it generates a new local UUID instead of preserving the OpenAI-assigned item ID.

Root Cause

realtime_model_beta.py:1412 creates a FunctionCall without passing the id parameter, just like the original bug in realtime_model.py that this PR fixes. The item_id is already extracted at realtime_model_beta.py:1402 via assert (item_id := event.item.id) is not None but is never passed to the FunctionCall constructor.

This causes the same ID mismatch between agent._chat_ctx and _remote_chat_ctx described in the PR description, leading to previous_item_id not found errors when update_chat_ctx is called during active tool execution.

Impact: The same delete+recreate diffs and previous_item_id not found errors will occur for users of the beta realtime model.

View 2 additional findings in Devin Review.

Open in Devin Review

@longcw longcw merged commit a39f215 into livekit:main Feb 18, 2026
9 checks passed
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.

3 participants

Comments