Files
hermes-agent/tests/tools/test_stt_language_resolution.py
teknium1 d1152d797c fix(stt): xAI format=true only with a language; accurate model-default notes
xAI's /v1/stt now answers HTTP 400 "Field 'language' is required when
'format' is true", so every transcription with stt.language "" (auto-detect)
failed on both the server path and the Desktop client-direct path. Send the
inverse-text-normalization flag only when a language is resolved; the model
field is still always named. Comments updated: the server default moved from
grok-voice-transcribe-1.0 to 2.0 between Sep 17 and Sep 21, so the pin is
about determinism and a 1.0 rollback knob, not about escaping a 1.0 default.

Live: auto-detect on origin/main -> HTTP 400; with this change -> transcript
returned for 2.0 and pinned 1.0; language=en still sends format=true.

(cherry picked from commit 1ff4887819a12a5e37e9cbea7f31b5cc6b040857)
2026-10-06 12:08:49 -07:00

103 lines
3.6 KiB
Python

"""Tests for the unified STT language resolution (_resolve_stt_language).
Class-level contract: EVERY provider resolves its language hint through one
helper with the order:
stt.<provider>.language > stt.language (global) > HERMES_LOCAL_STT_LANGUAGE > None
Regression coverage for the "STT transcribes the wrong language" issue class
(#55551, #50181 and siblings):
- xAI no longer silently forces "en" when nothing is configured
- the global ``stt.language`` key reaches every provider
- per-provider language still wins over the global key
"""
from unittest.mock import patch
import pytest
from tools.transcription_tools import _resolve_stt_language
@pytest.fixture(autouse=True)
def _clear_lang_env(monkeypatch):
monkeypatch.delenv("HERMES_LOCAL_STT_LANGUAGE", raising=False)
class TestResolveSttLanguage:
def test_provider_language_wins(self):
cfg = {"language": "en", "groq": {"language": "he"}}
assert _resolve_stt_language("groq", cfg) == "he"
def test_global_language_fallback(self):
cfg = {"language": "hu", "groq": {}}
assert _resolve_stt_language("groq", cfg) == "hu"
def test_value_is_stripped(self):
cfg = {"language": " ja "}
assert _resolve_stt_language("local", cfg) == "ja"
class TestXaiNoForcedEnglish:
"""xAI previously defaulted language to "en" — auto-detect must be the default."""
def test_no_language_sent_by_default(self, tmp_path, monkeypatch):
import tools.transcription_tools as tt
audio = tmp_path / "a.ogg"
audio.write_bytes(b"x")
captured = {}
class _Resp:
status_code = 200
@staticmethod
def json():
return {"text": "hola", "language": "es", "duration": 1.0}
import requests as _requests
def fake_post(url, **kwargs):
captured["data"] = kwargs.get("data")
return _Resp()
monkeypatch.setattr(_requests, "post", fake_post)
with patch.object(tt, "_load_stt_config", return_value={}), \
patch("tools.xai_http.resolve_xai_http_credentials",
return_value={"api_key": "xai-test", "base_url": "https://api.x.ai/v1"}):
result = tt._transcribe_xai(str(audio), "grok-stt")
assert result["success"] is True
assert "language" not in (captured["data"] or {}), \
"xAI must auto-detect when no language is configured (was forced to 'en')"
# /v1/stt returns HTTP 400 for format=true without a language, so auto-detect drops it.
assert "format" not in (captured["data"] or {}), \
"format=true must not be sent without a language (xAI rejects it with HTTP 400)"
def test_global_language_reaches_xai(self, tmp_path, monkeypatch):
import tools.transcription_tools as tt
audio = tmp_path / "a.ogg"
audio.write_bytes(b"x")
captured = {}
class _Resp:
status_code = 200
@staticmethod
def json():
return {"text": "ok", "language": "he", "duration": 1.0}
import requests as _requests
monkeypatch.setattr(
_requests, "post",
lambda url, **kw: captured.update(data=kw.get("data")) or _Resp(),
)
with patch.object(tt, "_load_stt_config", return_value={"language": "he"}), \
patch("tools.xai_http.resolve_xai_http_credentials",
return_value={"api_key": "xai-test", "base_url": "https://api.x.ai/v1"}):
result = tt._transcribe_xai(str(audio), "grok-stt")
assert result["success"] is True
assert captured["data"]["language"] == "he"