From d889c980f5cdd7e63ab8882b6f36dea9906aae7c Mon Sep 17 00:00:00 2001 From: "Tobias Safaie geb. Schmidt-Philipp" Date: Tue, 14 Jul 2026 20:51:54 +0100 Subject: [PATCH] fix(stt): prefer explicit xAI API key --- .../tools/test_transcription_dotenv_fallback.py | 15 +++++++-------- tools/transcription_tools.py | 16 +++++++++++++++- 2 files changed, 22 insertions(+), 9 deletions(-) diff --git a/tests/tools/test_transcription_dotenv_fallback.py b/tests/tools/test_transcription_dotenv_fallback.py index 6684e174fb6..3d9f98c52bd 100644 --- a/tests/tools/test_transcription_dotenv_fallback.py +++ b/tests/tools/test_transcription_dotenv_fallback.py @@ -205,13 +205,7 @@ class TestTranscribeCallSitesReadDotenv: assert seen_keys == ["mistral-dotenv-key"] def test_transcribe_xai_forwards_dotenv_key(self): - """xAI STT now resolves credentials through ``tools.xai_http`` so the - OAuth bearer wins when present and ``XAI_API_KEY`` is the fallback. - Patch the resolver's ``get_env_value`` to simulate a dotenv-only key - and confirm it reaches the HTTP call. The per-call-site - ``transcription_tools.get_env_value`` is still consulted for the - ``XAI_STT_BASE_URL`` override (covered by ``test_custom_base_url``). - """ + """An explicit XAI_API_KEY must win over Grok subscription OAuth for STT.""" from tools import transcription_tools as tt from tools import xai_http @@ -231,7 +225,12 @@ class TestTranscribeCallSitesReadDotenv: return "xai-dotenv-key" return None - with patch.object(xai_http, "get_env_value", side_effect=fake_get_env_value), \ + with patch.object(tt, "get_env_value", side_effect=fake_get_env_value), \ + patch.object(xai_http, "resolve_xai_http_credentials", return_value={ + "provider": "xai-oauth", + "api_key": "subscription-oauth-token", + "base_url": "https://api.x.ai/v1", + }), \ patch("requests.post", side_effect=fake_post), \ patch("builtins.open", MagicMock()): result = tt._transcribe_xai("/tmp/fake.mp3", "grok-stt") diff --git a/tools/transcription_tools.py b/tools/transcription_tools.py index 500f521df3d..a8880acbc9d 100644 --- a/tools/transcription_tools.py +++ b/tools/transcription_tools.py @@ -1794,7 +1794,21 @@ def _transcribe_xai(file_path: str, model_name: str) -> Dict[str, Any]: """ from tools.xai_http import resolve_xai_http_credentials - creds = resolve_xai_http_credentials() + # STT is an API-billed endpoint. Prefer the explicit XAI_API_KEY over the + # general xAI OAuth/Grok-subscription credential; subscription OAuth may be + # valid for Grok while returning personal-team spending-limit errors for + # /v1/stt. Other xAI integrations keep their existing resolver precedence. + direct_api_key = str(get_env_value("XAI_API_KEY") or "").strip() + if direct_api_key: + creds = { + "provider": "xai", + "api_key": direct_api_key, + "base_url": str( + get_env_value("XAI_BASE_URL") or "https://api.x.ai/v1" + ).strip().rstrip("/"), + } + else: + creds = resolve_xai_http_credentials() api_key = str(creds.get("api_key") or "").strip() if not api_key: return {