feat: forward optional language form field to faster-whisper - #19
Open
h3x89 wants to merge 1 commit into
Open
Conversation
Add an optional 'language' multipart form field to POST /v1/audio/transcriptions and pass it into the existing WhisperModel.transcribe() call as its language argument. - omitted language keeps automatic language detection unchanged - valid codes are forwarded as-is and take precedence over any 'language' key inside settings_override - codes are validated against faster_whisper.tokenizer._LANGUAGE_CODES (the same canonical, case-sensitive set the library's Tokenizer enforces); invalid codes are rejected with 400 Bad Request before any inference runs, instead of a silent ignore or a 500 ValueError Also add a pytest regression suite (first tests in the repo) with pytest/httpx declared as dev dependencies, refresh uv.lock, configure pythonpath for 'uv run pytest', and document the field plus the uv-based test workflow in the README.
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.
Problem
The OpenAI-compatible transcription endpoint currently silently ignores the multipart
languagefield: it is not declared by the FastAPI route, so FastAPI drops it before any application code runs. Callers that sendlanguage=plget automatic language detection and have no way to know their parameter had no effect.Fix
languageform parameter toPOST /v1/audio/transcriptions;WhisperModel.transcribe()call;Contract
Validation uses
faster_whisper.tokenizer._LANGUAGE_CODES— a private faster-whisper symbol, chosen deliberately so server-side validation is identical to the library's own case-sensitiveTokenizercheck (the same tuple; anything we accept cannot fail downstream, and uppercase codes get a clean 400 instead of a 500ValueError). The project pins its runtime via committeduv.lock+uv sync --frozen, so symbol availability is deterministic; happy to switch to a public API if one exists.400(rather than422) matches this file's existing convention: bad file / badresponse_format/ badtemperatureare all explicitHTTPException(400, "Bad Request, …").Tests
First test suite in the repo (
tests/test_language_forwarding.py, 11 tests, pytest + httpx added as dev dependencies):Coverage proves: omitted → no
languagekwarg reachestranscribe();language=pl→ forwarded as"pl"; invalid → 400 with inference never invoked; empty string behaves as omitted (multipart stack delivers it asNone); uppercase rejected like the Tokenizer would; the dedicatedlanguagefield takes precedence over alanguagekey insidesettings_override; OpenAPI schema exposes optionallanguage; existing json/text/srt/verbose_json shapes and response_format/temperature validation unchanged.Docker/runtime proof (built from this exact commit):
A/B proof
Instrumented
WhisperModel.transcribekwargs for identical requests (MODEL_SIZE=base, real Polish audio):Compatibility
Requests without
languageare byte-for-byte unchanged (no new kwarg on the transcribe path). Clients already sending lowercase ISO-639-1 codes previously had them silently discarded; after this patch those requests actually take effect — there is no client for which behavior can regress.Notes
settings_overridesemantics (other than precedence of the dedicated field), or any existing endpoint behavior.response_format=vttraises 500 (seg["start"]dict access on NamedTuple),textformat returns the JSON dict shape, wrongmodelvalue fails viaassert.