Repository navigation
fix(responses/mcp): keep follow-up calls stateless when store=false - #36575
Conversation
Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
🤖 Devin AI EngineerI'll be helping with this pull request! Here's what you should know: ✅ I will automatically:
Note: I can only respond to comments from users who have write access to this repository. ⚙️ Control Options:
|
Greptile SummaryThis PR keeps MCP follow-up calls stateless when Confidence Score: 5/5The PR appears safe to merge, with no outstanding correctness or repository-rule issues in its current changes The stateless MCP paths consistently avoid chaining to an unpersisted response while replaying encrypted reasoning, and the stateful paths preserve their existing behavior. Both previous review threads are resolved, including the test-patching concern that Greptile withdrew after the implementation constraints were clarified
|
| Filename | Overview |
|---|---|
| litellm/responses/main.py | Selects stateless follow-up request behavior when persistence is explicitly disabled |
| litellm/responses/mcp/litellm_proxy_mcp_handler.py | Detects disabled persistence and preserves ordered reasoning items while constructing follow-up input |
| litellm/responses/mcp/mcp_streaming_iterator.py | Applies the same reasoning replay behavior to streaming MCP follow-ups |
| tests/test_litellm/responses/mcp/test_litellm_proxy_mcp_handler.py | Covers stateless response identifiers, encrypted reasoning replay, ordering, and stateful behavior |
| tests/test_litellm/responses/mcp/test_mcp_streaming_iterator.py | Covers streaming reasoning replay and unchanged stateful chaining |
Reviews (4): Last reviewed commit: "Merge remote-tracking branch 'origin/lit..." | Re-trigger Greptile
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
…itellm_mcp_stateless_follow_up_zdr
…_id on stateless follow-ups
|
bugbot run |
…itellm_mcp_stateless_follow_up_zdr
|
bugbot run |
There was a problem hiding this comment.
✅ Bugbot reviewed your changes and found no new issues!
Comment @cursor review or bugbot run to trigger another review on this PR
Reviewed by Cursor Bugbot for commit 4b24e3b. Configure here.
6960a42
into
litellm_internal_staging
TLDR
Problem this solves:
store=false(zero data retention) callersprevious_response_id, which was never persistedreasoning.encrypted_contentHow it solves it:
store=false, the follow-up call keeps the caller's ownprevious_response_id(usually none)storeunset/true) behavior is unchangedUser Flow
Before: a developer on zero data retention calls
/v1/responseswith an MCP tool and the request fails once the tool has run"store": false, a reasoning model,"include": ["reasoning.encrypted_content"], and"tools": [{"type": "mcp", "server_url": "litellm_proxy", "require_approval": "never"}]Previous response with id 'resp_...' not found, so the caller gets HTTP 400 instead of an answer"stream": truethe answer does stream back, but the second model turn is sent without the model's encrypted reasoning from the first turn, so the model reasons again from scratchAfter: the same request completes and returns the tool-informed answer
"store": falseand the same MCP toolresponsecomes back with HTTP 200,status: "completed", and output text that uses the tool result"stream": truethe answer streams back and the second model turn carries the encrypted reasoning from the first turn"store": true(or omitting it) behaves exactly as beforeRelevant issues
Linear ticket
Resolves LIT-5427
Pre-Submission checklist
Please complete all items before asking a LiteLLM maintainer to review your PR
uv run pytest tests/test_litellm/<your_test_file>.py -v. Leave the suites (make test-unit-*,make test-unit) to CI: it finishes in ~15 minutes where a laptop takes an hour or more@greptileaito re-request a review after pushing changes)Delays in PR merge?
If you're seeing a delay in your PR being merged, ping the LiteLLM Team on Slack (#pr-review).
Screenshots / Proof of Fix
Shared setup for both sides: two proxies ran at the same time, one per commit, each from its own checkout and venv with its own Postgres, booted with
python litellm/proxy/proxy_cli.py --config lit5427-qa-config.yaml --port <port> --num_workers 2 --detailed_debug. Before is the merge base b3c867c on port 25797, After is the PR tip 4b24e3b on port 30453. Every model call is real: OpenAIgpt-5.6andgpt-5.5, Anthropicclaude-sonnet-5, and a real stdio MCP server registered asmath_stdio(tests/mcp_tests/mcp_server.py). The verdict for each case is the HTTP status and body the caller gets back; the "outbound follow-up" line under it is the second request the proxy sent to the provider, read from the--detailed_debuglog, and is there only to show why.gpt-5.6returned no reasoning items in most of these runs, so the reasoning replay cases usegpt-5.5, which doesEvery request below uses the same tool list,
TOOLS='[{"type":"mcp","server_url":"litellm_proxy","server_label":"litellm","require_approval":"never"}]', and the same headers,-H "Authorization: Bearer sk-lit5427-qa" -H "Content-Type: application/json". Prompts:P1isUse the available tools to compute 17 + 25, then tell me the result.,P2isUsing the tools, compute 17 + 25 and separately compute 6 * 7 (one tool call each), then report both results in one sentence.,P3isUse the tools to compute 17 + 25 and separately 6 * 7 (one tool call each), then decide whether each result is prime and explain briefly.,P4isFirst use a tool to compute 17 + 25. Only after you have that result, use a tool to multiply that result by 2. Then report both numbers in one sentence.Before (b3c867c)
ZDR, single tool, non-streaming (gpt-5.6, effort low)
curl -sS -w '\nHTTP %{http_code}\n' -X POST http://127.0.0.1:25797/v1/responses -d '{"model":"gpt-5.6","input":"'"$P1"'","store":false,"reasoning":{"effort":"low"},"include":["reasoning.encrypted_content"],"tools":'"$TOOLS"'}'HTTP 400with{"error":{"message":"litellm.BadRequestError: OpenAIException - {\"error\": {\"message\": \"Previous response with id 'resp_07cbb0153c6939bf016a9b5cbfc1c087d0896af95efc90612c' not found.\", \"type\": \"invalid_request_error\", \"param\": \"previous_response_id\", \"code\": \"previous_response_not_found\"}}. Received Model Group=gpt-5.6", "code":"400"}}store=False previous_response_id=resp_07cbb0153c69... input=[message, function_call call_34TyY4s, function_call_output call_34TyY4s]ZDR, two tools, non-streaming (gpt-5.6, effort medium)
curl -sS -w '\nHTTP %{http_code}\n' -X POST http://127.0.0.1:25797/v1/responses -d '{"model":"gpt-5.6","input":"'"$P2"'","store":false,"reasoning":{"effort":"medium"},"include":["reasoning.encrypted_content"],"tools":'"$TOOLS"'}'HTTP 400with the sameprevious_response_not_founderror forresp_0d73e735e1f87178016a9b5cc4868887d0b5995bacf109c73astore=False previous_response_id=resp_0d73e735e1f8... input=[message, function_call call_3ZAbsxb, function_call call_94OVPFd, function_call_output call_3ZAbsxb, function_call_output call_94OVPFd]ZDR, streaming (gpt-5.6, effort low)
curl -sS -N -w '\nHTTP %{http_code}\n' -X POST http://127.0.0.1:25797/v1/responses -d '{"model":"gpt-5.6","input":"'"$P1"'","store":false,"stream":true,"reasoning":{"effort":"low"},"include":["reasoning.encrypted_content"],"tools":'"$TOOLS"'}'HTTP 200, SSE eventsresponse.created: 2, response.output_item.done: 4, response.output_text.done: 1, response.completed: 2, [DONE]: 1, text42store=False previous_response_id=None input=[message, function_call call_7nrwdKy, function_call_output call_7nrwdKy](no reasoning item, none emitted by this model)Stateful regression (gpt-5.6, store unset)
curl -sS -w '\nHTTP %{http_code}\n' -X POST http://127.0.0.1:25797/v1/responses -d '{"model":"gpt-5.6","input":"'"$P1"'","reasoning":{"effort":"low"},"tools":'"$TOOLS"'}'HTTP 200,status: completed, output types[message, mcp_tools_fetched, tool_execution_results], text42store=None previous_response_id=resp_09ef2659e00c... input=[message, function_call call_I5mFTs1, function_call_output call_I5mFTs1]ZDR, high reasoning, non-streaming (gpt-5.5)
curl -sS -w '\nHTTP %{http_code}\n' -X POST http://127.0.0.1:25797/v1/responses -d '{"model":"gpt-5.5","input":"'"$P3"'","store":false,"reasoning":{"effort":"high"},"include":["reasoning.encrypted_content"],"tools":'"$TOOLS"'}'HTTP 400withprevious_response_not_foundforresp_0e1f5bfc730f57be016a9b5cd6e37487d0b803426e2c216ab3store=False previous_response_id=resp_0e1f5bfc730f... input=[message, function_call ..., function_call_output ...](thers_reasoning item from the first response is missing)ZDR, high reasoning, streaming (gpt-5.5)
"stream":true, sent withcurl -sS -NHTTP 200, SSEresponse.created: 2, response.output_item.done: 7, response.completed: 2, [DONE]: 1, text starts17 + 25 = 42and says neither result is primestore=False previous_response_id=None input=[message, function_call call_7HlYe1Q, function_call call_GEaMp90, function_call_output call_7HlYe1Q, function_call_output call_GEaMp90](thers_reasoning item from the first response is missing)ZDR without
include(gpt-5.5, effort low, non-streaming)curl -sS -w '\nHTTP %{http_code}\n' -X POST http://127.0.0.1:25797/v1/responses -d '{"model":"gpt-5.5","input":"'"$P1"'","store":false,"reasoning":{"effort":"low"},"tools":'"$TOOLS"'}'HTTP 400withprevious_response_not_foundforresp_013f73d48ffc18f9016a9b5ce904bc87d0a560830742f90a87store=False previous_response_id=resp_013f73d48ffc... input=[message, function_call ..., function_call_output ...]ZDR with a caller-supplied
previous_response_id(gpt-5.6)curl -sS -X POST http://127.0.0.1:25797/v1/responses -d '{"model":"gpt-5.6","input":"Remember: my favorite number is 42. Reply with OK.","store":true,"reasoning":{"effort":"low"}}'returnsid: resp_...006...curl -sS -w '\nHTTP %{http_code}\n' -X POST http://127.0.0.1:25797/v1/responses -d '{"model":"gpt-5.6","input":"Use the tools to add my favorite number and 8, then tell me the result.","store":false,"previous_response_id":"<that id>","reasoning":{"effort":"low"},"include":["reasoning.encrypted_content"],"tools":'"$TOOLS"'}'HTTP 400withprevious_response_not_foundforresp_02bfee05b7e97d33016a9b5cf3d24c87d09b751cc01671d62e, the unstored first-turn id, not the caller's stored onestore=False previous_response_id=resp_02bfee05b7e9... input=[message, function_call ..., function_call_output ...]ZDR, high reasoning, no
include, non-streaming (gpt-5.5)curl -sS -w '\nHTTP %{http_code}\n' -X POST http://127.0.0.1:25797/v1/responses -d '{"model":"gpt-5.5","input":"'"$P3"'","store":false,"reasoning":{"effort":"high"},"tools":'"$TOOLS"'}'HTTP 400withprevious_response_not_foundforresp_00b96e9013209c42016a9b5ed1024c87d088e36bc73748112estore=False previous_response_id=resp_00b96e901320... input=[message, function_call call_0kLfwF4, function_call_output call_0kLfwF4]ZDR, high reasoning, no
include, streaming (gpt-5.5)"stream":true, sent withcurl -sS -NHTTP 200, SSEresponse.created: 6, response.output_item.done: 24, response.completed: 6, [DONE]: 1, six model rounds whose text fragments are42five times and then a summary sentencestore=False previous_response_id=Nonecarrying only the latest round'sfunction_callandfunction_call_output, never a reasoning itemstore: falseset in the model's config, not the request (gpt-5.6-zdr)curl -sS -w '\nHTTP %{http_code}\n' -X POST http://127.0.0.1:25797/v1/responses -d '{"model":"gpt-5.6-zdr","input":"'"$P1"'","reasoning":{"effort":"low"},"include":["reasoning.encrypted_content"],"tools":'"$TOOLS"'}'HTTP 400withprevious_response_not_foundforresp_096f04a1a6dcca31016a9b5ecc773487d0b6f72aa002bc2239,Received Model Group=gpt-5.6-zdrstore=False previous_response_id=resp_096f04a1a6dc... input=[message, function_call call_hBRksDb, function_call_output call_hBRksDb]ZDR through the Anthropic bridge, non-streaming (claude-sonnet-5)
curl -sS -w '\nHTTP %{http_code}\n' -X POST http://127.0.0.1:25797/v1/responses -d '{"model":"claude","input":"'"$P1"'","store":false,"reasoning":{"effort":"low"},"include":["reasoning.encrypted_content"],"tools":'"$TOOLS"'}'HTTP 200,status: completed, text17 + 25 = 42user, assistant, userturn to Anthropic, noprevious_response_idinvolvedZDR through the Anthropic bridge, streaming (claude-sonnet-5)
"stream":true, sent withcurl -sS -NHTTP 200, text17 + 25 = 42/v1/chat/completionscontrol withstore: false(gpt-5.6)curl -sS -w '\nHTTP %{http_code}\n' -X POST http://127.0.0.1:25797/v1/chat/completions -d '{"model":"gpt-5.6","messages":[{"role":"user","content":"'"$P1"'"}],"store":false,"reasoning_effort":"low","tools":'"$TOOLS"'}'HTTP 200, text17 + 25 = **42**.previous_response_id=None(the chat path never chained on a response id)Stateful, high reasoning control (gpt-5.5, store unset)
curl -sS -w '\nHTTP %{http_code}\n' -X POST http://127.0.0.1:25797/v1/responses -d '{"model":"gpt-5.5","input":"'"$P3"'","reasoning":{"effort":"high"},"tools":'"$TOOLS"'}'HTTP 200,status: completed; this run the model asked for its two tools one after the other, so the single follow-up round ended on afunction_calland the text was emptystore=None previous_response_id=resp_0a54535db0c1... input=[message, function_call call_olu5ynn, function_call_output call_olu5ynn]ZDR, sequential tool rounds, streaming (gpt-5.6)
curl -sS -N -w '\nHTTP %{http_code}\n' -X POST http://127.0.0.1:25797/v1/responses -d '{"model":"gpt-5.6","input":"'"$P4"'","store":false,"stream":true,"reasoning":{"effort":"low"},"include":["reasoning.encrypted_content"],"tools":'"$TOOLS"'}'HTTP 200, SSEresponse.created: 6, response.output_item.done: 15, response.completed: 6, response.output_text.done: 1, text17 + 25 = 42, and 42 × 2 = 84.store=False previous_response_id=None input=[message, function_call ..., function_call_output ...], never a reasoning itemZDR, sequential tool rounds, non-streaming (gpt-5.6)
curl -sS -w '\nHTTP %{http_code}\n' -X POST http://127.0.0.1:25797/v1/responses -d '{"model":"gpt-5.6","input":"'"$P4"'","store":false,"reasoning":{"effort":"low"},"include":["reasoning.encrypted_content"],"tools":'"$TOOLS"'}'HTTP 400withprevious_response_not_foundforresp_0593dd2d18f64279016a9b5da2c87487d09eea1a195b10b464store=False previous_response_id=resp_0593dd2d18f6... input=[message, function_call ..., function_call_output ...]Stateful, sequential tool rounds, non-streaming (gpt-5.6, store unset)
curl -sS -w '\nHTTP %{http_code}\n' -X POST http://127.0.0.1:25797/v1/responses -d '{"model":"gpt-5.6","input":"'"$P4"'","reasoning":{"effort":"low"},"tools":'"$TOOLS"'}'HTTP 200,status: completed, empty text: the single follow-up round ended on the secondfunction_callstore=None previous_response_id=resp_03798bfc145d... input=[message, function_call ..., function_call_output ...]Spend rows
curl -sS http://127.0.0.1:25797/spend/logs -H "Authorization: Bearer sk-lit5427-qa"/v1/responsescase above is logged as a failed callAfter (4b24e3b)
ZDR, single tool, non-streaming (gpt-5.6, effort low)
HTTP 200,status: completed, output types[message, mcp_tools_fetched, tool_execution_results], text17 + 25 = **42**.store=False previous_response_id=None input=[message, function_call call_cgIY87q, function_call_output call_cgIY87q]ZDR, two tools, non-streaming (gpt-5.6, effort medium)
HTTP 200,status: completed, text17 + 25 = 42, and 6 × 7 = 42.store=False previous_response_id=None input=[message, function_call call_yjSfxLA, function_call call_mSNurOU, function_call_output call_yjSfxLA, function_call_output call_mSNurOU]ZDR, streaming (gpt-5.6, effort low)
HTTP 200, SSE eventsresponse.created: 2, response.output_item.done: 4, response.output_text.done: 1, response.completed: 2, [DONE]: 1, text17 + 25 = **42**.store=False previous_response_id=None input=[message, function_call call_miALrfB, function_call_output call_miALrfB](no reasoning item, none emitted by this model)Stateful regression (gpt-5.6, store unset)
HTTP 200,status: completed, output types[message, mcp_tools_fetched, tool_execution_results], text17 + 25 = **42**.store=None previous_response_id=resp_0f1b2c2cecb8... input=[message, function_call call_q6PfdlV, function_call_output call_q6PfdlV](unchanged: still chained on the first response id)ZDR, high reasoning, non-streaming (gpt-5.5)
HTTP 200,status: completed; this run the model asked for its two tools one after the other, so the single follow-up round ended on afunction_calland the text was empty (same limit the stateful control hits below)store=False previous_response_id=None input=[message, reasoning rs_096f709b2... (encrypted_content set), function_call call_2u4bjhT, function_call_output call_2u4bjhT]ZDR, high reasoning, streaming (gpt-5.5)
HTTP 200, SSEresponse.created: 6, response.output_item.done: 15, response.output_text.done: 1, response.completed: 6, [DONE]: 1, text starts17 + 25 = 42and says neither result is primestore=False previous_response_id=None input=[message, reasoning rs_04adff0ee... (encrypted_content set), function_call call_FIGfTKQ, function_call_output call_FIGfTKQ], the last carryingreasoning rs_0ef3a884d... (encrypted_content set)withcall_ldrnwSJZDR without
include(gpt-5.5, effort low, non-streaming)HTTP 200,status: completed, text17 + 25 = 42store=False previous_response_id=None input=[message, function_call call_yqJFGGJ, function_call_output call_yqJFGGJ]ZDR with a caller-supplied
previous_response_id(gpt-5.6)id: resp_04c8c3c84a67...006...HTTP 200,status: completed, textThe result is **50**.(42 + 8, so the stored turn was honored)store=False previous_response_id=resp_04c8c3c84a67...006... input=[message, function_call call_lG2Euk9, function_call_output call_lG2Euk9](the caller's own id, kept as sent)ZDR, high reasoning, no
include, non-streaming (gpt-5.5)HTTP 200,status: completed, output types[reasoning, message, mcp_tools_fetched, tool_execution_results], text starts- **17 + 25 = 42**and says neither result is primestore=False previous_response_id=None input=[message, function_call call_BlRDxSG, function_call call_RKGqKsw, function_call_output call_BlRDxSG, function_call_output call_RKGqKsw](the first response carried no reasoning item this run; the final one carriedrs_017d30fe...withencrypted_contentset even withoutinclude)ZDR, high reasoning, no
include, streaming (gpt-5.5)HTTP 200, SSEresponse.created: 2, response.output_item.done: 6, response.output_text.done: 1, response.completed: 2, [DONE]: 1, text starts17 + 25 = 42and says neither result is primestore=False previous_response_id=None input=[message, function_call call_7Tqh1Ak, function_call call_V6tEfPt, function_call_output call_7Tqh1Ak, function_call_output call_V6tEfPt]store: falseset in the model's config, not the request (gpt-5.6-zdr)HTTP 200,status: completed, output types[message, mcp_tools_fetched, tool_execution_results], text17 + 25 = **42**.store=False previous_response_id=None input=[message, function_call call_gPVpZGc, function_call_output call_gPVpZGc]ZDR through the Anthropic bridge, non-streaming (claude-sonnet-5)
HTTP 200,status: completed, text17 + 25 = 42user, assistant, userturn to Anthropic (unchanged)ZDR through the Anthropic bridge, streaming (claude-sonnet-5)
HTTP 200, text17 + 25 = 42/v1/chat/completionscontrol withstore: false(gpt-5.6)HTTP 200, text17 + 25 = **42**.previous_response_id=None(unchanged)Stateful, high reasoning control (gpt-5.5, store unset)
HTTP 200,status: completed, text starts17 + 25 = 42and says neither result is prime (this run the model asked for both tools in one round)store=None previous_response_id=resp_0f1685d9b228... input=[message, function_call call_LeGfUET, function_call call_F4wdmEY, function_call_output call_LeGfUET, function_call_output call_F4wdmEY](unchanged: chained on the first response id, no reasoning replay)ZDR, sequential tool rounds, streaming (gpt-5.6)
HTTP 200, SSEresponse.created: 6, response.output_item.done: 15, response.completed: 6, response.output_text.done: 1, text17 + 25 = 42, and 42 × 2 = 84.store=False previous_response_id=None; the rounds where the model emitted reasoning carry it, e.g.input=[message, reasoning rs_03b2fdb8c... (encrypted_content set), function_call call_j1sOMEA, function_call_output call_j1sOMEA]ZDR, sequential tool rounds, non-streaming (gpt-5.6)
HTTP 200,status: completed, empty text: the single follow-up round ended on the secondfunction_call, exactly as the stateful case belowstore=False previous_response_id=None input=[message, function_call call_yRLQecQ, function_call_output call_yRLQecQ]Stateful, sequential tool rounds, non-streaming (gpt-5.6, store unset)
HTTP 200,status: completed, empty text: the single follow-up round ended on the secondfunction_call(unchanged)store=None previous_response_id=resp_01987751b5da... input=[message, function_call ..., function_call_output ...](unchanged)Spend rows
curl -sS http://127.0.0.1:30453/spend/logs -H "Authorization: Bearer sk-lit5427-qa"/v1/responsescases are now logged as successful calls, the MCPlist_toolsandcall_mcp_toolrows alongside themObservations from the run, each with whether this PR causes it, worsens it, or leaves it alone:
function_call: pre-existing, left aloneresponse.created/response.completedpair per round: pre-existing, left aloneencrypted_contentunderstore: falsewithoutinclude: observed, why the no-includecases passgpt-5.6emitted no reasoning items in these runs: observed, replay evidence is fromgpt-5.5Type
🐛 Bug Fix
Caveats (if any)
Low
function_callencrypted_contentwould be sent back verbatimstore: false; handling an unseen shape is net-negativeproxy-infra / Run tests (Python 3.10)is red on staging's owntest_http_parsing_utils.py(fails on every PR and on staging itself, fix in fix(proxy): strip every TypedDict qualifier before numeric form-field detection #39780), not on this diff, and it is not a required checkci/circleci: proxy_store_model_in_db_testsis red ontests/store_model_in_db_tests/test_openai_error_handling.py::test_chat_completion_bad_model_with_spend_logs(assert '' == 'non-existent-model'), which also fails on staging's six latest pipelines (89086 through 89115) and is not a required checkFinal Attestation
Link to Devin session: https://app.devin.ai/sessions/59dc2762429347968e755ac996c90c34
Requested by: @mateo-berri
Note
Medium Risk
Changes MCP follow-up request shaping and
previous_response_idfor a sensitive ZDR path; scope is limited to Responses MCP auto-execute with broad test coverage, but incorrect replay could still break reasoning models or multi-tool turns.Overview
Fixes MCP auto-execution on zero data retention (
store: false) Responses requests, where the post-tool follow-up used to chain on the first turn’sprevious_response_ideven though that response was never stored.When persistence is off, the non-streaming MCP path now passes the caller’s
previous_response_idinto_make_follow_up_callinstead of the ephemeral first-response id, and builds follow-upinputwithpreserve_reasoningso reasoning items (includingencrypted_content) are replayed in original output order alongside function calls—not batched separately.Adds
_is_persistence_disabledand extends_create_follow_up_inputwith orderedturn_items; the streaming MCP iterator uses the same reasoning replay whenstoreis false. Stateful requests (storetrue/unset) still chain on the first response id and do not resend reasoning. Regression tests cover LIT-5427 for both paths.Reviewed by Cursor Bugbot for commit 4b24e3b. Bugbot is set up for automated code reviews on this repo. Configure here.