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
Fix: Header forwarding for embeddings endpoint
Sameer Kankutedbec9c708463
diff --git a/litellm/main.py b/litellm/main.pyindex 6905d8f6a87..f198c14e595 100644--- a/litellm/main.py+++ b/litellm/main.py@@ -4587,8 +4587,8 @@ def embedding( # noqa: PLR0915or get_secret_str("OPENAI_API_KEY"))- if extra_headers is not None:- optional_params["extra_headers"] = extra_headers+ if headers is not None and headers != {}:+ optional_params["extra_headers"] = headersif encoding_format is not None:optional_params["encoding_format"] = encoding_format@@ -4656,8 +4656,8 @@ def embedding( # noqa: PLR0915or get_secret_str("OPENAI_LIKE_API_KEY"))- if extra_headers is not None:- optional_params["extra_headers"] = extra_headers+ if headers is not None and headers != {}:+ optional_params["extra_headers"] = headers## EMBEDDING CALLresponse = openai_like_embedding.embedding(@@ -4681,9 +4681,9 @@ def embedding( # noqa: PLR0915or litellm.api_key)- if extra_headers is not None and isinstance(extra_headers, dict):- headers = extra_headers- else:+ # Use the merged headers variable (already merged at the top of the function)+ # Don't overwrite it with just extra_headers+ if headers is None:headers = {}response = base_llm_http_handler.embedding(diff --git a/tests/test_litellm/proxy/test_litellm_pre_call_utils.py b/tests/test_litellm/proxy/test_litellm_pre_call_utils.pyindex fd39b308a7a..deaa47d9da7 100644--- a/tests/test_litellm/proxy/test_litellm_pre_call_utils.py+++ b/tests/test_litellm/proxy/test_litellm_pre_call_utils.py@@ -1297,3 +1297,145 @@ def test_update_model_if_key_alias_exists():original_model = data["model"]_update_model_if_key_alias_exists(data=data, user_api_key_dict=user_api_key_dict)assert data["model"] == original_model # Should remain unchanged+++async def test_embedding_header_forwarding_with_model_group():+ """+ Test that headers are properly forwarded for embedding requests when+ forward_client_headers_to_llm_api is configured for the model group.++ This test verifies the fix for embedding endpoints not forwarding headers+ similar to how chat completion endpoints do.+ """+ import litellm++ # Setup mock request for embeddings+ request_mock = MagicMock(spec=Request)+ request_mock.url.path = "/v1/embeddings"+ request_mock.url = MagicMock()+ request_mock.url.__str__.return_value = "http://localhost/v1/embeddings"+ request_mock.method = "POST"+ request_mock.query_params = {}+ request_mock.headers = {+ "Content-Type": "application/json",+ "X-Custom-Header": "custom-value",+ "X-Request-ID": "test-request-123",+ "Authorization": "Bearer sk-test-key",+ }+ request_mock.client = MagicMock()+ request_mock.client.host = "127.0.0.1"++ # Setup embedding request data+ data = {+ "model": "local-openai/text-embedding-3-small",+ "input": ["Text to embed"],+ }++ # Setup user API key+ user_api_key_dict = UserAPIKeyAuth(+ api_key="test-key",+ user_id="test-user",+ org_id="test-org",+ )++ # Mock model_group_settings to enable header forwarding for the model+ mock_settings = MagicMock(forward_client_headers_to_llm_api=["local-openai/*"])+ original_model_group_settings = getattr(litellm, "model_group_settings", None)+ litellm.model_group_settings = mock_settings++ try:+ # Call add_litellm_data_to_request which includes header forwarding logic+ updated_data = await add_litellm_data_to_request(+ data=data,+ request=request_mock,+ user_api_key_dict=user_api_key_dict,+ proxy_config=MagicMock(),+ general_settings={},+ version="test-version",+ )++ # Verify that headers were added to the request data+ assert "headers" in updated_data, "Headers should be added to embedding request"++ # Verify that only x- prefixed headers (except x-stainless) were forwarded+ forwarded_headers = updated_data["headers"]+ assert "X-Custom-Header" in forwarded_headers, "X-Custom-Header should be forwarded"+ assert forwarded_headers["X-Custom-Header"] == "custom-value"+ assert "X-Request-ID" in forwarded_headers, "X-Request-ID should be forwarded"+ assert forwarded_headers["X-Request-ID"] == "test-request-123"++ # Verify that authorization header was NOT forwarded (sensitive header)+ assert "Authorization" not in forwarded_headers, "Authorization header should not be forwarded"++ # Verify that Content-Type was NOT forwarded (doesn't start with x-)+ assert "Content-Type" not in forwarded_headers, "Content-Type should not be forwarded"++ # Verify original data fields are preserved+ assert updated_data["model"] == "local-openai/text-embedding-3-small"+ assert updated_data["input"] == ["Text to embed"]++ finally:+ # Restore original model_group_settings+ litellm.model_group_settings = original_model_group_settings+++async def test_embedding_header_forwarding_without_model_group_config():+ """+ Test that headers are NOT forwarded for embedding requests when+ the model is not in the forward_client_headers_to_llm_api list.+ """+ import litellm++ # Setup mock request for embeddings+ request_mock = MagicMock(spec=Request)+ request_mock.url.path = "/v1/embeddings"+ request_mock.url = MagicMock()+ request_mock.url.__str__.return_value = "http://localhost/v1/embeddings"+ request_mock.method = "POST"+ request_mock.query_params = {}+ request_mock.headers = {+ "Content-Type": "application/json",+ "X-Custom-Header": "custom-value",+ }+ request_mock.client = MagicMock()+ request_mock.client.host = "127.0.0.1"++ # Setup embedding request data with a model NOT in the forward list+ data = {+ "model": "text-embedding-ada-002",+ "input": ["Text to embed"],+ }++ user_api_key_dict = UserAPIKeyAuth(+ api_key="test-key",+ user_id="test-user",+ )++ # Mock model_group_settings with a different model in the forward list+ mock_settings = MagicMock(forward_client_headers_to_llm_api=["gpt-4", "claude-*"])+ original_model_group_settings = getattr(litellm, "model_group_settings", None)+ litellm.model_group_settings = mock_settings++ try:+ updated_data = await add_litellm_data_to_request(+ data=data,+ request=request_mock,+ user_api_key_dict=user_api_key_dict,+ proxy_config=MagicMock(),+ general_settings={},+ version="test-version",+ )++ # Verify that headers were NOT added since model is not in forward list+ assert "headers" not in updated_data or updated_data.get("headers") is None, \+ "Headers should not be forwarded for models not in forward_client_headers_to_llm_api list"++ # Verify original data fields are preserved+ assert updated_data["model"] == "text-embedding-ada-002"+ assert updated_data["input"] == ["Text to embed"]++ finally:+ # Restore original model_group_settings+ litellm.model_group_settings = original_model_group_settings