Skip to content

Commit 73e99b6

Browse files
committed
fix: add token to the openai request
OpenAIMixin expects to use an API key and creates its own AsyncOpenAI client. So our code now authenticate with the Google service, retrieves a token and pass it to the OpenAI client. Falls back to an empty string if credentials can't be obtained (letting LiteLLM handle ADC directly). Signed-off-by: Sébastien Han <[email protected]>
1 parent 3442f88 commit 73e99b6

File tree

2 files changed

+28
-5
lines changed

2 files changed

+28
-5
lines changed

llama_stack/providers/remote/inference/vertexai/vertexai.py

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66

77
from typing import Any
88

9+
import google.auth.transport.requests
10+
from google.auth import default
11+
912
from llama_stack.apis.inference import ChatCompletionRequest
1013
from llama_stack.providers.utils.inference.litellm_openai_mixin import (
1114
LiteLLMOpenAIMixin,
@@ -28,12 +31,29 @@ def __init__(self, config: VertexAIConfig) -> None:
2831
self.config = config
2932

3033
def get_api_key(self) -> str:
31-
# Vertex AI doesn't use API keys, it uses Application Default Credentials
32-
# Return empty string to let litellm handle authentication via ADC
33-
return ""
34+
"""
35+
Get an access token for Vertex AI using Application Default Credentials.
36+
37+
Vertex AI uses ADC instead of API keys. This method obtains an access token
38+
from the default credentials and returns it for use with the OpenAI-compatible client.
39+
"""
40+
try:
41+
# Get default credentials - will read from GOOGLE_APPLICATION_CREDENTIALS
42+
credentials, _ = default(scopes=["https://www.googleapis.com/auth/cloud-platform"])
43+
credentials.refresh(google.auth.transport.requests.Request())
44+
return credentials.token
45+
except Exception:
46+
# If we can't get credentials, return empty string to let LiteLLM handle it
47+
# This allows the LiteLLM mixin to work with ADC directly
48+
return ""
49+
50+
def get_base_url(self) -> str:
51+
"""
52+
Get the Vertex AI OpenAI-compatible API base URL.
3453
35-
def get_base_url(self):
36-
# source - https://cloud.google.com/vertex-ai/generative-ai/docs/start/openai
54+
Returns the Vertex AI OpenAI-compatible endpoint URL.
55+
Source: https://cloud.google.com/vertex-ai/generative-ai/docs/start/openai
56+
"""
3757
return f"https://{self.config.location}-aiplatform.googleapis.com/v1/projects/{self.config.project}/locations/{self.config.location}/endpoints/openapi"
3858

3959
async def _get_params(self, request: ChatCompletionRequest) -> dict[str, Any]:

tests/integration/inference/test_openai_completion.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,9 @@ def skip_if_doesnt_support_n(client_with_models, model_id):
7676
"remote::gemini",
7777
# https://docs.anthropic.com/en/api/openai-sdk#simple-fields
7878
"remote::anthropic",
79+
"remote::vertexai",
80+
# Error code: 400 - [{'error': {'code': 400, 'message': 'Unable to submit request because candidateCount must be 1 but
81+
# the entered value was 2. Update the candidateCount value and try again.', 'status': 'INVALID_ARGUMENT'}
7982
):
8083
pytest.skip(f"Model {model_id} hosted by {provider.provider_type} doesn't support n param.")
8184

0 commit comments

Comments
 (0)