From 69b98f9fcacb4e3ac7fd080ade7c0f31670c7d50 Mon Sep 17 00:00:00 2001 From: Kevin Veen-Birkenbach Date: Wed, 23 Sep 2026 17:22:22 +0200 Subject: [PATCH] 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 _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) --- roles/svc-ai-litellm/templates/config.yaml.j2 | 2 +- roles/svc-ai-litellm/templates/test.env.j2 | 2 +- roles/svc-ai-litellm/vars/main.yml | 2 + .../templates/test_config_model_list.py | 4 +- .../roles/svc-ai-litellm/vars/__init__.py | 3 + .../vars/test_keyed_providers.py | 80 +++++++++++++++++++ 6 files changed, 90 insertions(+), 3 deletions(-) create mode 100644 tests/unit/python/roles/svc-ai-litellm/vars/__init__.py create mode 100644 tests/unit/python/roles/svc-ai-litellm/vars/test_keyed_providers.py diff --git a/roles/svc-ai-litellm/templates/config.yaml.j2 b/roles/svc-ai-litellm/templates/config.yaml.j2 index d7d0180637..3f28472001 100644 --- a/roles/svc-ai-litellm/templates/config.yaml.j2 +++ b/roles/svc-ai-litellm/templates/config.yaml.j2 @@ -1,7 +1,7 @@ {% set _routes = (lookup('config', 'svc-ai-ollama', 'services.ollama.preload_models') if LITELLM_OLLAMA_BACKEND | bool else []) | litellm_model_routes( (lookup('config', 'svc-ai-lmstudio', 'services.lmstudio.preload_models') if LITELLM_LMSTUDIO_BACKEND | bool else []), - (LITELLM_REMOTE_MODELS | selectattr('provider', 'in', LITELLM_KEYED_PROVIDERS) | list), + (LITELLM_REMOTE_MODELS | selectattr('provider', 'in', LITELLM_SERVED_PROVIDERS) | list), ollama_url=OLLAMA_BASE_LOCAL_URL, lmstudio_url=LMSTUDIO_BASE_LOCAL_URL, max_tokens=LITELLM_MAX_OUTPUT_TOKENS, diff --git a/roles/svc-ai-litellm/templates/test.env.j2 b/roles/svc-ai-litellm/templates/test.env.j2 index bd384557a2..9f49e2e53b 100644 --- a/roles/svc-ai-litellm/templates/test.env.j2 +++ b/roles/svc-ai-litellm/templates/test.env.j2 @@ -1,7 +1,7 @@ {%- set _ollama_models = (lookup('config', 'svc-ai-ollama', 'services.ollama.preload_models') | map(attribute='alias') | list) if LITELLM_TEST_CONTRACT.ollama | bool else [] -%} {%- set _lmstudio_declared = lookup('config', 'svc-ai-lmstudio', 'services.lmstudio.preload_models') | map(attribute='alias') | list -%} {%- set _lmstudio_only = _lmstudio_declared | reject('in', _ollama_models) | list -%} -{%- set _remote = LITELLM_REMOTE_MODELS | selectattr('provider', 'in', LITELLM_KEYED_PROVIDERS) | map(attribute='alias') | list -%} +{%- set _remote = LITELLM_REMOTE_MODELS | selectattr('provider', 'in', LITELLM_SERVED_PROVIDERS) | map(attribute='alias') | list -%} {%- set _expected = _ollama_models + (_lmstudio_only if LITELLM_TEST_CONTRACT.lmstudio | bool else []) + _remote -%} LITELLM_CONTAINER={{ CLI_LOCAL_CID }} LITELLM_PORT={{ LITELLM_PORT }} diff --git a/roles/svc-ai-litellm/vars/main.yml b/roles/svc-ai-litellm/vars/main.yml index ff54c0a1cd..1e025242b2 100644 --- a/roles/svc-ai-litellm/vars/main.yml +++ b/roles/svc-ai-litellm/vars/main.yml @@ -14,6 +14,8 @@ LITELLM_PROVIDER_KEYS: anthropic: "{{ lookup('config', application_id, 'secrets.credentials.anthropic_api_key') }}" openrouter: "{{ lookup('config', application_id, 'secrets.credentials.openrouter_api_key') }}" LITELLM_KEYED_PROVIDERS: "{{ LITELLM_PROVIDER_KEYS | dict2items | selectattr('value') | map(attribute='key') | list }}" + +LITELLM_SERVED_PROVIDERS: "{{ LITELLM_KEYED_PROVIDERS + [AI_MOCK_PROVIDER] }}" LITELLM_REMOTE_MODELS: "{{ lookup('config', application_id, 'services.litellm.remote_models') }}" LITELLM_UI_ROLE: "web-app-litellm" LITELLM_UI_SSO: "{{ (LITELLM_UI_ROLE in group_names) and (lookup('sso', LITELLM_UI_ROLE, 'is_oidc_native') | bool) }}" diff --git a/tests/unit/python/roles/svc-ai-litellm/templates/test_config_model_list.py b/tests/unit/python/roles/svc-ai-litellm/templates/test_config_model_list.py index 2540c91cd5..3e7348e92f 100644 --- a/tests/unit/python/roles/svc-ai-litellm/templates/test_config_model_list.py +++ b/tests/unit/python/roles/svc-ai-litellm/templates/test_config_model_list.py @@ -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 diff --git a/tests/unit/python/roles/svc-ai-litellm/vars/__init__.py b/tests/unit/python/roles/svc-ai-litellm/vars/__init__.py new file mode 100644 index 0000000000..9f69c70b47 --- /dev/null +++ b/tests/unit/python/roles/svc-ai-litellm/vars/__init__.py @@ -0,0 +1,3 @@ +from pathlib import Path + +PROJECT_ROOT: Path = Path(__file__).resolve().parents[6] diff --git a/tests/unit/python/roles/svc-ai-litellm/vars/test_keyed_providers.py b/tests/unit/python/roles/svc-ai-litellm/vars/test_keyed_providers.py new file mode 100644 index 0000000000..8fdcc1fadc --- /dev/null +++ b/tests/unit/python/roles/svc-ai-litellm/vars/test_keyed_providers.py @@ -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 _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()