Skip to content

fix: dispatch on type instead of length in ReferenceLoader.load_audio() - #1318

Open
yuvrajnode wants to merge 1 commit into
fishaudio:mainfrom
yuvrajnode:fix/load-audio-bytes-handling
Open

fix: dispatch on type instead of length in ReferenceLoader.load_audio()#1318
yuvrajnode wants to merge 1 commit into
fishaudio:mainfrom
yuvrajnode:fix/load-audio-bytes-handling

Conversation

@yuvrajnode

@yuvrajnode yuvrajnode commented Aug 6, 2026

Copy link
Copy Markdown

ReferenceLoader.load_audio() accepts bytes | str and uses len(reference_audio) > 255 as a proxy for "this is raw audio rather than a path". That heuristic is wrong in three of the four possible cases.

Current behavior

input result
bytes shorter than 256 (truncated/empty upload) TypeError: argument should be a str object or an os.PathLike object returning str, not <class 'bytes'>
bytes longer than 255 (normal audio) ✅ works
str path that does not exist TypeError: a bytes-like object is required, not 'str'
valid str path longer than 255 chars ❌ misread as audio data → same TypeError

Only the common "long bytes" case behaves as intended. Short byte strings fall through to Path(bytes), and any str that fails the .exists() check reaches io.BytesIO(str) — neither accepts the type it is handed.

Impact

Both production callers reach this through VQManager.encode_reference() with bytesaudio_to_bytes() for id-based references, and ServeReferenceAudio.audio for API requests. So a small or malformed upload surfaces an opaque TypeError about os.PathLike rather than a meaningful decode error.

Fix

if isinstance(reference_audio, bytes):
    reference_audio = io.BytesIO(reference_audio)

Dispatch on the actual type and let torchaudio.load() report missing or undecodable files — it already does so with a clear message. This fixes all three broken cases and leaves the working one unchanged.

The repo has no test suite, so I kept this to the behavioral fix rather than introducing pytest as a new dependency.

load_audio() accepts `bytes | str` and used `len(reference_audio) > 255`
as a proxy for "this is raw audio rather than a path". That heuristic is
wrong in three of the four possible cases:

- bytes shorter than 256 (a truncated or empty upload) fall through to
  `Path(bytes)`, raising `TypeError: argument should be a str object or
  an os.PathLike object returning str, not <class 'bytes'>`
- a str path that does not exist reaches `io.BytesIO(str)`, raising
  `TypeError: a bytes-like object is required, not 'str'`
- a valid str path longer than 255 characters is misread as audio data
  and also reaches `io.BytesIO(str)`, raising the same TypeError

Only the "long bytes" case, which is the common one for real audio,
worked as intended.

Both production callers reach this via VQManager.encode_reference() with
bytes (audio_to_bytes() for id-based references, ServeReferenceAudio.audio
for API requests), so a small or malformed upload surfaces an opaque
TypeError instead of a decode error.

Dispatch on isinstance(..., bytes) and let torchaudio.load() report
missing or undecodable files, which it already does with a clear message.
@yuvrajnode

Copy link
Copy Markdown
Author

Hi @tongtong-lu, @leng-yue, @Stardust-minus, @Whale-Dolphin

Could you please take a look when you have a moment? Let me know if you need any additional context or if any changes are required. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant