Duplicated function_call entries when using openai responses api
Bug Description
When using the OpenAI Responses API, function_call items from a previous response are duplicated in subsequent requests.
This results in the same tool call appearing multiple times in the OpenAI logs for a single turn.
Expected Behavior
- Each tool call should appear only once in OpenAI logs
Reproduction Steps
1. Configure the agent LLM to use openai response api:
llm = openai.responses.LLM(
model="gpt-5.4",
temperature=0.4,
)
2. Ask the agent anything that triggers a tool call.
3. Inspect the OpenAI logs for the request.
Operating System
macOS Tahoe
Models Used
GPT 5.4
Package Versions
livekit-agents[openai]==1.4.6
Session/Room/Call IDs
No response
Proposed Solution
Filter out previously emitted function_call items when previous_response_id is present:
async def _run_impl(self) -> None:
self._response_completed = False
chat_ctx, _ = self._chat_ctx.to_provider_format(format="openai.responses")
if "previous_response_id" in self._extra_kwargs and self._llm._pending_tool_calls:
chat_ctx = [
item for item in chat_ctx
if not (
isinstance(item, dict)
and item.get("type") == "function_call"
and item.get("call_id") in self._llm._pending_tool_calls
)
]
Additional Context
No response
Screenshots and Recordings
No response
Root Cause
In livekit-plugins/livekit-plugins-openai/livekit/plugins/openai/responses/llm.py, the LLM.chat() method (lines 304-318) correctly computes a delta input_chat_ctx containing only new items when previous_response_id is set. However, this delta includes both function_call items (from the previous LLM response, added to the chat context by the agent framework) and function_call_output items (tool results).
When to_responses_chat_ctx() serializes these into the input array, the function_call items are sent to OpenAI again β but OpenAI already knows about them from the previous response (referenced by previous_response_id). This causes each tool call to appear twice.
Proposed Fix
In _run_impl() (line ~380 of responses/llm.py), filter out function_call items from the serialized chat_ctx when previous_response_id is present:
async def _run_impl(self) -> None:
self._response_completed = False
chat_ctx, _ = self._chat_ctx.to_provider_format(format="openai.responses")
# When using previous_response_id, the server already has function_call
# items from the previous response β don't send them again.
if "previous_response_id" in self._extra_kwargs:
chat_ctx = [
item for item in chat_ctx
if not (isinstance(item, dict) and item.get("type") == "function_call")
]
This is safe because when previous_response_id is used, the delta context only ever contains function_call items that originated from the previous response β they are inherently duplicates.
Happy to submit a PR if this approach looks good.
Hi, thanks for creating this report! I am unable to reproduce this error; when logging the input items sent to OpenAI, there are no repeats of function_call entries. I also don't think filtering out function_call items entirely would be the right approach, otherwise function_call items would never be sent (and they are an accepted item).
Could you provide more steps to reproduce the double entry and some logs?
When using previous_response_id, the server already has function_call items from the previous response β don't send them again.
@tinalenguyen I guess this is the reason there are multiple tool call logs in OpenAI logs? maybe tool call item is not needed to be sent back in response api usage?
@longcw yes, that's exactly the cause. When previous_response_id is set, the delta context computed in LLM.chat() (lines 304-318) includes both function_call items (already known to OpenAI from the previous response) and function_call_output items (the new tool results). The function_call items get re-serialized into the input array and sent again, creating duplicates in OpenAI's logs.
The simplest fix is to filter function_call items from the chat_ctx in _run_impl() when previous_response_id is in the kwargs β happy to submit a PR if this approach looks good.
Hi, I'd like to work on this. I'll submit a PR shortly.
I measured what this does to the conversation, on livekit-agents 1.8.8 / livekit-plugins-openai 1.8.8 (gpt-6.1-sol, Responses API over websocket, default store). The setup is a refund tool that runs once, with no interruption.
- Chaining on (default): 4 of 6 replies told the user about a second refund, for example "I'm sorryβI accidentally submitted the request twice, so an additional $40 refund may also be processed."
store=False: 0 of 6.- The tool's backend recorded one refund in all 12 runs.
In 40 more runs (30 with an interruption), 17 replies mentioned a duplicate with chaining on, and 0 of 40 with store=False.
The wire capture of the follow-up request matches the cause described above. previous_response_id is set, and input holds both items for the same call:
function_call call_tORP6LpUIY3tguyRtBm6aL5c
function_call_output call_tORP6LpUIY3tguyRtBm6aL5c
Repro: https://github.com/chrislazar25/stopbench, livekit_run.py --llm openai --moments none --commit-delay-ms 400, with and without --no-chain. Happy to run it against #7762.