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]

Merge branch 'main' into litellm_wt_0918_138e

Yuneng Jiangd90da59ac712
diff --cc tests/e2e/llm_translation/test_responses_e2e.py
index 259eafa9efe,9cc70da63b0..6fa77694eb8
--- a/tests/e2e/llm_translation/test_responses_e2e.py
+++ b/tests/e2e/llm_translation/test_responses_e2e.py
@@@ -39,18 -45,55 +45,58 @@@ class _OptionalResponsesBody(BaseModel)
BEDROCK_CONVERSE_BACKEND = "bedrock/us.anthropic.claude-haiku-4-5-20251001-v1:0"
+VERTEX_BACKEND: Final = "vertex_ai/gemini-2.5-flash"
+AZURE_OPENAI_BACKEND: Final = "azure/gpt-5.4-nano"
+AZURE_OPENAI_API_VERSION: Final = "v1"
+ INSTRUCTIONS = "You are a helpful assistant"
+ CAT_IMAGE_URL = "https://upload.wikimedia.org/wikipedia/commons/3/3a/Cat03.jpg"
+ BEDROCK_EDGE_REGION: Final = "us-east-1"
+ BEDROCK_EDGE_MOUNT: Final = f"bedrock/{BEDROCK_EDGE_REGION}"
- WEATHER_TOOL = ResponsesFunctionTool(
- name="get_weather",
- description="Get the weather for a location",
- parameters=FunctionParameters(
- properties={"location": FunctionParameterProperty(type="string")},
- required=["location"],
- ),
- )
+
+ class ConverseRequestBody(BaseModel):
+ additionalModelRequestFields: dict[str, str] | None = None
+
+
+ @dataclass(slots=True)
+ class ConverseRequestCapture:
+ """The Converse bodies the proxy actually sent upstream, as seen by a live
+ edge sitting between the proxy and Bedrock."""
+
+ _bodies: list[ConverseRequestBody] = field(default_factory=list)
+ _lock: threading.Lock = field(default_factory=threading.Lock)
+
+ def observe(self, url: str, headers: Mapping[str, str], body: bytes | None) -> None:
+ if body is None or "/converse" not in url:
+ return
+ with self._lock:
+ self._bodies.append(ConverseRequestBody.model_validate_json(body))
+
+ @property
+ def bodies(self) -> tuple[ConverseRequestBody, ...]:
+ with self._lock:
+ return tuple(self._bodies)
+
+
+ WEATHER_TOOL: FunctionToolParam = {
+ "type": "function",
+ "name": "get_weather",
+ "description": "Get the weather for a location",
+ "parameters": {
+ "type": "object",
+ "properties": {"location": {"type": "string"}},
+ "required": ["location"],
+ },
+ "strict": False,
+ }
+
+
+ def _openai_params() -> LiteLLMParamsBody:
+ return LiteLLMParamsBody(model="openai/gpt-4o-mini", api_key="os.environ/OPENAI_API_KEY")
+
+
+ def _anthropic_params() -> LiteLLMParamsBody:
+ return LiteLLMParamsBody(model="anthropic/claude-haiku-4-5", api_key="os.environ/ANTHROPIC_API_KEY")
def _bedrock_params() -> LiteLLMParamsBody:
@@@ -62,6 -105,27 +108,44 @@@
)
++def _vertex_params() -> LiteLLMParamsBody:
++ return LiteLLMParamsBody(
++ model=VERTEX_BACKEND,
++ vertex_project="os.environ/VERTEXAI_PROJECT",
++ vertex_location="us-central1",
++ )
++
++
++def _azure_openai_params() -> LiteLLMParamsBody:
++ return LiteLLMParamsBody(
++ model=AZURE_OPENAI_BACKEND,
++ api_base="os.environ/AZURE_API_BASE",
++ api_key="os.environ/AZURE_API_KEY",
++ api_version=AZURE_OPENAI_API_VERSION,
++ )
++
++
+ def _register(
+ proxy: ProxyClient, resources: ResourceManager, params: LiteLLMParamsBody, prefix: str = "e2e-responses"
+ ) -> str:
+ model = f"{prefix}-{unique_marker()}"
+ model_id = proxy.create_model(model, params)
+ resources.defer(lambda: proxy.delete_model(model_id))
+ return model
+
+
+ def _function_calls(response: Response) -> tuple[ResponseFunctionToolCall, ...]:
+ return tuple(item for item in response.output if isinstance(item, ResponseFunctionToolCall))
+
+
+ def _assert_weather_call(response: Response) -> None:
+ function_call = next((call for call in _function_calls(response) if call.name == "get_weather"), None)
+ assert function_call is not None, f"no get_weather function call: {response.output!r}"
+ raw_arguments = cast(object, json.loads(function_call.arguments))
+ arguments = WeatherArguments.model_validate(raw_arguments)
+ assert arguments.location, f"function call arguments missing location: {function_call.arguments}"
+
+
class WeatherArguments(BaseModel):
location: str
@@@ -279,164 -277,89 +297,149 @@@ class TestResponses
@pytest.mark.covers("llm.responses.bedrock_converse.tool_use.nonstream.works")
def test_responses_bedrock_returns_function_call(
- self, endpoints_client: EndpointsClient, resources: ResourceManager
+ self, proxy: ProxyClient, resources: ResourceManager, sdk: SdkClients
) -> None:
- model = f"e2e-responses-{unique_marker()}"
- model_id = endpoints_client.create_model(model, _bedrock_params())
- resources.defer(lambda: endpoints_client.delete_model(model_id))
- key = resources.key()
-
- result = endpoints_client.responses_with_tools(
- key, model, "What is the weather in San Francisco? Use the get_weather tool.", [WEATHER_TOOL]
+ model = _register(proxy, resources, _bedrock_params())
+ client = sdk.openai(resources.key())
+
+ response = client.responses.create(
+ model=model,
+ input="What is the weather in San Francisco? Use the get_weather tool.",
+ instructions=INSTRUCTIONS,
+ tools=[WEATHER_TOOL],
+ extra_body=NO_PROXY_CACHE,
)
- require_successful_call(result)
- parsed = ResponsesResult.model_validate_json(result.body)
- function_call = next((call for call in parsed.function_calls if call.name == "get_weather"), None)
- assert function_call is not None, f"no get_weather function call over bedrock: {result.body[:500]}"
- assert function_call.arguments is not None
- raw_arguments = cast(object, json.loads(function_call.arguments))
- arguments = WeatherArguments.model_validate(raw_arguments)
- assert arguments.location, f"function call arguments missing location: {function_call.arguments}"
-
- def _register(
- self,
- endpoints_client: EndpointsClient,
- resources: ResourceManager,
- prefix: str,
- params: LiteLLMParamsBody,
- ) -> tuple[str, str]:
- model = f"{prefix}-{unique_marker()}"
- model_id = endpoints_client.create_model(model, params)
- resources.defer(lambda: endpoints_client.delete_model(model_id))
- return model, resources.key()
+ _assert_weather_call(response)
+ @pytest.mark.covers("llm.responses.vertex.basic.nonstream.works")
+ def test_responses_vertex_returns_completion(
- self, endpoints_client: EndpointsClient, resources: ResourceManager
++ self, proxy: ProxyClient, resources: ResourceManager, sdk: SdkClients
+ ) -> None:
- model, key = self._register(
- endpoints_client,
- resources,
- "e2e-responses-vertex",
- LiteLLMParamsBody(
- model=VERTEX_BACKEND,
- vertex_project="os.environ/VERTEXAI_PROJECT",
- vertex_location="us-central1",
- ),
- )
++ model = _register(proxy, resources, _vertex_params(), prefix="e2e-responses-vertex")
++ client = sdk.openai(resources.key())
+
- result = endpoints_client.responses(key, model, "reply with one word")
- require_successful_call(result)
- parsed = ResponsesResult.model_validate_json(result.body)
- assert parsed.text.strip(), f"/responses over vertex returned no output text: {result.body[:300]}"
++ response = client.responses.create(
++ model=model, input="reply with one word", instructions=INSTRUCTIONS, extra_body=NO_PROXY_CACHE
++ )
++ assert response.output_text.strip(), f"/responses over vertex returned no output text: {response.output!r}"
+
+ @pytest.mark.covers("llm.responses.vertex.tool_use.nonstream.works")
+ def test_responses_vertex_returns_function_call(
- self, endpoints_client: EndpointsClient, resources: ResourceManager
++ self, proxy: ProxyClient, resources: ResourceManager, sdk: SdkClients
+ ) -> None:
- model, key = self._register(
- endpoints_client,
- resources,
- "e2e-responses-vertex-tool",
- LiteLLMParamsBody(
- model=VERTEX_BACKEND,
- vertex_project="os.environ/VERTEXAI_PROJECT",
- vertex_location="us-central1",
- ),
- )
-
- result = endpoints_client.responses_with_tools(
- key,
- model,
- "What is the weather in San Francisco? Use the get_weather tool.",
- [WEATHER_TOOL],
++ model = _register(proxy, resources, _vertex_params(), prefix="e2e-responses-vertex-tool")
++ client = sdk.openai(resources.key())
++
++ response = client.responses.create(
++ model=model,
++ input="What is the weather in San Francisco? Use the get_weather tool.",
++ instructions=INSTRUCTIONS,
++ tools=[WEATHER_TOOL],
+ tool_choice="required",
++ extra_body=NO_PROXY_CACHE,
+ )
- require_successful_call(result)
- parsed = ResponsesResult.model_validate_json(result.body)
- function_call = next(
- (call for call in parsed.function_calls if call.name == "get_weather"),
- None,
- )
- assert function_call is not None, f"no vertex get_weather function call: {result.body[:500]}"
- assert function_call.arguments is not None
- raw_arguments = cast(object, json.loads(function_call.arguments))
- arguments = WeatherArguments.model_validate(raw_arguments)
- assert arguments.location, f"vertex function call arguments missing location: {function_call.arguments}"
++ _assert_weather_call(response)
+
+ @pytest.mark.covers("llm.responses.azure_openai.basic.nonstream.works")
+ def test_responses_azure_openai_returns_completion(
- self, endpoints_client: EndpointsClient, resources: ResourceManager
++ self, proxy: ProxyClient, resources: ResourceManager, sdk: SdkClients
+ ) -> None:
- model, key = self._register(
- endpoints_client,
- resources,
- "e2e-responses-azure-openai",
- LiteLLMParamsBody(
- model=AZURE_OPENAI_BACKEND,
- api_base="os.environ/AZURE_API_BASE",
- api_key="os.environ/AZURE_API_KEY",
- api_version=AZURE_OPENAI_API_VERSION,
- ),
- )
++ model = _register(proxy, resources, _azure_openai_params(), prefix="e2e-responses-azure-openai")
++ client = sdk.openai(resources.key())
+
- result = endpoints_client.responses(key, model, "reply with one word")
- require_successful_call(result)
- parsed = ResponsesResult.model_validate_json(result.body)
- assert parsed.text.strip(), (
- f"/responses over azure openai returned no output text: {result.body[:300]}"
++ response = client.responses.create(
++ model=model, input="reply with one word", instructions=INSTRUCTIONS, extra_body=NO_PROXY_CACHE
++ )
++ assert response.output_text.strip(), (
++ f"/responses over azure openai returned no output text: {response.output!r}"
+ )
+
+ @pytest.mark.covers("llm.responses.azure_openai.tool_use.nonstream.works")
+ def test_responses_azure_openai_returns_function_call(
- self, endpoints_client: EndpointsClient, resources: ResourceManager
++ self, proxy: ProxyClient, resources: ResourceManager, sdk: SdkClients
+ ) -> None:
- model, key = self._register(
- endpoints_client,
- resources,
- "e2e-responses-azure-openai-tool",
- LiteLLMParamsBody(
- model=AZURE_OPENAI_BACKEND,
- api_base="os.environ/AZURE_API_BASE",
- api_key="os.environ/AZURE_API_KEY",
- api_version=AZURE_OPENAI_API_VERSION,
- ),
++ model = _register(proxy, resources, _azure_openai_params(), prefix="e2e-responses-azure-openai-tool")
++ client = sdk.openai(resources.key())
++
++ response = client.responses.create(
++ model=model,
++ input="What is the weather in San Francisco? Use the get_weather tool.",
++ instructions=INSTRUCTIONS,
++ tools=[WEATHER_TOOL],
++ tool_choice="required",
++ extra_body=NO_PROXY_CACHE,
+ )
++ _assert_weather_call(response)
+
- result = endpoints_client.responses_with_tools(
- key,
+ @pytest.mark.provider_edge_host
+ @pytest.mark.parametrize("endpoint", ["/v1/responses", "/v1/chat/completions"])
+ def test_bedrock_forwards_allowed_safety_identifier_as_additional_model_request_field(
+ self, proxy: ProxyClient, resources: ResourceManager, sdk: SdkClients, endpoint: str
+ ) -> None:
+ """Judges the Converse bodies the edge captured, not the reply: Claude on
+ Bedrock rejects the forwarded field with a 400, which the chat leg's
+ ``Result`` carries as a value and the OpenAI SDK raises."""
+ capture: Final = ConverseRequestCapture()
+ edge: Final = start_provider_edge(
+ LiveEdge(observe_request=capture.observe, sign=bedrock_signer(BEDROCK_EDGE_REGION)),
+ mounts=MappingProxyType(
+ {BEDROCK_EDGE_MOUNT: f"https://bedrock-runtime.{BEDROCK_EDGE_REGION}.amazonaws.com"}
+ ),
+ bind_host=PROVIDER_EDGE_BIND_HOST,
+ advertise_host=PROVIDER_EDGE_ADVERTISE_HOST,
+ )
+ resources.defer(edge.shutdown)
+ model: Final = f"e2e-responses-{unique_marker()}"
+ model_id: Final = proxy.create_model(
model,
- "What is the weather in San Francisco? Use the get_weather tool.",
- [WEATHER_TOOL],
- tool_choice="required",
+ LiteLLMParamsBody(
+ model=BEDROCK_CONVERSE_BACKEND,
+ api_base=edge.edge.api_base(BEDROCK_EDGE_MOUNT),
+ aws_access_key_id="os.environ/AWS_ACCESS_KEY_ID",
+ aws_secret_access_key="os.environ/AWS_SECRET_ACCESS_KEY",
+ aws_region_name=BEDROCK_EDGE_REGION,
+ allowed_openai_params=["safety_identifier"],
+ ),
)
- require_successful_call(result)
- parsed = ResponsesResult.model_validate_json(result.body)
- function_call = next(
- (call for call in parsed.function_calls if call.name == "get_weather"),
- None,
+ resources.defer(lambda: proxy.delete_model(model_id))
+ key: Final = resources.key()
+ safety_identifier: Final = f"end-user-{unique_marker()}"
+
+ if endpoint == "/v1/responses":
+ with contextlib.suppress(openai.BadRequestError):
+ sdk.openai(key).responses.create(
+ model=model,
+ input="reply with one word",
+ instructions=INSTRUCTIONS,
+ safety_identifier=safety_identifier,
+ extra_body=NO_PROXY_CACHE,
+ )
+ else:
+ proxy.chat(
+ key,
+ ChatBody(
+ model=model,
+ messages=[ChatMessage(role="user", content="reply with one word")],
+ safety_identifier=safety_identifier,
+ ),
+ )
+
+ forwarded: Final = tuple(body.additionalModelRequestFields for body in capture.bodies)
+ assert forwarded, f"{endpoint} produced no Bedrock Converse request"
+ assert forwarded == ({"safety_identifier": safety_identifier},) * len(forwarded), (
+ f"{endpoint} did not forward safety_identifier to Bedrock Converse on every attempt: {capture.bodies}"
)
- assert function_call is not None, f"no azure openai get_weather function call: {result.body[:500]}"
- assert function_call.arguments is not None
- raw_arguments = cast(object, json.loads(function_call.arguments))
- arguments = WeatherArguments.model_validate(raw_arguments)
- assert arguments.location, f"azure openai function call arguments missing location: {function_call.arguments}"
- @pytest.mark.skip(reason="stage red: product gap, /v1/responses 500s (aresponses TypeError) on missing input instead of 400")
+ @pytest.mark.skip(
+ reason="stage red: product gap, /v1/responses 500s (aresponses TypeError) on missing input instead of 400"
+ )
@pytest.mark.covers("llm.responses.openai.input_validation.nonstream.works")
- def test_missing_input_returns_error(
- self, endpoints_client: EndpointsClient, resources: ResourceManager
- ) -> None:
- model = f"e2e-responses-val-{unique_marker()}"
- model_id = endpoints_client.create_model(
- model,
- LiteLLMParamsBody(model="openai/gpt-4o-mini", api_key="os.environ/OPENAI_API_KEY"),
- )
- resources.defer(lambda: endpoints_client.delete_model(model_id))
+ def test_missing_input_returns_error(self, proxy: ProxyClient, resources: ResourceManager) -> None:
+ model = _register(proxy, resources, _openai_params(), prefix="e2e-responses-val")
key = resources.key()
- result = endpoints_client.proxy.transport.send(
+ result = proxy.transport.send(
"/v1/responses",
- headers=endpoints_client.proxy.transport.bearer(key),
+ headers=proxy.transport.bearer(key),
json=_OptionalResponsesBody(model=model),
)
assert_client_error(result, "responses missing input")