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
test: handle internal server errors
Krrish Dholakiacbb161f10b81
diff --git a/tests/llm_responses_api_testing/base_responses_api.py b/tests/llm_responses_api_testing/base_responses_api.pyindex 72f6fb8c9cf..5cb8295b1af 100644--- a/tests/llm_responses_api_testing/base_responses_api.py+++ b/tests/llm_responses_api_testing/base_responses_api.py@@ -101,11 +101,11 @@ def validate_responses_api_response(response, final_chunk: bool = False):return True # Return True if validation passes-class BaseResponsesAPITest(ABC):"""Abstract base test class that enforces a common test across all test classes."""+@abstractmethoddef get_base_completion_call_args(self) -> dict:"""Must return the base completion call args"""@@ -115,32 +115,32 @@ class BaseResponsesAPITest(ABC):"""Must return the base completion reasoning call args"""return None-@pytest.mark.parametrize("sync_mode", [True, False])@pytest.mark.asyncioasync def test_basic_openai_responses_api(self, sync_mode):litellm._turn_on_debug()litellm.set_verbose = Truebase_completion_call_args = self.get_base_completion_call_args()- try:+ try:if sync_mode:response = litellm.responses(- input="Basic ping", max_output_tokens=20,- **base_completion_call_args+ input="Basic ping",+ max_output_tokens=20,+ **base_completion_call_args,)else:response = await litellm.aresponses(- input="Basic ping", max_output_tokens=20,- **base_completion_call_args+ input="Basic ping",+ max_output_tokens=20,+ **base_completion_call_args,)- except litellm.InternalServerError:+ except litellm.InternalServerError:pytest.skip("Skipping test due to litellm.InternalServerError")print("litellm response=", json.dumps(response, indent=4, default=str))# Use the helper function to validate the responsevalidate_responses_api_response(response, final_chunk=True)-@pytest.mark.parametrize("sync_mode", [True, False])@pytest.mark.asyncio@pytest.mark.flaky(retries=3, delay=2)@@ -151,9 +151,7 @@ class BaseResponsesAPITest(ABC):response_completed_event = Noneif sync_mode:response = litellm.responses(- input="Basic ping",- stream=True,- **base_completion_call_args+ input="Basic ping", stream=True, **base_completion_call_args)for event in response:print("litellm response=", json.dumps(event, indent=4, default=str))@@ -163,9 +161,7 @@ class BaseResponsesAPITest(ABC):response_completed_event = eventelse:response = await litellm.aresponses(- input="Basic ping",- stream=True,- **base_completion_call_args+ input="Basic ping", stream=True, **base_completion_call_args)async for event in response:print("litellm response=", json.dumps(event, indent=4, default=str))@@ -188,15 +184,29 @@ class BaseResponsesAPITest(ABC):assert response_completed_event.response.usage is not None# basic test assert the usage seems reasonable- print("response_completed_event.response.usage=", response_completed_event.response.usage)- assert response_completed_event.response.usage.input_tokens > 0 and response_completed_event.response.usage.input_tokens < 100- assert response_completed_event.response.usage.output_tokens > 0 and response_completed_event.response.usage.output_tokens < 2000- assert response_completed_event.response.usage.total_tokens > 0 and response_completed_event.response.usage.total_tokens < 2000+ print(+ "response_completed_event.response.usage=",+ response_completed_event.response.usage,+ )+ assert (+ response_completed_event.response.usage.input_tokens > 0+ and response_completed_event.response.usage.input_tokens < 100+ )+ assert (+ response_completed_event.response.usage.output_tokens > 0+ and response_completed_event.response.usage.output_tokens < 2000+ )+ assert (+ response_completed_event.response.usage.total_tokens > 0+ and response_completed_event.response.usage.total_tokens < 2000+ )# total tokens should be the sum of input and output tokens- assert response_completed_event.response.usage.total_tokens == response_completed_event.response.usage.input_tokens + response_completed_event.response.usage.output_tokens--+ assert (+ response_completed_event.response.usage.total_tokens+ == response_completed_event.response.usage.input_tokens+ + response_completed_event.response.usage.output_tokens+ )@pytest.mark.parametrize("sync_mode", [False, True])@pytest.mark.asyncio@@ -206,48 +216,44 @@ class BaseResponsesAPITest(ABC):base_completion_call_args = self.get_base_completion_call_args()if sync_mode:response = litellm.responses(- input="Basic ping", max_output_tokens=20,- **base_completion_call_args+ input="Basic ping", max_output_tokens=20, **base_completion_call_args)# delete the responseif isinstance(response, ResponsesAPIResponse):litellm.delete_responses(- response_id=response.id,- **base_completion_call_args+ response_id=response.id, **base_completion_call_args)else:raise ValueError("response is not a ResponsesAPIResponse")else:response = await litellm.aresponses(- input="Basic ping", max_output_tokens=20,- **base_completion_call_args+ input="Basic ping", max_output_tokens=20, **base_completion_call_args)# async delete the responseif isinstance(response, ResponsesAPIResponse):await litellm.adelete_responses(- response_id=response.id,- **base_completion_call_args+ response_id=response.id, **base_completion_call_args)else:raise ValueError("response is not a ResponsesAPIResponse")-@pytest.mark.parametrize("sync_mode", [True, False])@pytest.mark.flaky(retries=3, delay=2)@pytest.mark.asyncioasync def test_basic_openai_responses_streaming_delete_endpoint(self, sync_mode):- #litellm._turn_on_debug()- #litellm.set_verbose = True+ # litellm._turn_on_debug()+ # litellm.set_verbose = Truebase_completion_call_args = self.get_base_completion_call_args()response_id = Noneif sync_mode:response_id = Noneresponse = litellm.responses(- input="Basic ping", max_output_tokens=20,+ input="Basic ping",+ max_output_tokens=20,stream=True,- **base_completion_call_args+ **base_completion_call_args,)for event in response:print("litellm response=", json.dumps(event, indent=4, default=str))@@ -260,14 +266,14 @@ class BaseResponsesAPITest(ABC):# delete the responseassert response_id is not Nonelitellm.delete_responses(- response_id=response_id,- **base_completion_call_args+ response_id=response_id, **base_completion_call_args)else:response = await litellm.aresponses(- input="Basic ping", max_output_tokens=20,+ input="Basic ping",+ max_output_tokens=20,stream=True,- **base_completion_call_args+ **base_completion_call_args,)async for event in response:print("litellm response=", json.dumps(event, indent=4, default=str))@@ -280,8 +286,7 @@ class BaseResponsesAPITest(ABC):# delete the responseassert response_id is not Noneawait litellm.adelete_responses(- response_id=response_id,- **base_completion_call_args+ response_id=response_id, **base_completion_call_args)@pytest.mark.parametrize("sync_mode", [False, True])@@ -293,15 +298,13 @@ class BaseResponsesAPITest(ABC):base_completion_call_args = self.get_base_completion_call_args()if sync_mode:response = litellm.responses(- input="Basic ping", max_output_tokens=20,- **base_completion_call_args+ input="Basic ping", max_output_tokens=20, **base_completion_call_args)# get the responseif isinstance(response, ResponsesAPIResponse):result = litellm.get_responses(- response_id=response.id,- **base_completion_call_args+ response_id=response.id, **base_completion_call_args)assert result is not Noneassert result.id == response.id@@ -310,14 +313,12 @@ class BaseResponsesAPITest(ABC):raise ValueError("response is not a ResponsesAPIResponse")else:response = await litellm.aresponses(- input="Basic ping", max_output_tokens=20,- **base_completion_call_args+ input="Basic ping", max_output_tokens=20, **base_completion_call_args)# async get the responseif isinstance(response, ResponsesAPIResponse):result = await litellm.aget_responses(- response_id=response.id,- **base_completion_call_args+ response_id=response.id, **base_completion_call_args)assert result is not Noneassert result.id == response.id@@ -351,58 +352,60 @@ class BaseResponsesAPITest(ABC):json.dumps(list_items_response, indent=4, default=str),)-@pytest.mark.asyncioasync 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- )+ try:+ 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- )+ # 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+ except litellm.InternalServerError:+ pytest.skip("Skipping test due to litellm.InternalServerError")- # assert the response is not None- assert response_1 is not None- assert response_2 is not None-@pytest.mark.asyncioasync def test_responses_api_with_tool_calls(self):"""Test that calls the Responses API with tool calls including function call and output"""litellm._turn_on_debug()litellm.set_verbose = Truebase_completion_call_args = self.get_base_completion_call_args()-+# Define the input with message, function call, and function call outputinput_data: ResponseInputParam = [{"type": "message","role": "user",- "content": "How is the weather in São Paulo today ?"+ "content": "How is the weather in São Paulo today ?",},{"type": "function_call",- "arguments": "{\"location\": \"São Paulo, Brazil\"}",+ "arguments": '{"location": "São Paulo, Brazil"}',"call_id": "fc_1fe70e2a-a596-45ef-b72c-9b8567c460e5","name": "get_weather","id": "fc_1fe70e2a-a596-45ef-b72c-9b8567c460e5",- "status": "completed"+ "status": "completed",},{"type": "function_call_output","call_id": "fc_1fe70e2a-a596-45ef-b72c-9b8567c460e5",- "output": "Rainy"- }+ "output": "Rainy",+ },]-+# Define the toolstools = [{@@ -414,71 +417,67 @@ class BaseResponsesAPITest(ABC):"properties": {"location": {"type": "string",- "description": "City and country e.g. Bogotá, Colombia"+ "description": "City and country e.g. Bogotá, Colombia",}},"required": ["location"],- "additionalProperties": False- }+ "additionalProperties": False,+ },}]-+try:# Make the responses API callresponse = await litellm.aresponses(- input=input_data,- store=False,- tools=tools,- **base_completion_call_args+ input=input_data, store=False, tools=tools, **base_completion_call_args)except litellm.InternalServerError:pytest.skip("Skipping test due to litellm.InternalServerError")-+print("litellm response=", json.dumps(response, indent=4, default=str))-+# Validate the response structurevalidate_responses_api_response(response, final_chunk=True)-+# Additional assertions specific to tool callsassert response is not Noneassert "output" in responseassert len(response["output"]) > 0-+@pytest.mark.asyncioasync def test_responses_api_multi_turn_with_reasoning_and_structured_output(self):"""Test multi-turn conversation with reasoning, structured output, and tool calls.-+This test validates:- First call: Model uses reasoning to process a question and makes a tool call- - Tool call handling: Function call output is properly processed+ - Tool call handling: Function call output is properly processed- Second call: Model produces structured output incorporating tool results- Structured output: Response conforms to defined Pydantic model schema"""from pydantic import BaseModel-+litellm._turn_on_debug()litellm.set_verbose = Truebase_completion_call_args = self.get_base_completion_reasoning_call_args()if base_completion_call_args is None:pytest.skip("Skipping test due to no base completion reasoning call args")-+# Define tools for the conversationtools = [{"type": "function", "name": "get_today"}]-+# Define structured output schemaclass Output(BaseModel):today: strnumber_of_r: str-+# Initial conversation inputinput_messages = [{- "role": "user",+ "role": "user","content": "How many r in strrawberrry? While you're thinking, you should call tool get_today. Then you output the today and number of r",}]-# First call - should trigger reasoning and tool callresponse = await litellm.aresponses(@@ -486,49 +485,54 @@ class BaseResponsesAPITest(ABC):tools=tools,reasoning={"effort": "low", "summary": "detailed"},text_format=Output,- **base_completion_call_args+ **base_completion_call_args,)print("First call output:")print(json.dumps(response.output, indent=4, default=str))-+# Validate first response structurevalidate_responses_api_response(response, final_chunk=True)assert response.output is not Noneassert len(response.output) > 0-+# Extend input with first response outputinput_messages.extend(response.output)-+# Process any tool calls and add function outputsfunction_outputs = []for item in response.output:- if hasattr(item, 'type') and item.type in ["function_call", "custom_tool_call"]:- if hasattr(item, 'name') and item.name == "get_today":- function_outputs.append({- "type": "function_call_output",- "call_id": item.call_id,- "output": "2025-01-15"- })-+ if hasattr(item, "type") and item.type in [+ "function_call",+ "custom_tool_call",+ ]:+ if hasattr(item, "name") and item.name == "get_today":+ function_outputs.append(+ {+ "type": "function_call_output",+ "call_id": item.call_id,+ "output": "2025-01-15",+ }+ )+# Add function outputs to conversationinput_messages.extend(function_outputs)-+print("Second call input:")print(json.dumps(input_messages, indent=4, default=str))-+# Second call - should produce structured outputfinal_response = await litellm.aresponses(input=input_messages,tools=tools,reasoning={"effort": "low", "summary": "detailed"},text_format=Output,- **base_completion_call_args+ **base_completion_call_args,)-+print("Second call output:")print(json.dumps(final_response.output, indent=4, default=str))-+# Validate final response structurevalidate_responses_api_response(final_response, final_chunk=True)assert final_response.output is not None