Skip to content

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]

[Bug Fix] Responses API - fix for handling multiturn responses API sessions (#10415)

Ishaan Jaffa69fa1dc1e32
diff --git a/litellm/proxy/proxy_config.yaml b/litellm/proxy/proxy_config.yaml
index 78cb7e010aa..8ff36b91f57 100644
--- a/litellm/proxy/proxy_config.yaml
+++ b/litellm/proxy/proxy_config.yaml
@@ -1,7 +1,10 @@
model_list:
- - model_name: anthropic/*
+ - model_name: computer-user-agent
litellm_params:
- model: anthropic/*
- api_key: os.environ/ANTHROPIC_API_KEY
+ model: azure/computer-use-preview
+ truncation: auto
+ api_key: os.environ/AZURE_RESPONSES_OPENAI_API_KEY
+ api_base: os.environ/AZURE_RESPONSES_OPENAI_ENDPOINT
+ api_version: os.environ/AZURE_RESPONSES_OPENAI_API_VERSION
general_settings:
store_prompts_in_spend_logs: true
diff --git a/litellm/responses/utils.py b/litellm/responses/utils.py
index cf51c40b2a1..77462925e53 100644
--- a/litellm/responses/utils.py
+++ b/litellm/responses/utils.py
@@ -79,6 +79,14 @@ class ResponsesAPIRequestUtils:
filtered_params = {
k: v for k, v in params.items() if k in valid_keys and v is not None
}
+
+ # decode previous_response_id if it's a litellm encoded id
+ if "previous_response_id" in filtered_params:
+ decoded_previous_response_id = ResponsesAPIRequestUtils.decode_previous_response_id_to_original_previous_response_id(
+ filtered_params["previous_response_id"]
+ )
+ filtered_params["previous_response_id"] = decoded_previous_response_id
+
return cast(ResponsesAPIOptionalRequestParams, filtered_params)
@staticmethod
@@ -186,6 +194,30 @@ class ResponsesAPIRequestUtils:
)
return decoded_response_id.get("model_id") or None
+ @staticmethod
+ def decode_previous_response_id_to_original_previous_response_id(
+ previous_response_id: str,
+ ) -> str:
+ """
+ Decode the previous_response_id to the original previous_response_id
+
+ Why?
+ - LiteLLM encodes the `custom_llm_provider` and `model_id` into the `previous_response_id` this helps with maintaining session consistency when load balancing multiple deployments of the same model.
+ - We cannot send the litellm encoded b64 to the upstream llm api, hence we decode it to the original `previous_response_id`
+
+ Args:
+ previous_response_id: The previous_response_id to decode
+
+ Returns:
+ The original previous_response_id
+ """
+ decoded_response_id = (
+ ResponsesAPIRequestUtils._decode_responses_api_response_id(
+ previous_response_id
+ )
+ )
+ return decoded_response_id.get("response_id", previous_response_id)
+
class ResponseAPILoggingUtils:
@staticmethod
diff --git a/tests/litellm/responses/test_responses_utils.py b/tests/litellm/responses/test_responses_utils.py
index 3567f609e73..af01ca56ddb 100644
--- a/tests/litellm/responses/test_responses_utils.py
+++ b/tests/litellm/responses/test_responses_utils.py
@@ -1,3 +1,4 @@
+import base64
import json
import os
import sys
@@ -84,6 +85,35 @@ class TestResponsesAPIRequestUtils:
assert result["temperature"] == 0.7
assert result["max_output_tokens"] == 100
+ def test_decode_previous_response_id_to_original_previous_response_id(self):
+ """Test decoding a LiteLLM encoded previous_response_id to the original previous_response_id"""
+ # Setup
+ test_provider = "openai"
+ test_model_id = "gpt-4o"
+ original_response_id = "resp_abc123"
+
+ # Use the helper method to build an encoded response ID
+ encoded_id = ResponsesAPIRequestUtils._build_responses_api_response_id(
+ custom_llm_provider=test_provider,
+ model_id=test_model_id,
+ response_id=original_response_id,
+ )
+
+ # Execute
+ result = ResponsesAPIRequestUtils.decode_previous_response_id_to_original_previous_response_id(
+ encoded_id
+ )
+
+ # Assert
+ assert result == original_response_id
+
+ # Test with a non-encoded ID
+ plain_id = "resp_xyz789"
+ result_plain = ResponsesAPIRequestUtils.decode_previous_response_id_to_original_previous_response_id(
+ plain_id
+ )
+ assert result_plain == plain_id
+
class TestResponseAPILoggingUtils:
def test_is_response_api_usage_true(self):
diff --git a/tests/llm_responses_api_testing/base_responses_api.py b/tests/llm_responses_api_testing/base_responses_api.py
index 83122f9876d..71dca524b9b 100644
--- a/tests/llm_responses_api_testing/base_responses_api.py
+++ b/tests/llm_responses_api_testing/base_responses_api.py
@@ -314,4 +314,24 @@ class BaseResponsesAPITest(ABC):
else:
raise ValueError("response is not a ResponsesAPIResponse")
-
+ @pytest.mark.asyncio
+ async def test_multiturn_responses_api(self):
+ litellm._turn_on_debug()
+ litellm.set_verbose = True
+ base_completion_call_args = self.get_base_completion_call_args()
+ response_1 = await litellm.aresponses(
+ input="Basic ping", max_output_tokens=20, **base_completion_call_args
+ )
+
+ # follow up with a second request
+ response_1_id = response_1.id
+ response_2 = await litellm.aresponses(
+ input="Basic ping",
+ max_output_tokens=20,
+ previous_response_id=response_1_id,
+ **base_completion_call_args
+ )
+
+ # assert the response is not None
+ assert response_1 is not None
+ assert response_2 is not None