Skip to content

Conversation

@OiPunk
Copy link
Contributor

@OiPunk OiPunk commented Feb 10, 2026

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.frombuffer raises:

  • ValueError: buffer size must be a multiple of element size

Changes

  • In StreamedAudioResult._transform_audio_buffer, join buffered chunks and pad with one zero byte when the combined buffer length is odd.
  • Added regression tests:
    • odd-length buffer with dtype=np.int16
    • odd-length buffer with dtype=np.float32

Why this is safe

  • Padding only happens for odd-length payloads.
  • Existing even-length behavior is unchanged.
  • Output path for both int16 and float32 remains the same after alignment.

Validation

  • uv run ruff check src/agents/voice/result.py tests/voice/test_pipeline.py
  • uv run --extra voice pytest tests/voice/test_pipeline.py -q
  • uv run --extra voice python -m trace --count --coverdir /tmp/openai_agents_1824_trace --module pytest tests/voice/test_pipeline.py -k odd_length -q
    • confirms new alignment logic is executed in tests

Fixes #1824

Copy link

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 039083d702

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@OiPunk
Copy link
Contributor Author

OiPunk commented Feb 10, 2026

Thanks for the careful catch. You were right: zero-padding every odd flush can corrupt sample boundaries when a single PCM sample is split across chunks.

I pushed commit 6b10362 to address this:

  • Preserve a trailing byte across intermediate flushes in _stream_audio.
  • Only apply zero-padding on the final flush if one dangling byte still remains.
  • Added a regression test that reproduces split-sample chunks with buffer_size=1 and verifies we emit a single correct sample instead of two corrupted samples.

Local validation:

  • uv run ruff check src/agents/voice/result.py tests/voice/test_pipeline.py
  • uv run --extra voice pytest tests/voice/test_pipeline.py -q
  • uv run --extra voice python -m trace --count --coverdir /tmp/openai_agents_2456_cr_trace --module pytest tests/voice/test_pipeline.py -q

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

'ValueError:buffer size must be a multiple of element size' when mp3 audio chunks have odd byte length

1 participant