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
chore: merge litellm_internal_staging into litellm_forward_bedrock_response_headers
Devin AI58c4fa6ae983
diff --cc tests/test_litellm/llms/custom_httpx/test_llm_http_handler.pyindex c568b82ebba,b78b313e05c..c87abbd8bc4--- a/tests/test_litellm/llms/custom_httpx/test_llm_http_handler.py+++ b/tests/test_litellm/llms/custom_httpx/test_llm_http_handler.py@@@ -2144,228 -2073,77 +2144,304 @@@ async def test_anthropic_invalid_thinkiassert retry_authorization != first_attempt_headers["Authorization"]+def _make_stub_direct_vector_store_config(response):+ from litellm.llms.base_llm.vector_store.transformation import (+ BaseDirectVectorStoreConfig,+ )++ class StubDirectVectorStoreConfig(BaseDirectVectorStoreConfig):+ def __init__(self):+ super().__init__()+ self.sync_calls = []+ self.async_calls = []++ def execute_search_vector_store_request(self, **kwargs):+ self.sync_calls.append(kwargs)+ return response++ async def aexecute_search_vector_store_request(self, **kwargs):+ self.async_calls.append(kwargs)+ return response++ return StubDirectVectorStoreConfig()+++def test_vector_store_search_handler_direct_config_sync_skips_http():+ handler = BaseLLMHTTPHandler()+ stub_response = {"object": "vector_store.search_results.page", "search_query": "q", "data": []}+ config = _make_stub_direct_vector_store_config(stub_response)+ logging_obj = Mock()++ with patch("litellm.llms.custom_httpx.llm_http_handler._get_httpx_client") as mock_get_client:+ result = handler.vector_store_search_handler(+ vector_store_id="vs_direct",+ query="q",+ vector_store_search_optional_params={"max_num_results": 4},+ vector_store_provider_config=config,+ custom_llm_provider="valkey",+ litellm_params=GenericLiteLLMParams(valkey_host="localhost"),+ logging_obj=logging_obj,+ timeout=12.5,+ _is_async=False,+ )++ assert result is stub_response+ mock_get_client.assert_not_called()+ assert len(config.sync_calls) == 1+ call = config.sync_calls[0]+ assert call["vector_store_id"] == "vs_direct"+ assert call["query"] == "q"+ assert call["timeout"] == 12.5+ assert call["vector_store_search_optional_params"] == {"max_num_results": 4}+ assert isinstance(call["litellm_params"], dict)+ assert call["litellm_params"]["valkey_host"] == "localhost"+ pre_call_args = logging_obj.pre_call.call_args.kwargs["additional_args"]+ assert pre_call_args["query"] == "q"+ assert pre_call_args["vector_store_id"] == "vs_direct"+++async def test_vector_store_search_handler_direct_config_async_skips_http():+ handler = BaseLLMHTTPHandler()+ stub_response = {"object": "vector_store.search_results.page", "search_query": "q", "data": []}+ config = _make_stub_direct_vector_store_config(stub_response)+ logging_obj = Mock()++ with patch("litellm.llms.custom_httpx.llm_http_handler.get_async_httpx_client") as mock_get_client:+ result = await handler.vector_store_search_handler(+ vector_store_id="vs_direct",+ query=["q1", "q2"],+ vector_store_search_optional_params={},+ vector_store_provider_config=config,+ custom_llm_provider="valkey",+ litellm_params=GenericLiteLLMParams(valkey_host="localhost"),+ logging_obj=logging_obj,+ timeout=7.0,+ _is_async=True,+ )++ assert result is stub_response+ mock_get_client.assert_not_called()+ assert len(config.async_calls) == 1+ assert config.async_calls[0]["query"] == ["q1", "q2"]+ assert config.async_calls[0]["litellm_params"]["valkey_host"] == "localhost"+ assert config.async_calls[0]["timeout"] == 7.0+ pre_call_args = logging_obj.pre_call.call_args.kwargs["additional_args"]+ assert pre_call_args["query"] == ["q1", "q2"]+ assert pre_call_args["vector_store_id"] == "vs_direct"+++def _direct_vector_store_debug_logging_obj():+ from litellm.litellm_core_utils.litellm_logging import Logging as LitellmLogging++ logging_obj = LitellmLogging(+ model="valkey",+ messages=[{"role": "user", "content": "q"}],+ stream=False,+ call_type="vector_store_search",+ start_time=time.time(),+ litellm_call_id="vs-debug-call-id",+ function_id="vs-debug-function-id",+ log_raw_request_response=True,+ )+ logging_obj.update_environment_variables(+ model="valkey",+ optional_params={"vector_store_id": "vs_direct", "query": "q"},+ litellm_params={+ "litellm_call_id": "vs-debug-call-id",+ "vector_store_id": "vs_direct",+ "litellm_request_debug": True,+ "metadata": {"user_api_key_alias": "vs-test-key"},+ "valkey_host": "valkey.internal",+ "valkey_password": "sup3r-s3cret-valkey-pw",+ "litellm_embedding_config": {"api_key": "sk-embedding-s3cret"},+ },+ )+ return logging_obj++[email protected]("is_async", [False, True])+def test_direct_vector_store_search_debug_log_omits_stored_credentials(caplog, is_async):+ """Regression: an empty api_base made pre_call dump the whole model_call_details, so every+ search shipped the stored valkey_password / embedding api_key into the raw_request metadata."""+ handler = BaseLLMHTTPHandler()+ stub_response = {"object": "vector_store.search_results.page", "search_query": "q", "data": []}+ config = _make_stub_direct_vector_store_config(stub_response)+ logging_obj = _direct_vector_store_debug_logging_obj()++ with caplog.at_level(logging.DEBUG, logger=verbose_logger.name):+ result = handler.vector_store_search_handler(+ vector_store_id="vs_direct",+ query="q",+ vector_store_search_optional_params={"max_num_results": 4},+ vector_store_provider_config=config,+ custom_llm_provider="valkey",+ litellm_params=GenericLiteLLMParams(+ valkey_host="valkey.internal",+ valkey_password="sup3r-s3cret-valkey-pw",+ ),+ logging_obj=logging_obj,+ _is_async=is_async,+ )+ if is_async:+ result = asyncio.run(result)++ assert result is stub_response+ raw_request = logging_obj.model_call_details["litellm_params"]["metadata"]["raw_request"]+ assert "sup3r-s3cret-valkey-pw" not in raw_request+ assert "sk-embedding-s3cret" not in raw_request+ assert "valkey://vs_direct" in raw_request+ logged = "\n".join(record.getMessage() for record in caplog.records)+ assert "sup3r-s3cret-valkey-pw" not in logged+ assert "sk-embedding-s3cret" not in logged+++async def test_async_anthropic_messages_handler_carries_deployment_vertex_location_for_pricing(monkeypatch):+ """+ The proxy pre-creates the logging object before the router picks a deployment, so the+ native /v1/messages path must copy the deployment's vertex_location into the logging+ params it updates; otherwise cost resolution falls back to the environment and every+ call on this surface prices with the regional uplift (#34393).+ """+ import contextlib+ from datetime import datetime++ from litellm.litellm_core_utils.litellm_logging import (+ Logging,+ _resolve_vertex_location_for_cost,+ )++ monkeypatch.setenv("VERTEXAI_LOCATION", "us-east5")+ monkeypatch.setattr(litellm, "vertex_location", None)++ handler = BaseLLMHTTPHandler()++ async def logging_obj_after_handler(generic_params):+ logging_obj = Logging(+ model="vertex_ai/claude-haiku-4-5@20251001",+ messages=[{"role": "user", "content": "hi"}],+ stream=False,+ call_type="anthropic_messages",+ start_time=datetime.now(),+ litellm_call_id="vertex-messages-location",+ function_id="f",+ )+ logging_obj.update_environment_variables(+ model="vertex_ai/claude-haiku-4-5@20251001",+ user="",+ optional_params={},+ litellm_params={"api_base": ""},+ custom_llm_provider="vertex_ai",+ )+ mock_config = Mock()+ mock_config.validate_anthropic_messages_environment = Mock(+ return_value=({"authorization": "Bearer t"}, "https://us-east5-aiplatform.googleapis.com")+ )+ mock_config.transform_anthropic_messages_request = Mock(+ return_value={"model": "claude-haiku-4-5@20251001", "messages": []}+ )+ with contextlib.suppress(Exception):+ await handler.async_anthropic_messages_handler(+ model="claude-haiku-4-5@20251001",+ messages=[{"role": "user", "content": "hi"}],+ anthropic_messages_provider_config=mock_config,+ anthropic_messages_optional_request_params={"max_tokens": 10},+ custom_llm_provider="vertex_ai",+ litellm_params=generic_params,+ logging_obj=logging_obj,+ client=AsyncMock(),+ kwargs={},+ )+ return logging_obj++ global_deployment = await logging_obj_after_handler(GenericLiteLLMParams(vertex_location="global"))+ assert global_deployment.litellm_params["vertex_location"] == "global"+ assert (+ _resolve_vertex_location_for_cost(+ custom_llm_provider="vertex_ai",+ litellm_params=global_deployment.litellm_params,+ optional_params=global_deployment.optional_params,+ model="claude-haiku-4-5@20251001",+ )+ == "global"+ )++ unconfigured_deployment = await logging_obj_after_handler(GenericLiteLLMParams())+ assert "vertex_location" not in unconfigured_deployment.litellm_params+++++ _GENERIC_STREAM_SSE = (+ b'data: {"id":"chatcmpl-1","object":"chat.completion.chunk","created":1,'+ b'"model":"test-model","choices":[{"index":0,"delta":{"content":"hi"},'+ b'"finish_reason":null}]}\n\n'+ b"data: [DONE]\n\n"+ )+++ def _generic_stream_upstream_response() -> httpx.Response:+ return httpx.Response(+ 200,+ headers={+ "x-request-id": "generic-req-123",+ "x-ratelimit-remaining-requests": "42",+ },+ content=_GENERIC_STREAM_SSE,+ request=httpx.Request("POST", "https://fake-vllm.test/v1/chat/completions"),+ )+++ def test_generic_http_handler_sync_streaming_forwards_provider_response_headers():+ """+ Regression test for the generic BaseLLMHTTPHandler streaming path used by+ ~30 providers (deepseek, groq, hosted_vllm, databricks, openrouter, ...).++ The sync `completion()` streaming branch builds the CustomStreamWrapper from+ `make_sync_call`, which returns the upstream response headers alongside the+ stream. Those headers must reach the caller as `llm_provider-*` entries in+ `_hidden_params["additional_headers"]`, which is what the proxy merges into+ the client-facing response headers.+ """+ mock_client = Mock(spec=HTTPHandler)+ mock_client.post = Mock(return_value=_generic_stream_upstream_response())++ response = litellm.completion(+ model="hosted_vllm/test-model",+ messages=[{"role": "user", "content": "Hello"}],+ api_base="https://fake-vllm.test/v1",+ api_key="sk-test",+ stream=True,+ client=mock_client,+ )++ additional_headers = response._hidden_params["additional_headers"]+ assert additional_headers["llm_provider-x-request-id"] == "generic-req-123"+ assert additional_headers["llm_provider-x-ratelimit-remaining-requests"] == "42"++ assert "".join([chunk.choices[0].delta.content or "" for chunk in response]) == "hi"+++ @pytest.mark.asyncio+ async def test_generic_http_handler_async_streaming_forwards_provider_response_headers():+ """+ Companion to the sync test above for `acompletion_stream_function`, which+ builds its CustomStreamWrapper from `make_async_call_stream_helper`.+ """+ mock_client = AsyncMock(spec=AsyncHTTPHandler)+ mock_client.post = AsyncMock(return_value=_generic_stream_upstream_response())++ response = await litellm.acompletion(+ model="hosted_vllm/test-model",+ messages=[{"role": "user", "content": "Hello"}],+ api_base="https://fake-vllm.test/v1",+ api_key="sk-test",+ stream=True,+ client=mock_client,+ )++ additional_headers = response._hidden_params["additional_headers"]+ assert additional_headers["llm_provider-x-request-id"] == "generic-req-123"+ assert additional_headers["llm_provider-x-ratelimit-remaining-requests"] == "42"++ collected = [chunk async for chunk in response]+ assert "".join([chunk.choices[0].delta.content or "" for chunk in collected]) == "hi"