fix(svc-ai-litellm): serve the mock without a key, and separate keyed from served

A deploy warmed the chat model and got HTTP 400. The rendered config in the running container read 'model_list: []' while every name-level variable resolved to mock/deterministic: the alias list had been taught about the mock, the list that decides what is actually published had not.

One list could not answer both questions. config.yaml.j2 and test.env.j2 ask which models are served and want the mock; env.j2 asks which providers hold a key and emits <PROVIDER>_API_KEY by indexing LITELLM_PROVIDER_KEYS, so a keyless entry there kills the render. LITELLM_KEYED_PROVIDERS keeps its original meaning and LITELLM_SERVED_PROVIDERS is that set plus the mock.

The new test renders both from the role's own vars and pins the distinction from either side: the mock is served without a key, and the mock is never keyed - the second assertion carries the reason, because that is the one whose violation only shows up at deploy time.

The first attempt at this fix put the mock into the keyed list and would have broken the env render. It was caught by enumerating the variable's consumers before naming a confidence number, not by a test.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
kevinveenbirkenbachandClaude Opus 5 committed 2026-09-23 17:22:22 +02:00
1 parent 00910f0674
commit 69b98f9fca
6 files changed
+90 -3

No files matched your search

@@ -88,7 +88,9 @@ def render(*, ollama=(), lmstudio=(), keys=None, remote_models=REMOTE_MODELS):
LITELLM_MAX_OUTPUT_TOKENS=512,
LITELLM_UPSTREAM_TIMEOUT=60,
LITELLM_REMOTE_MODELS=list(remote_models),
LITELLM_KEYED_PROVIDERS=[name for name, key in (keys or {}).items() if key],
LITELLM_SERVED_PROVIDERS=[
name for name, key in (keys or {}).items() if key
],
)
return yaml.safe_load(
rendered
@@ -0,0 +1,3 @@
from pathlib import Path
PROJECT_ROOT: Path = Path(__file__).resolve().parents[6]
@@ -0,0 +1,80 @@
"""The provider list the gateway actually publishes from.
A model whose provider is missing here is filtered out of the model list, and
the proxy then answers HTTP 400 for it while every name-level variable still
resolves to it. That combination cost a deploy: the chat model resolved to the
mock and the gateway served `model_list: []`.
"""
from __future__ import annotations
import ast
import unittest
from typing import ClassVar
from jinja2 import Environment, StrictUndefined
from utils.cache.yaml import load_yaml
from utils.roles.mapping import ROLE_FILE_VARS_MAIN
from . import PROJECT_ROOT
_ROLE_VARS = PROJECT_ROOT / "roles/svc-ai-litellm" / ROLE_FILE_VARS_MAIN
_MOCK = "mock"
class TestProviderLists(unittest.TestCase):
"""Two lists that mean different things and are easy to confuse."""
source: ClassVar[dict]
@classmethod
def setUpClass(cls):
cls.source = load_yaml(str(_ROLE_VARS))
def _render(self, name, provider_keys, keyed=None):
env = Environment(undefined=StrictUndefined, autoescape=False) # noqa: S701 - renders a Python list literal, not markup
env.filters["dict2items"] = lambda mapping: [
{"key": key, "value": value} for key, value in mapping.items()
]
return ast.literal_eval(
env.from_string(self.source[name]).render(
LITELLM_PROVIDER_KEYS=provider_keys,
LITELLM_KEYED_PROVIDERS=keyed if keyed is not None else [],
AI_MOCK_PROVIDER=_MOCK,
)
)
def _keyed(self, provider_keys):
return self._render("LITELLM_KEYED_PROVIDERS", provider_keys)
def test_a_provider_without_a_key_is_not_keyed(self) -> None:
self.assertNotIn("openai", self._keyed({"openai": "", "anthropic": "sk-x"}))
def test_a_provider_with_a_key_is_keyed(self) -> None:
self.assertIn("anthropic", self._keyed({"openai": "", "anthropic": "sk-x"}))
def test_the_mock_is_never_keyed(self) -> None:
self.assertNotIn(
_MOCK,
self._keyed({"openai": "sk-x"}),
"env.j2 emits <PROVIDER>_API_KEY for every keyed provider by indexing "
"LITELLM_PROVIDER_KEYS, so a keyless entry here fails the render",
)
def test_the_mock_is_served_without_any_key(self) -> None:
self.assertIn(
_MOCK,
self._render("LITELLM_SERVED_PROVIDERS", {}, keyed=[]),
"a mock answers from its own response, so the model list must carry it",
)
def test_a_keyed_provider_is_also_served(self) -> None:
self.assertIn(
"anthropic",
self._render("LITELLM_SERVED_PROVIDERS", {}, keyed=["anthropic"]),
)
if __name__ == "__main__":
unittest.main()