mirror of
https://github.com/BerriAI/litellm.git
synced 2026-10-07 02:59:05 +00:00
fix(responses): make response-id encoding idempotent to prevent MCP gateway double-encoding previous_response_id (#32034)
This commit is contained in:
parent
3ea27bd64c
commit
4b0ac8b352
2 changed files with 31 additions and 0 deletions
|
|
@ -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"""
|
||||
|
|
|
|||
|
|
@ -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(
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue