The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]
Python ai-gatewayanthropicazure-openaibedrockgatewaylangchainlitellmllmllm-gatewayllmopsmcp-gatewayopenaiopenai-proxyrustrust-aivertex-ai
fix(responses): make response-id encoding idempotent to prevent MCP gateway double-encoding previous_response_id (#32034)
thibault-linktree4b0ac8b352e1
diff --git a/litellm/responses/utils.py b/litellm/responses/utils.pyindex cff113dc3e5..234eb777aca 100644--- a/litellm/responses/utils.py+++ b/litellm/responses/utils.py@@ -204,6 +204,9 @@ class ResponsesAPIRequestUtils:if response_id is None:return responses_api_response+ if ResponsesAPIRequestUtils._is_litellm_encoded_response_id(response_id):+ return responses_api_response+updated_id = ResponsesAPIRequestUtils._build_responses_api_response_id(model_id=model_id,custom_llm_provider=custom_llm_provider,@@ -470,6 +473,14 @@ class ResponsesAPIRequestUtils:response_id=response_id,)+ @staticmethod+ def _is_litellm_encoded_response_id(response_id: str) -> bool:+ decoded_response_id = ResponsesAPIRequestUtils._decode_responses_api_response_id(response_id)+ return (+ decoded_response_id.get("model_id") is not None+ or decoded_response_id.get("custom_llm_provider") is not None+ )+@staticmethoddef get_model_id_from_response_id(response_id: Optional[str]) -> Optional[str]:"""Get the model_id from the response_id"""diff --git a/tests/test_litellm/responses/test_responses_utils.py b/tests/test_litellm/responses/test_responses_utils.pyindex bd441321507..bbc137b959f 100644--- a/tests/test_litellm/responses/test_responses_utils.py+++ b/tests/test_litellm/responses/test_responses_utils.py@@ -142,6 +142,26 @@ class TestResponsesAPIRequestUtils:assert decoded.get("model_id") == "gpt-4o"assert decoded.get("custom_llm_provider") == "openai"++ def test_update_responses_api_response_id_with_model_id_is_idempotent_for_litellm_ids(self):+ raw = "resp_" + "a" * 48+ litellm_metadata = {"model_info": {"id": "model-123"}}++ once = ResponsesAPIRequestUtils._update_responses_api_response_id_with_model_id(+ {"id": raw},+ custom_llm_provider="openai",+ litellm_metadata=litellm_metadata,+ )+ twice = ResponsesAPIRequestUtils._update_responses_api_response_id_with_model_id(+ {"id": once["id"]},+ custom_llm_provider="openai",+ litellm_metadata=litellm_metadata,+ )++ assert twice == once+ assert ResponsesAPIRequestUtils.decode_previous_response_id_to_original_previous_response_id(twice["id"]) == raw+ assert ResponsesAPIRequestUtils._decode_responses_api_response_id(once["id"]).get("response_id") == raw+def test_build_decode_container_id_omits_none_model_id(self):"""model_id=None must not round-trip as the truthy string 'None'."""encoded = ResponsesAPIRequestUtils._build_container_id(