Skip to content

fix(voice): stop AudioInput.to_base64() from mutating caller's buffer#3201

Merged
seratch merged 2 commits intoopenai:mainfrom
adityasingh2400:fix/voice-to-base64-mutation
May 9, 2026
Merged

fix(voice): stop AudioInput.to_base64() from mutating caller's buffer#3201
seratch merged 2 commits intoopenai:mainfrom
adityasingh2400:fix/voice-to-base64-mutation

Conversation

@adityasingh2400
Copy link
Copy Markdown
Contributor

Summary

AudioInput.to_base64() currently rebinds self.buffer to an int16 array when the input is float32:

def to_base64(self) -> str:
    if self.buffer.dtype == np.float32:
        # convert to int16
        self.buffer = np.clip(self.buffer, -1.0, 1.0)
        self.buffer = (self.buffer * 32767).astype(np.int16)
    ...

This silently corrupts any reference the caller still holds to the original float32 buffer. A user who creates an AudioInput, calls to_base64() (e.g. for tracing), and then inspects audio_input.buffer again will find an int16 array where they put a float32 one — different dtype, different values.

The companion _buffer_to_audio_file() already does the conversion via a local rebinding (the parameter buffer is shadowed inside the function), so to_audio_file() does not have this bug. This PR aligns to_base64() with that pattern: do the int16 conversion into a local variable and leave self.buffer untouched.

Changes

  • src/agents/voice/input.py: rewrite to_base64() to convert into a local int16_buffer instead of mutating self.buffer.
  • tests/voice/test_input.py: add a regression test asserting audio_input.buffer.dtype and contents are unchanged after to_base64(), and that calling it twice produces the same encoding.

Diff is 25 lines total.

Test plan

  • pytest tests/voice/ — all 31 tests pass
  • Regression test fails on main, passes with this change
  • ruff check clean

`AudioInput.to_base64()` rebinds `self.buffer` to an int16 array when the
input is float32, silently corrupting any reference the caller still holds
to the original buffer. Convert into a local variable instead so the call
is side-effect-free, and add a regression test.
@github-actions github-actions Bot added bug Something isn't working feature:voice labels May 8, 2026
@seratch
Copy link
Copy Markdown
Member

seratch commented May 8, 2026

@codex review

@seratch seratch marked this pull request as draft May 8, 2026 16:56
@chatgpt-codex-connector
Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Nice work!

ℹ️ 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".

@seratch seratch marked this pull request as ready for review May 9, 2026 03:46
@seratch seratch added this to the 0.17.x milestone May 9, 2026
@seratch seratch merged commit f3d434c into openai:main May 9, 2026
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working feature:voice

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants