fix(proxy): lazy-import soundfile in transcribe pass-through handler

Since v1.103.0, `import soundfile` at the top of
transcribe_passthrough_logging_handler.py breaks every `/v1/messages`
streaming request for the Anthropic and Bedrock providers unless the
`proxy` or `stt-nvidia-riva` extra is installed: success_handler imports
this module at module top, and streaming_iterator imports success_handler.
`soundfile` is only used by `media_file_seconds()` to measure media
duration.

Move the import into `media_file_seconds()` and skip the duration
measurement with a warning when soundfile is unavailable, so the default
streaming path no longer requires it.

Fixes #45379
This commit is contained in:
KaiyiQuan 2026-10-09 01:43:20 +08:00
parent b7ecb05340
commit b0c1119d66

View file

@ -13,7 +13,6 @@ from typing import IO, Final, Protocol, TypeAlias
from urllib.parse import quote
import httpx
import soundfile
from pydantic import ConfigDict, TypeAdapter, ValidationError
from typing_extensions import ReadOnly, TypedDict
@ -511,6 +510,17 @@ async def write_media_within_limit(response: httpx.Response, media_file: IO[byte
def media_file_seconds(path: Path) -> float | None:
try:
# soundfile is only needed to measure media duration and is declared in
# extras that are not installed on the default `/v1/messages` streaming
# path; import lazily so module import never fails without it.
import soundfile
except ModuleNotFoundError:
verbose_proxy_logger.warning(
"soundfile not installed; Transcribe media duration measurement is skipped (install the 'proxy' or 'stt-nvidia-riva' extra to enable it)"
)
return None
try:
with soundfile.SoundFile(str(path)) as audio:
return len(audio) / audio.samplerate