Speakr is a personal, self-hosted web application designed for transcribing audio recordings
Python
No language model client unless one is configured (#403 follow-up)
Murtaza Nasir192c878052d3
diff --git a/src/config/app_config.py b/src/config/app_config.pyindex 821ce331..af8c3f4d 100644--- a/src/config/app_config.py+++ b/src/config/app_config.py@@ -155,8 +155,10 @@ def initialize_config(app):client = Nonetry:+ from src.services.llm import llm_timeout, LLM_MAX_RETRIES, TEXT_MODEL_CONFIGURED+ if not TEXT_MODEL_CONFIGURED:+ raise RuntimeError("no text model configured (set TEXT_MODEL_BASE_URL, and TEXT_MODEL_API_KEY for a hosted service)")api_key = TEXT_MODEL_API_KEY or "not-needed"- from src.services.llm import llm_timeout, LLM_MAX_RETRIESclient = OpenAI(api_key=api_key, base_url=TEXT_MODEL_BASE_URL, http_client=http_client_no_proxy, timeout=llm_timeout, max_retries=LLM_MAX_RETRIES)app.logger.info(f"LLM client initialized: {TEXT_MODEL_BASE_URL} / {TEXT_MODEL_NAME}")except Exception as e:diff --git a/src/services/llm.py b/src/services/llm.pyindex 7c4f3a31..1ac19315 100644--- a/src/services/llm.py+++ b/src/services/llm.py@@ -153,8 +153,15 @@ http_client_no_proxy = httpx.Client(headers=app_headers)-# Create client with placeholder key if not provided (allows app to start)+# A language model is configured when it has a key or its server address was+# set explicitly; a local server often needs no key (#403). Without either,+# no client is created and nothing is sent: the default address is a hosted+# service, and a transcript must never go there unasked.+TEXT_MODEL_CONFIGURED = bool(TEXT_MODEL_API_KEY) or bool(os.environ.get("TEXT_MODEL_BASE_URL", "").split('#')[0].strip())+try:+ if not TEXT_MODEL_CONFIGURED:+ raise RuntimeError("no text model configured (set TEXT_MODEL_BASE_URL, and TEXT_MODEL_API_KEY for a hosted service)")api_key = TEXT_MODEL_API_KEY or "not-needed"client = OpenAI(api_key=api_key,@@ -180,6 +187,8 @@ def _build_chat_client():chat_config = get_chat_config()if (chat_config['api_key'], chat_config['base_url']) == (TEXT_MODEL_API_KEY, TEXT_MODEL_BASE_URL):return client+ if not (CHAT_MODEL_API_KEY or CHAT_MODEL_BASE_URL or TEXT_MODEL_CONFIGURED):+ return Nonededicated = OpenAI(api_key=chat_config['api_key'] or "not-needed",base_url=chat_config['base_url'],@@ -245,7 +254,7 @@ def call_llm_completion(messages, temperature=0.7, response_format=None, stream=OpenAI completion object or generator (if streaming)"""if not client:- raise ValueError("LLM client not initialized")+ raise ValueError("No text model is configured. Set TEXT_MODEL_BASE_URL, and TEXT_MODEL_API_KEY for a hosted service.")# Check budget before making the callif user_id and operation_type:diff --git a/tests/test_cov_llm.py b/tests/test_cov_llm.pyindex bfde963f..2535778a 100644--- a/tests/test_cov_llm.py+++ b/tests/test_cov_llm.py@@ -183,7 +183,7 @@ def test_get_chat_config_base_url_falls_back_to_text(monkeypatch):def test_call_llm_completion_no_client(monkeypatch):monkeypatch.setattr(llm, "client", None)- with pytest.raises(ValueError, match="not initialized"):+ with pytest.raises(ValueError, match="No text model is configured"):call_llm_completion([{"role": "user", "content": "hi"}])diff --git a/tests/test_keyless_llm.py b/tests/test_keyless_llm.pyindex 27143386..6c3be734 100644--- a/tests/test_keyless_llm.py+++ b/tests/test_keyless_llm.py@@ -1,5 +1,9 @@"""Language models without an API key (#403).+With neither a key nor an explicit server address, no model is configured:+no client is created, so nothing (no transcript) is sent to the default+hosted address, and model steps report that no model is configured.+A local server (Ollama, vLLM, llama.cpp) often runs without a key. Everymodel call refused to run when TEXT_MODEL_API_KEY was empty, although theclient was built with a placeholder for exactly this case, and the chat@@ -74,3 +78,38 @@ def test_a_chat_model_with_its_own_key(env):def test_a_chat_name_alone_keeps_the_text_model(env):env(text_key="sk-text", chat_name="chat-model")assert llm.get_chat_config()["model_name"] == "text-model"+++_PROBE = (+ "import json, src.services.llm as l; "+ "print(json.dumps([l.TEXT_MODEL_CONFIGURED, l.client is not None, l.chat_client is not None]))"+)+++def _clients(**env):+ import json+ import subprocess+ root = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))+ clean = {k: v for k, v in os.environ.items()+ if not k.startswith(("TEXT_MODEL_", "CHAT_MODEL_"))}+ clean.update(env)+ out = subprocess.run([sys.executable, "-c", _PROBE], cwd=root, env=clean,+ capture_output=True, text=True, timeout=120)+ assert out.returncode == 0, out.stderr[-2000:]+ return json.loads(out.stdout.strip().splitlines()[-1])++[email protected]("env,expected", [+ ({}, [False, False, False]), # nothing set: nothing is sent+ ({"TEXT_MODEL_BASE_URL": "http://llm.lan/v1"}, [True, True, True]), # local server, no key+ ({"TEXT_MODEL_API_KEY": "sk-x"}, [True, True, True]), # hosted default with a key+ ({"CHAT_MODEL_NAME": "c", "CHAT_MODEL_BASE_URL": "http://chat.lan/v1"}, [False, False, True]),+])+def test_a_model_is_configured_only_when_set(env, expected):+ assert _clients(**env) == expected+++def test_without_a_model_the_call_says_so(monkeypatch):+ monkeypatch.setattr(llm, "client", None)+ with pytest.raises(ValueError, match="No text model is configured"):+ llm.call_llm_completion([{"role": "user", "content": "hi"}])