#5982·pipecat

BaseOutputTransport drops audio buffered for the next turn when playback stops

Author: kelvinymouraCreated Sep 30, 2026Updated Sep 30, 2026

pipecat version

1.12.0 (also reproduced on main at 20999cd)

Python version

3.12.7

Operating System

Windows 11

Issue description

BaseOutputTransport.MediaSender buffers and resamples output audio as it arrives, ahead of the paced playback queue. _bot_stopped_speaking() runs on the playback side, when playback reaches the end of speech, but it also clears that producer-side state (_clear_audio_buffer() and _resampler.reset()). Playback runs behind the producer, so at that point the buffer usually holds the start of what the bot says next, and it is dropped:

  • The next turn starts arriving while the previous one is still playing (TTS/S2S services deliver faster than real time, e.g. the response after a quick tool call). When playback reaches the previous turn's TTSStoppedFrame, the next turn's buffered audio is discarded.
  • In a SpeechOutputAudioRawFrame stream, the silence-based stop fires during a pause; if the next sentence has already started arriving, its beginning is discarded.

Two related gaps in the same buffer:

  • handle_interruptions() relies on _bot_stopped_speaking() to discard buffered audio, but that returns early when the bot hasn't started speaking. Audio buffered before playback starts survives the interruption, and the interrupted turn's TTSStoppedFrame then plays it after the user interrupted.
  • MediaSender.stop() doesn't flush the buffer, so an EndFrame without a preceding TTSStoppedFrame drops the last partial chunk and the resampler tail.

In our telephony pipeline (24 kHz speech-to-speech output resampled to 8 kHz) we measured 40–160 ms of speech lost per occurrence in the first two cases, ~30 ms of stale audio after an early interruption and ~40 ms lost at EndFrame. We have run an equivalent local override of MediaSender since mid-September.

Reproduction steps

  1. Run the script below with pipecat 1.12.0 or main.
  2. Turn 1 arrives in a burst, and turn 2 starts arriving while turn 1 is still playing.
  3. It prints turn 2 sent 2880 bytes, played 1920 bytes, lost 20 ms: the part of turn 2 that was buffered when playback reached turn 1's TTSStoppedFrame never plays. There is no resampling in this script; with resampling, the resampler's contents are lost as well.

Minimal reproducible example

python
import asyncio
from types import SimpleNamespace

from pipecat.clocks.system_clock import SystemClock
from pipecat.frames.frames import (
    BotStoppedSpeakingFrame,
    CancelFrame,
    StartFrame,
    TTSAudioRawFrame,
    TTSStoppedFrame,
)
from pipecat.processors.frame_processor import FrameDirection, FrameProcessorSetup
from pipecat.transports.base_output import BaseOutputTransport
from pipecat.transports.base_transport import TransportParams
from pipecat.utils.asyncio.task_manager import TaskManager

DOWN = FrameDirection.DOWNSTREAM


async def main():
    transport = BaseOutputTransport(TransportParams(audio_out_enabled=True))
    stops = []
    written = bytearray()

    async def push_frame(frame, direction=DOWN):
        if isinstance(frame, BotStoppedSpeakingFrame) and direction == DOWN:
            stops.append(frame)

    async def write_audio_frame(frame):
        # Take as long as the audio lasts, like a real output.
        await asyncio.sleep(len(frame.audio) / (2 * frame.sample_rate))
        written.extend(frame.audio)
        return True

    transport.push_frame = push_frame
    transport.write_audio_frame = write_audio_frame
    await transport.setup(
        FrameProcessorSetup(
            clock=SystemClock(),
            task_manager=TaskManager(),
            pipeline_worker=SimpleNamespace(app_resources=None),
        )
    )
    start = StartFrame()
    await transport.process_frame(start, DOWN)
    await transport.set_transport_ready(start)
    sender = transport._media_senders[None]
    rate, chunk = sender.sample_rate, sender.audio_chunk_size

    def tts(audio, context_id):
        return TTSAudioRawFrame(
            audio=audio, sample_rate=rate, num_channels=1, context_id=context_id
        )

    first = b"\x01\x02" * (3 * chunk // 2)
    second = b"\x03\x04" * (3 * chunk // 4)
    head, rest = second[: chunk // 2], second[chunk // 2 :]

    # Turn 1 arrives in a burst; turn 2 starts arriving while turn 1 still plays.
    await transport.process_frame(tts(first, "turn-1"), DOWN)
    await transport.process_frame(TTSStoppedFrame(context_id="turn-1"), DOWN)
    await transport.process_frame(tts(head, "turn-2"), DOWN)

    # Playback reaches turn 1's TTSStoppedFrame, then the rest of turn 2 arrives.
    while not stops:
        await asyncio.sleep(0.01)
    await transport.process_frame(tts(rest, "turn-2"), DOWN)
    await transport.process_frame(TTSStoppedFrame(context_id="turn-2"), DOWN)
    while len(stops) < 2:
        await asyncio.sleep(0.01)

    played = bytes(written[len(first) :]).rstrip(b"\x00")
    lost_ms = (len(second) - len(played)) / 2 / rate * 1000
    print(f"turn 2 sent {len(second)} bytes, played {len(played)} bytes, lost {lost_ms:.0f} ms")
    await transport.cancel(CancelFrame())


asyncio.run(main())

Eval scenario or recordings (optional)

No response

Expected behavior

A playback-side stop leaves buffered audio for what comes next. Only producer-side boundaries (the TTSStoppedFrame flush, interruptions, EndFrame/CancelFrame) decide what happens to it: interruptions drop not-yet-played audio even if the bot hadn't started speaking, and EndFrame plays what's still buffered.

Actual behavior

The next turn loses the audio that was buffered when playback reached the end of the previous one (lost 20 ms in the script above), audio buffered before an interruption can still play after it, and an EndFrame drops what's still buffered.

Logs

No response

Before you submit

  • I have personally reproduced this issue on the latest Pipecat release (or main).
  • I have searched existing issues for this bug.
  • If AI tools helped write this report, I have verified the behavior myself and will personally answer follow-up questions.