fix(stt): prefer explicit xAI API key

This commit is contained in:
Tobias Safaie geb. Schmidt-Philipp 2026-07-14 20:51:54 +01:00 committed by Teknium
parent c0c5dac531
commit d889c980f5
2 changed files with 22 additions and 9 deletions

View file

@ -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")

View file

@ -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 {