Skip to content

fix(openai): filter duplicate function_call items when using previous_response_id#5183

Open
FelmonFekadu wants to merge 2 commits intolivekit:mainfrom
FelmonFekadu:fix/duplicate-function-call-responses-api
Open

fix(openai): filter duplicate function_call items when using previous_response_id#5183
FelmonFekadu wants to merge 2 commits intolivekit:mainfrom
FelmonFekadu:fix/duplicate-function-call-responses-api

Conversation

@FelmonFekadu
Copy link

Summary

When using the Responses API with previous_response_id, the delta input_chat_ctx includes function_call items from the previous LLM response. The server already knows about these from the referenced response, so sending them again causes each tool call to appear twice in the API logs.

This filters out function_call items from the serialized input when previous_response_id is set, keeping only function_call_output items which are genuinely new.

Root cause: In LLM.chat() (line 304-317), when the chat context prefix matches the previous context, a delta is computed containing items added since the last response. This delta includes both function_call items (the LLM's tool calls) and function_call_output items (tool results). Only the outputs are new — the calls themselves originated from the previous response referenced by previous_response_id.

Fix: In LLMStream._run_impl(), filter the serialized chat_ctx to exclude function_call type items when previous_response_id is present in extra_kwargs.

Test plan

  • Added test_responses_chat_ctx_excludes_function_calls_with_previous_response_id to verify function_call items are filtered while function_call_output items are preserved
  • All existing test_chat_ctx.py tests pass (18 passed, 1 skipped)
  • ruff check and ruff format pass

Fixes #5136

@CLAassistant
Copy link

CLAassistant commented Mar 20, 2026

CLA assistant check
All committers have signed the CLA.

@FelmonFekadu
Copy link
Author

CI note: the 6 test failures are all in test_recording.py — a pre-existing issue with _upload_session_report() and _setup_cloud_tracer() getting an unexpected cloud_hostname kwarg. Unrelated to this change. The other 396 tests (including the new one) pass.

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: No Issues Found

Devin Review analyzed this PR and found no potential bugs to report.

View in Devin Review to see 2 additional findings.

Open in Devin Review

…_response_id

When using the Responses API with previous_response_id, the delta
input_chat_ctx includes function_call items from the previous LLM
response. The server already knows about these from the referenced
response, so sending them again causes each tool call to appear
twice in the API logs.

Filter out function_call items from the serialized input when
previous_response_id is set, keeping only function_call_output
items which are genuinely new.

Fixes livekit#5136
@FelmonFekadu FelmonFekadu force-pushed the fix/duplicate-function-call-responses-api branch from ce3ec20 to 46594bc Compare March 20, 2026 21:20
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.

Duplicated function_call entries when using openai responses api

2 participants