Skip to content

Commit cef6167

Browse files
committed
feat: Add clear error message when API key is missing
Improve user experience by providing specific guidance when no API key is available, showing both provider data header and config options with the correct field name for each provider. Also adds comprehensive test coverage for API key resolution scenarios.
1 parent 5c33bc1 commit cef6167

File tree

2 files changed

+118
-0
lines changed

2 files changed

+118
-0
lines changed

llama_stack/providers/utils/inference/litellm_openai_mixin.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,12 @@ def get_api_key(self) -> str:
254254
api_key = getattr(provider_data, key_field)
255255
else:
256256
api_key = self.api_key_from_config
257+
if not api_key:
258+
raise ValueError(
259+
"API key is not set. Please provide a valid API key in the "
260+
"provider data header, e.g. x-llamastack-provider-data: "
261+
f'{{"{key_field}": "<API_KEY>"}}, or in the provider config.'
262+
)
257263
return api_key
258264

259265
async def embeddings(
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
# Copyright (c) Meta Platforms, Inc. and affiliates.
2+
# All rights reserved.
3+
#
4+
# This source code is licensed under the terms described in the LICENSE file in
5+
# the root directory of this source tree.
6+
7+
import json
8+
from unittest.mock import MagicMock
9+
10+
import pytest
11+
from pydantic import BaseModel, Field
12+
13+
from llama_stack.core.request_headers import request_provider_data_context
14+
from llama_stack.providers.utils.inference.litellm_openai_mixin import LiteLLMOpenAIMixin
15+
16+
17+
# Test fixtures and helper classes
18+
class TestConfig(BaseModel):
19+
api_key: str | None = Field(default=None)
20+
21+
22+
class TestProviderDataValidator(BaseModel):
23+
test_api_key: str | None = Field(default=None)
24+
25+
26+
class TestLiteLLMAdapter(LiteLLMOpenAIMixin):
27+
def __init__(self, config: TestConfig):
28+
super().__init__(
29+
model_entries=[],
30+
litellm_provider_name="test",
31+
api_key_from_config=config.api_key,
32+
provider_data_api_key_field="test_api_key",
33+
openai_compat_api_base=None,
34+
)
35+
36+
37+
@pytest.fixture
38+
def adapter_with_config_key():
39+
"""Fixture to create adapter with API key in config"""
40+
config = TestConfig(api_key="config-api-key")
41+
adapter = TestLiteLLMAdapter(config)
42+
adapter.__provider_spec__ = MagicMock()
43+
adapter.__provider_spec__.provider_data_validator = (
44+
"tests.unit.providers.inference.test_litellm_openai_mixin.TestProviderDataValidator"
45+
)
46+
return adapter
47+
48+
49+
@pytest.fixture
50+
def adapter_without_config_key():
51+
"""Fixture to create adapter without API key in config"""
52+
config = TestConfig(api_key=None)
53+
adapter = TestLiteLLMAdapter(config)
54+
adapter.__provider_spec__ = MagicMock()
55+
adapter.__provider_spec__.provider_data_validator = (
56+
"tests.unit.providers.inference.test_litellm_openai_mixin.TestProviderDataValidator"
57+
)
58+
return adapter
59+
60+
61+
def test_api_key_from_config_when_no_provider_data(adapter_with_config_key):
62+
"""Test that adapter uses config API key when no provider data is available"""
63+
api_key = adapter_with_config_key.get_api_key()
64+
assert api_key == "config-api-key"
65+
66+
67+
def test_provider_data_takes_priority_over_config(adapter_with_config_key):
68+
"""Test that provider data API key overrides config API key"""
69+
with request_provider_data_context(
70+
{"x-llamastack-provider-data": json.dumps({"test_api_key": "provider-data-key"})}
71+
):
72+
api_key = adapter_with_config_key.get_api_key()
73+
assert api_key == "provider-data-key"
74+
75+
76+
def test_fallback_to_config_when_provider_data_missing_key(adapter_with_config_key):
77+
"""Test fallback to config when provider data doesn't have the required key"""
78+
with request_provider_data_context({"x-llamastack-provider-data": json.dumps({"wrong_key": "some-value"})}):
79+
api_key = adapter_with_config_key.get_api_key()
80+
assert api_key == "config-api-key"
81+
82+
83+
def test_error_when_no_api_key_available(adapter_without_config_key):
84+
"""Test that ValueError is raised when neither config nor provider data have API key"""
85+
with pytest.raises(ValueError, match="API key is not set"):
86+
adapter_without_config_key.get_api_key()
87+
88+
89+
def test_error_when_provider_data_has_wrong_key(adapter_without_config_key):
90+
"""Test that ValueError is raised when provider data exists but doesn't have required key"""
91+
with request_provider_data_context({"x-llamastack-provider-data": json.dumps({"wrong_key": "some-value"})}):
92+
with pytest.raises(ValueError, match="API key is not set"):
93+
adapter_without_config_key.get_api_key()
94+
95+
96+
def test_provider_data_works_when_config_is_none(adapter_without_config_key):
97+
"""Test that provider data works even when config has no API key"""
98+
with request_provider_data_context(
99+
{"x-llamastack-provider-data": json.dumps({"test_api_key": "provider-only-key"})}
100+
):
101+
api_key = adapter_without_config_key.get_api_key()
102+
assert api_key == "provider-only-key"
103+
104+
105+
def test_error_message_includes_correct_field_names(adapter_without_config_key):
106+
"""Test that error message includes correct field name and header information"""
107+
try:
108+
adapter_without_config_key.get_api_key()
109+
raise AssertionError("Should have raised ValueError")
110+
except ValueError as e:
111+
assert "test_api_key" in str(e) # Should mention the correct field name
112+
assert "x-llamastack-provider-data" in str(e) # Should mention header name

0 commit comments

Comments
 (0)