fix(voice): handle odd-length audio buffers in StreamedAudioResult #2456
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.
Summary
Fixes odd-length audio-buffer crashes in voice streaming when converting PCM chunks with
np.frombuffer(..., dtype=np.int16).Problem
StreamedAudioResult._transform_audio_buffer()assumed a 2-byte aligned buffer. If a provider emits an odd number of bytes in a chunk (observed with some MP3 streaming providers),np.frombufferraises:ValueError: buffer size must be a multiple of element sizeChanges
StreamedAudioResult._transform_audio_buffer, join buffered chunks and pad with one zero byte when the combined buffer length is odd.dtype=np.int16dtype=np.float32Why this is safe
Validation
uv run ruff check src/agents/voice/result.py tests/voice/test_pipeline.pyuv run --extra voice pytest tests/voice/test_pipeline.py -quv run --extra voice python -m trace --count --coverdir /tmp/openai_agents_1824_trace --module pytest tests/voice/test_pipeline.py -k odd_length -qFixes #1824