> ## Documentation Index
> Fetch the complete documentation index at: https://agno-v2.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Audio Streaming

> Stream PCM16 audio events from OpenAI's gpt-audio model and write the decoded chunks to a WAV file in real time.

Stream audio responses from an Agno agent using OpenAI's `gpt-audio` model.

```python audio_streaming.py theme={null}
import base64
import wave
from typing import Iterator

from agno.agent import Agent, RunOutputEvent
from agno.models.openai import OpenAIChat

# Audio Configuration
SAMPLE_RATE = 24000  # Hz (24kHz)
CHANNELS = 1  # Mono (Change to 2 if Stereo)
SAMPLE_WIDTH = 2  # Bytes (16 bits)

# Provide the agent with the audio file and audio configuration and get result as text + audio
agent = Agent(
    model=OpenAIChat(
        id="gpt-audio",
        modalities=["text", "audio"],
        audio={
            "voice": "alloy",
            "format": "pcm16",
        },  # Only pcm16 is supported with streaming
    ),
)
output_stream: Iterator[RunOutputEvent] = agent.run(
    "Tell me a 10 second story", stream=True
)

filename = "tmp/response_stream.wav"

# Open the file once in append-binary mode
with wave.open(str(filename), "wb") as wav_file:
    wav_file.setnchannels(CHANNELS)
    wav_file.setsampwidth(SAMPLE_WIDTH)
    wav_file.setframerate(SAMPLE_RATE)

    # Iterate over generated audio
    for response in output_stream:
        response_audio = response.response_audio  # type: ignore
        if response_audio:
            if response_audio.transcript:
                print(response_audio.transcript, end="", flush=True)
            if response_audio.content:
                try:
                    pcm_bytes = base64.b64decode(response_audio.content)
                    wav_file.writeframes(pcm_bytes)
                except Exception as e:
                    print(f"Error decoding audio: {e}")
print()
```

## Usage

<Snippet file="create-venv-step.mdx" />

**1. Install dependencies**

```bash theme={null}
uv pip install -U openai agno
```

**2. Export your OpenAI API key**

```bash Mac/Linux theme={null}
  export OPENAI_API_KEY="your_openai_api_key_here"
```

```bash Windows theme={null}
  $Env:OPENAI_API_KEY="your_openai_api_key_here"
```

**3. Create the output directory**

```bash theme={null}
python -c "from pathlib import Path; Path('tmp').mkdir(parents=True, exist_ok=True)"
```

**4. Run Agent**

```bash theme={null}
python audio_streaming.py
```

## Key Features

* **Streamed output**: Consumes audio chunks as the model returns them
* **PCM16 format**: Writes 16-bit PCM audio at 24 kHz in mono
* **Transcript output**: Prints transcript chunks as they arrive
* **WAV file**: Saves the audio stream to `tmp/response_stream.wav`
* **Decode failures**: Prints chunk decoding failures and continues consuming the stream

## Next Steps

| Task | Guide |
| - | - |
| Send and receive audio | [Audio Input and Output](https://agno-v2.mintlify.site/multimodal/agent/usage/audio-input-output.md) |
| Transcribe audio | [Audio to Text](https://agno-v2.mintlify.site/multimodal/agent/usage/audio-to-text.md) |
| Continue an audio conversation | [Audio Multi-Turn](https://agno-v2.mintlify.site/multimodal/agent/usage/audio-multi-turn.md) |

## Output Format

The agent writes 24 kHz, mono, 16-bit PCM audio to `tmp/response_stream.wav`. The script saves the stream to disk. It does not play the audio.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.