Repository navigation
feat(audio): add speech, transcriptions and translations - #11
Merged
Merged
Conversation
The gateway serves /v1/audio/speech, /v1/audio/transcriptions and /v1/audio/translations, but the SDK had no audio resource. - client.audio.speech.create() posts JSON and returns BinaryResponseContent (.content, .write_to_file()). - client.audio.transcriptions.create() and translations.create() upload the file as multipart form data. They accept a path, an open binary file, bytes, or a (filename, bytes) tuple. They return Transcription / Translation for json and verbose_json, keeping extra fields, and the response text for text, srt and vtt. - The HTTP client accepts data and files. It drops the JSON content type so httpx can set the multipart boundary. The file is read once before the retry loop, so a retry resends the same bytes. Closes #5 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Uy5vXsRpMhug7v3XwTMZHv
- Drop any casing of a default Content-Type header when sending files, so a lowercase content-type in default_headers no longer forces JSON. - Encode booleans as true / false and objects as JSON in form fields; only name[] keys are sent as repeated fields. - Accept a (filename, content, content_type) tuple, and fall back to "upload" when a file object's name is not a string (for example fdopen). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Uy5vXsRpMhug7v3XwTMZHv
Merged
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #5.
Adds
client.audio(sync and async) for the gateway's/v1/audio/speech,/v1/audio/transcriptionsand/v1/audio/translations. The spec is inopenspec/changes/add-audio-resource/.API
audio.speech.create(model, input, voice, instructions=, response_format=, speed=, extra_body=)BinaryResponseContent, which has.content(bytes) and.write_to_file(path).audio.transcriptions.create(file, model, language=, prompt=, response_format=, temperature=, timestamp_granularities=, extra_body=)audio.translations.create(file, model, prompt=, response_format=, temperature=, extra_body=)multipart/form-data.timestamp_granularitiesis sent as repeatedtimestamp_granularities[]fields.filecan be a path, an open binary file,bytes, or a(filename, content[, content_type])tuple. The filename is sent because the API uses its extension to detect the format.jsonandverbose_jsonthey returnTranscriptionorTranslation, withtext. Extra verbose fields such assegmentsandlanguageare kept.text,srtandvttthey return astr.HTTP client (
_base_client.request)dataandfiles.files, it drops anyContent-Typeheader, whatever its casing, so httpx can set the multipart boundary.true/false, and objects as JSON.Proof
tests/test_audio.pycovers:write_to_filesrtreturning text from a(filename, bytes)uploadmain: 122 passed on each of:main.Optional/Dictstyle.openspec validate add-audio-resource --strictpasses.audioresources. All tests are respx mocks; no live calls were made.Review: an independent read-only review found no blockers. Its findings are fixed in the second commit, each with a test:
content-typeset indefault_headersforced JSON on multipart uploads. (P2)fdopenfiles failed, and 3-tuples were rejected. (P3)Not included: streamed speech (
stream_format="sse") and streamed transcriptions. They can be added later if needed.🤖 Generated with Claude Code
https://claude.ai/code/session_01Uy5vXsRpMhug7v3XwTMZHv