fix(responses): make response-id encoding idempotent to prevent MCP gateway double-encoding previous_response_id (#32034)

This commit is contained in:
thibault-linktree 2026-07-08 12:47:11 +10:00 • committed by GitHub
parent 3ea27bd64c
commit 4b0ac8b352
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 31 additions and 0 deletions

View file

@ -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
)
@staticmethod
def get_model_id_from_response_id(response_id: Optional[str]) -> Optional[str]:
"""Get the model_id from the response_id"""

View file

@ -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(