Skip to content

Commit ef941e6

Browse files
authored
Merge pull request chigwell#229 from omid-io/fix/groq-oga-audio-transcription
fix(transcription): normalize oga audio extension to ogg for Groq uploads
2 parents c6e6472 + c8afb0b commit ef941e6

2 files changed

Lines changed: 49 additions & 2 deletions

File tree

‎telegram_mcp/transcription.py‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,11 @@ async def _transcribe_via_groq(cl, msg) -> dict:
362362
return _too_large_for_groq(size, limit)
363363

364364
file_attr = getattr(msg, "file", None)
365-
ext = (getattr(file_attr, "ext", None) or ".oga").lstrip(".")
365+
ext = (getattr(file_attr, "ext", None) or "ogg").lstrip(".")
366+
# Telegram voice notes carry 'oga' (Opus-in-Ogg), which Groq rejects with 400
367+
# unsupported_audio_format. Groq requires .ogg, so normalize oga -> ogg.
368+
if ext.lower() == "oga":
369+
ext = "ogg"
366370
mime = getattr(file_attr, "mime_type", None) or "audio/ogg"
367371

368372
try:

‎tests/test_transcription.py‎

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -446,10 +446,53 @@ async def _download_media(m, file=None):
446446

447447
assert result == {"status": "ok", "text": "hello world", "lang": "ru"}
448448
assert calls[0]["headers"]["Authorization"] == "Bearer test-key"
449-
assert calls[0]["files"]["file"][0] == "voice.oga"
449+
assert calls[0]["files"]["file"][0] == "voice.ogg"
450450
assert calls[0]["data"]["model"] == transcription.GROQ_MODEL
451451

452452

453+
@pytest.mark.asyncio
454+
@pytest.mark.parametrize(
455+
"ext_in,expected_filename",
456+
[
457+
(".oga", "voice.ogg"),
458+
("oga", "voice.ogg"),
459+
(".ogg", "voice.ogg"),
460+
(".mp3", "voice.mp3"),
461+
(".wav", "voice.wav"),
462+
(None, "voice.ogg"),
463+
],
464+
)
465+
async def test_transcribe_via_groq_normalizes_audio_file_extension(
466+
monkeypatch, ext_in, expected_filename
467+
):
468+
"""Groq API audio transcription only accepts [flac mp3 mp4 mpeg mpga m4a ogg opus wav webm].
469+
Telegram voice notes arrive as .oga, which Groq rejects with 400 unsupported_audio_format.
470+
Naming the upload as .ogg satisfies Groq while preserving the identical audio content (issue #219).
471+
"""
472+
monkeypatch.setenv("GROQ_API_KEY", "test-key")
473+
calls = []
474+
response = _FakeGroqResponse({"text": "ok", "language": "en"})
475+
monkeypatch.setattr(
476+
transcription.httpx, "AsyncClient", lambda **kw: _FakeHttpxClient(response, calls)
477+
)
478+
479+
file_obj = (
480+
SimpleNamespace(duration=10, ext=ext_in, mime_type="audio/ogg")
481+
if ext_in is not None
482+
else None
483+
)
484+
msg = _voice_msg(file=file_obj)
485+
486+
async def _download_media(m, file=None):
487+
return b"audio-bytes"
488+
489+
client = SimpleNamespace(download_media=_download_media)
490+
result = await transcription._transcribe_via_groq(client, msg)
491+
492+
assert result["status"] == "ok"
493+
assert calls[0]["files"]["file"][0] == expected_filename
494+
495+
453496
@pytest.mark.asyncio
454497
async def test_transcribe_via_groq_requires_api_key(monkeypatch):
455498
monkeypatch.delenv("GROQ_API_KEY", raising=False)

0 commit comments

Comments
 (0)