mirror of
https://github.com/BerriAI/litellm.git
synced 2026-11-23 22:07:29 +00:00
feat(gemini): support multiple transcription language hints
This commit is contained in:
parent
2e1a98f521
commit
50dc4ce397
3 changed files with 125 additions and 4 deletions
30
litellm/llms/gemini/audio_transcription/README.md
Normal file
30
litellm/llms/gemini/audio_transcription/README.md
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
# Gemini transcription language hints
|
||||
|
||||
Gemini transcription accepts multiple language hints through the provider-specific `language_codes` parameter. The existing OpenAI-compatible `language="en"` parameter still works for a single hint
|
||||
|
||||
```python
|
||||
import litellm
|
||||
|
||||
with open("sample.wav", "rb") as audio:
|
||||
transcript = litellm.transcription(
|
||||
model="gemini/gemini-3.5-transcribe",
|
||||
file=audio,
|
||||
language_codes=["en-US", "es-ES"],
|
||||
)
|
||||
```
|
||||
|
||||
Through the LiteLLM proxy, send repeated `language_codes[]` multipart fields or a JSON-encoded list in `language_codes`:
|
||||
|
||||
```bash
|
||||
curl http://localhost:4000/v1/audio/transcriptions \
|
||||
-H "Authorization: Bearer $LITELLM_API_KEY" \
|
||||
-F 'model=gemini/gemini-3.5-transcribe' \
|
||||
-F 'file=@sample.wav' \
|
||||
-F 'language_codes=["en-US", "es-ES"]'
|
||||
```
|
||||
|
||||
`language_codes` takes precedence over `language` when supplied. An empty list enables automatic language detection, even if `language` is also set. Omit both parameters to use automatic detection as before
|
||||
|
||||
Bare language codes use the same BCP-47 normalization as `language`; explicit script and region variants are preserved. Invalid lists return an error instead of silently discarding hints. Word timestamps and speaker diarization can be used with multiple hints
|
||||
|
||||
These hints guide recognition; they do not restrict the transcript to the listed languages. See the [Gemini transcription documentation](https://ai.google.dev/gemini-api/docs/transcribe#language-detection-and-hints) for provider behavior
|
||||
|
|
@ -1,8 +1,9 @@
|
|||
import base64
|
||||
from collections.abc import Mapping, Sequence
|
||||
from typing import Final
|
||||
from typing import Annotated, Final
|
||||
|
||||
from httpx import Headers, Response
|
||||
from pydantic import StringConstraints, TypeAdapter, ValidationError
|
||||
|
||||
from litellm.litellm_core_utils.audio_utils.subtitle_utils import SUBTITLE_RESPONSE_FORMATS
|
||||
from litellm.litellm_core_utils.audio_utils.utils import (
|
||||
|
|
@ -211,7 +212,25 @@ def _build_interaction_request(
|
|||
return configured_request
|
||||
|
||||
|
||||
def _language_config(language: object) -> GeminiTranscriptionConfig:
|
||||
_LANGUAGE_CODES_ADAPTER: Final = TypeAdapter(
|
||||
tuple[Annotated[str, StringConstraints(strict=True, strip_whitespace=True, min_length=1)], ...]
|
||||
)
|
||||
|
||||
|
||||
def _language_config(language: object, language_codes: object) -> GeminiTranscriptionConfig:
|
||||
if language_codes is not None:
|
||||
try:
|
||||
codes: Final = (
|
||||
_LANGUAGE_CODES_ADAPTER.validate_json(language_codes)
|
||||
if isinstance(language_codes, str)
|
||||
else _LANGUAGE_CODES_ADAPTER.validate_python(language_codes)
|
||||
)
|
||||
except ValidationError:
|
||||
raise GeminiError(status_code=400, message="language_codes must be a list of non-empty language strings")
|
||||
multiple_language_config: Final[GeminiTranscriptionConfig] = {
|
||||
"language_codes": tuple(normalize_transcription_language_to_bcp47(code) for code in codes),
|
||||
}
|
||||
return multiple_language_config
|
||||
if not isinstance(language, str) or not language:
|
||||
return _EMPTY_TRANSCRIPTION_CONFIG
|
||||
language_config: Final[GeminiTranscriptionConfig] = {
|
||||
|
|
@ -229,7 +248,7 @@ def _timestamp_config(timestamp_granularities: object, response_format: object)
|
|||
|
||||
def _build_transcription_config(optional_params: Mapping[str, object]) -> GeminiTranscriptionConfig:
|
||||
transcription_config: Final[GeminiTranscriptionConfig] = {
|
||||
**_language_config(optional_params.get("language")),
|
||||
**_language_config(optional_params.get("language"), optional_params.get("language_codes")),
|
||||
**_timestamp_config(optional_params.get("timestamp_granularities"), optional_params.get("response_format")),
|
||||
}
|
||||
return transcription_config
|
||||
|
|
|
|||
|
|
@ -1,5 +1,7 @@
|
|||
import base64
|
||||
import json
|
||||
from collections.abc import Mapping
|
||||
from typing import Final
|
||||
|
||||
import httpx
|
||||
import pytest
|
||||
|
|
@ -10,7 +12,7 @@ from litellm.llms.gemini.audio_transcription.transformation import (
|
|||
)
|
||||
from litellm.llms.gemini.common_utils import GeminiError
|
||||
from litellm.types.utils import LlmProviders
|
||||
from litellm.utils import ProviderConfigManager
|
||||
from litellm.utils import ProviderConfigManager, get_optional_params_transcription
|
||||
|
||||
AUDIO_BYTES = b"RIFF....WAVEfmt fake-wav-bytes"
|
||||
|
||||
|
|
@ -123,6 +125,76 @@ class TestGetCompleteUrl:
|
|||
|
||||
|
||||
class TestTransformRequest:
|
||||
@pytest.mark.parametrize(
|
||||
("params", "expected_codes"),
|
||||
[
|
||||
({"language_codes": ["en", "es-ES"]}, ["en-US", "es-ES"]),
|
||||
({"language_codes": '["en", "es-ES"]'}, ["en-US", "es-ES"]),
|
||||
({"language_codes": [" en ", " es-ES "]}, ["en-US", "es-ES"]),
|
||||
({"language_codes": ["zh-Hant-TW", "zh-Hans-CN"]}, ["zh-Hant-TW", "zh-Hans-CN"]),
|
||||
({"language": "fr", "language_codes": ["en", "es"]}, ["en-US", "es-ES"]),
|
||||
({"language": "fr", "language_codes": []}, []),
|
||||
({"language": "fr", "language_codes": "[]"}, []),
|
||||
({"language": "fr", "language_codes": None}, ["fr-FR"]),
|
||||
],
|
||||
)
|
||||
def test_language_codes_reach_gemini_through_provider_params(
|
||||
self,
|
||||
config: GeminiAudioTranscriptionConfig,
|
||||
params: Mapping[str, object],
|
||||
expected_codes: list[str],
|
||||
) -> None:
|
||||
optional_params: Final = get_optional_params_transcription(
|
||||
model="gemini-3.5-transcribe", custom_llm_provider="gemini", **params
|
||||
)
|
||||
request_data: Final = config.transform_audio_transcription_request(
|
||||
model="gemini-3.5-transcribe",
|
||||
audio_file=("sample.wav", AUDIO_BYTES, "audio/wav"),
|
||||
optional_params=optional_params,
|
||||
litellm_params={},
|
||||
)
|
||||
assert json.loads(json.dumps(request_data.data)) == {
|
||||
"model": "gemini-3.5-transcribe",
|
||||
"input": [
|
||||
{
|
||||
"type": "audio",
|
||||
"data": base64.b64encode(AUDIO_BYTES).decode("utf-8"),
|
||||
"mime_type": "audio/wav",
|
||||
}
|
||||
],
|
||||
"generation_config": {"transcription_config": {"language_codes": expected_codes}},
|
||||
}
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"language_codes",
|
||||
["en", '"en"', "[", "null", 1, {}, ["en", 1], ["en", ""], [" "], '["en", null]'],
|
||||
)
|
||||
def test_invalid_language_codes_raise_instead_of_silently_dropping_hints(
|
||||
self, config: GeminiAudioTranscriptionConfig, language_codes: object
|
||||
) -> None:
|
||||
with pytest.raises(GeminiError, match="language_codes must be a list of non-empty language strings") as excinfo:
|
||||
config.transform_audio_transcription_request(
|
||||
model="gemini-3.5-transcribe",
|
||||
audio_file=("sample.wav", AUDIO_BYTES, "audio/wav"),
|
||||
optional_params={"language_codes": language_codes},
|
||||
litellm_params={},
|
||||
)
|
||||
assert excinfo.value.status_code == 400
|
||||
|
||||
def test_multiple_language_codes_preserve_word_timestamps(self, config: GeminiAudioTranscriptionConfig) -> None:
|
||||
request_data: Final = config.transform_audio_transcription_request(
|
||||
model="gemini-3.5-transcribe",
|
||||
audio_file=("sample.wav", AUDIO_BYTES, "audio/wav"),
|
||||
optional_params={"language_codes": ["en-US", "es-ES"], "timestamp_granularities": ["word"]},
|
||||
litellm_params={},
|
||||
)
|
||||
assert json.loads(json.dumps(request_data.data["generation_config"])) == {
|
||||
"transcription_config": {
|
||||
"language_codes": ["en-US", "es-ES"],
|
||||
"mode": {"type": "verbatim", "timestamp_granularities": ["word"], "diarization_mode": "speaker"},
|
||||
}
|
||||
}
|
||||
|
||||
def test_builds_json_interaction_request(self, config):
|
||||
request_data = config.transform_audio_transcription_request(
|
||||
model="gemini/gemini-3.5-transcribe",
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue