Skip to content

fix(audio): return str for response_format="text" instead of a content-dependent type - #288

Open
Kayvan-Zahiri wants to merge 1 commit into
groq:mainfrom
Kayvan-Zahiri:fix/audio-response-format-text
Open

fix(audio): return str for response_format="text" instead of a content-dependent type#288
Kayvan-Zahiri wants to merge 1 commit into
groq:mainfrom
Kayvan-Zahiri:fix/audio-response-format-text

Conversation

@Kayvan-Zahiri

Copy link
Copy Markdown

audio.transcriptions.create and audio.translations.create hardcode cast_to=Transcription / cast_to=Translation regardless of response_format. Groq serves response_format="text" as text/plain, so _response.py falls back to response.json() and returns whatever type the transcript happens to parse as. The return type therefore varies with what the speaker said:

body='Hello there.'   -> str
body='42'             -> int
body='true'           -> bool
body='null'           -> NoneType
body='[1, 2]'         -> list
body='{"text": "hi"}' -> Transcription

With _strict_response_validation=True a plain English transcript raises APIResponseValidationError instead of returning.

The fix picks cast_to from response_format, the way openai-python does with _get_response_format_type, and widens the declared return to Transcription | str / Translation | str. _parse short-circuits on cast_to == str, so text now returns the exact body in both strict and loose mode. json / verbose_json / default are unchanged.

Added tests/test_audio_response_format.py: 28 of its 30 cases fail before the patch, all 30 pass after. tests/api_resources/audio/ stays at 16 passed / 44 skipped. ruff, mypy and pyright are clean on the changed files.

Overloads keyed on response_format would give exact per-format types, but that belongs in the Stainless config rather than a hand patch to generated files.

🤖 Generated with Claude Code

transcriptions.create and translations.create hardcoded cast_to to their
response models regardless of response_format. Groq serves response_format="text"
as text/plain, so the parser fell back to json.loads() and returned whatever
type the transcript happened to parse as: "42" -> int, "true" -> bool,
"null" -> None, "[1, 2]" -> list. With _strict_response_validation=True a plain
English transcript raised APIResponseValidationError instead.

Pick cast_to from response_format, matching openai-python, and widen the
declared return to `Transcription | str` / `Translation | str`.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DQughgr95y4B9H1jfaQH8o
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant