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]

fix(google_genai): forward extra_headers in generateContent adapter (#18935)

Jonathan Hoyt1169be44b5d7
diff --git a/litellm/google_genai/adapters/handler.py b/litellm/google_genai/adapters/handler.py
index 575c36b946a..209e03d2bda 100644
--- a/litellm/google_genai/adapters/handler.py
+++ b/litellm/google_genai/adapters/handler.py
@@ -37,9 +37,14 @@ class GenerateContentToCompletionHandler:
completion_kwargs: Dict[str, Any] = dict(completion_request)
- # feed metadata for custom callback
- if extra_kwargs is not None and "metadata" in extra_kwargs:
- completion_kwargs["metadata"] = extra_kwargs["metadata"]
+ # Forward extra_kwargs that should be passed to completion call
+ if extra_kwargs is not None:
+ # Forward metadata for custom callback
+ if "metadata" in extra_kwargs:
+ completion_kwargs["metadata"] = extra_kwargs["metadata"]
+ # Forward extra_headers for providers that require custom headers (e.g., github_copilot)
+ if "extra_headers" in extra_kwargs:
+ completion_kwargs["extra_headers"] = extra_kwargs["extra_headers"]
if stream:
completion_kwargs["stream"] = stream
diff --git a/litellm/google_genai/main.py b/litellm/google_genai/main.py
index 1dc805a6b54..9ec56c37170 100644
--- a/litellm/google_genai/main.py
+++ b/litellm/google_genai/main.py
@@ -330,6 +330,7 @@ def generate_content(
tools=tools,
_is_async=_is_async,
litellm_params=setup_result.litellm_params,
+ extra_headers=extra_headers,
**kwargs,
)
@@ -422,6 +423,7 @@ async def agenerate_content_stream(
litellm_params=setup_result.litellm_params,
tools=tools,
stream=True,
+ extra_headers=extra_headers,
**kwargs,
)
)
@@ -507,6 +509,7 @@ def generate_content_stream(
_is_async=_is_async,
litellm_params=setup_result.litellm_params,
stream=True,
+ extra_headers=extra_headers,
**kwargs,
)
diff --git a/tests/test_litellm/google_genai/test_google_genai_adapter.py b/tests/test_litellm/google_genai/test_google_genai_adapter.py
index 135881ad209..884e06fdbc0 100644
--- a/tests/test_litellm/google_genai/test_google_genai_adapter.py
+++ b/tests/test_litellm/google_genai/test_google_genai_adapter.py
@@ -1120,12 +1120,13 @@ async def test_google_generate_content_with_openai():
# Print the response for verification
print(f"Response: {response}")
- #########################################################
+ #########################################################
# validate only expected fields were sent to litellm.completion
passed_fields = set(call_kwargs.keys())
# remove any GenericLiteLLMParams fields
passed_fields = passed_fields - set(GenericLiteLLMParams.model_fields.keys())
- assert passed_fields == set(["model", "messages"]), f"Expected only model and messages to be passed through, got {passed_fields}"
+ # extra_headers is now explicitly passed through for providers that need custom headers
+ assert passed_fields == set(["model", "messages", "extra_headers"]), f"Expected model, messages, and extra_headers to be passed through, got {passed_fields}"
@pytest.mark.asyncio
async def test_agenerate_content_x_goog_api_key_header():
"""
diff --git a/tests/test_litellm/google_genai/test_google_genai_adapter_fixes.py b/tests/test_litellm/google_genai/test_google_genai_adapter_fixes.py
index d4d0ba9d44c..b45064003c6 100644
--- a/tests/test_litellm/google_genai/test_google_genai_adapter_fixes.py
+++ b/tests/test_litellm/google_genai/test_google_genai_adapter_fixes.py
@@ -252,7 +252,7 @@ def test_stream_transformation_error_handling():
def test_non_stream_response_when_stream_requested():
"""Test handling of non-stream responses when streaming was requested"""
from litellm.types.utils import Choices
-
+
# Mock a non-stream response (ModelResponse with valid choices)
mock_response = ModelResponse(
id="test-123",
@@ -270,13 +270,13 @@ def test_non_stream_response_when_stream_requested():
model="gpt-3.5-turbo",
object="chat.completion"
)
-
+
# Create an instance of the adapter
adapter = GoogleGenAIAdapter()
-
+
# Test the adapter's translate_completion_to_generate_content method directly
result = adapter.translate_completion_to_generate_content(mock_response)
-
+
# Verify the result is a valid Google GenAI format response
assert "candidates" in result
assert isinstance(result["candidates"], list)
@@ -287,4 +287,70 @@ def test_non_stream_response_when_stream_requested():
assert isinstance(candidate["content"]["parts"], list)
assert len(candidate["content"]["parts"]) > 0
assert "text" in candidate["content"]["parts"][0]
- assert candidate["content"]["parts"][0]["text"] == "Hello, world!"
\ No newline at end of file
+ assert candidate["content"]["parts"][0]["text"] == "Hello, world!"
+
+
+def test_extra_headers_forwarding():
+ """Test that extra_headers is correctly forwarded to completion call.
+
+ This is important for providers like github_copilot that require custom
+ headers (e.g., Editor-Version) for authentication.
+ """
+ # Test that extra_headers is included in completion kwargs
+ model = "gpt-3.5-turbo"
+ contents = {"role": "user", "parts": [{"text": "Test"}]}
+ config = {"temperature": 0.7}
+
+ extra_kwargs = {
+ "extra_headers": {
+ "Editor-Version": "vscode/1.95.0",
+ "Editor-Plugin-Version": "copilot-chat/0.22.4",
+ "Custom-Header": "custom-value"
+ },
+ "metadata": {"user_id": "test-user"}
+ }
+
+ completion_kwargs = GenerateContentToCompletionHandler._prepare_completion_kwargs(
+ model=model,
+ contents=contents,
+ config=config,
+ stream=False,
+ extra_kwargs=extra_kwargs
+ )
+
+ # Verify extra_headers is forwarded
+ assert "extra_headers" in completion_kwargs, "extra_headers should be forwarded to completion call"
+ assert completion_kwargs["extra_headers"]["Editor-Version"] == "vscode/1.95.0"
+ assert completion_kwargs["extra_headers"]["Editor-Plugin-Version"] == "copilot-chat/0.22.4"
+ assert completion_kwargs["extra_headers"]["Custom-Header"] == "custom-value"
+
+ # Verify metadata is also forwarded (existing behavior)
+ assert "metadata" in completion_kwargs
+ assert completion_kwargs["metadata"]["user_id"] == "test-user"
+
+
+def test_extra_headers_not_present():
+ """Test that missing extra_headers doesn't cause issues."""
+ model = "gpt-3.5-turbo"
+ contents = {"role": "user", "parts": [{"text": "Test"}]}
+ config = {"temperature": 0.7}
+
+ # extra_kwargs without extra_headers
+ extra_kwargs = {
+ "metadata": {"user_id": "test-user"}
+ }
+
+ completion_kwargs = GenerateContentToCompletionHandler._prepare_completion_kwargs(
+ model=model,
+ contents=contents,
+ config=config,
+ stream=False,
+ extra_kwargs=extra_kwargs
+ )
+
+ # Verify extra_headers is not present (no error)
+ assert "extra_headers" not in completion_kwargs
+
+ # Verify metadata is still forwarded
+ assert "metadata" in completion_kwargs
+ assert completion_kwargs["metadata"]["user_id"] == "test-user"
\ No newline at end of file