fix(openai): filter duplicate function_call items when using previous_response_id#5183
Open
FelmonFekadu wants to merge 2 commits intolivekit:mainfrom
Open
fix(openai): filter duplicate function_call items when using previous_response_id#5183FelmonFekadu wants to merge 2 commits intolivekit:mainfrom
FelmonFekadu wants to merge 2 commits intolivekit:mainfrom
Conversation
Author
|
CI note: the 6 test failures are all in |
…_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
ce3ec20 to
46594bc
Compare
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.
Summary
When using the Responses API with
previous_response_id, the deltainput_chat_ctxincludesfunction_callitems 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_callitems from the serialized input whenprevious_response_idis set, keeping onlyfunction_call_outputitems 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 bothfunction_callitems (the LLM's tool calls) andfunction_call_outputitems (tool results). Only the outputs are new — the calls themselves originated from the previous response referenced byprevious_response_id.Fix: In
LLMStream._run_impl(), filter the serializedchat_ctxto excludefunction_calltype items whenprevious_response_idis present inextra_kwargs.Test plan
test_responses_chat_ctx_excludes_function_calls_with_previous_response_idto verify function_call items are filtered while function_call_output items are preservedtest_chat_ctx.pytests pass (18 passed, 1 skipped)ruff checkandruff formatpassFixes #5136