> ## 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.

# OpenAI Audio Output Stream

> Stream pcm16 audio from gpt-audio, printing the transcript while writing frames to a WAV file.

```python audio_output_stream.py theme={null}
"""
Openai Audio Output Stream
==========================

Cookbook example for `openai/chat/audio_output_stream.py`.
"""

import base64
import wave
from typing import Iterator

from agno.agent import Agent, RunOutputEvent  # noqa
from agno.db.in_memory import InMemoryDb
from agno.models.openai import OpenAIChat

# ---------------------------------------------------------------------------
# Create Agent
# ---------------------------------------------------------------------------

# 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
    ),
    db=InMemoryDb(),
)
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 = response_audio.content
                    pcm_bytes = base64.b64decode(pcm_bytes)
                    wav_file.writeframes(pcm_bytes)
                except Exception as e:
                    print(f"Error decoding audio: {e}")
print()
print(f"Saved audio to {filename}")

print("Metrics:")
print(agent.get_last_run_output().metrics)

# ---------------------------------------------------------------------------
# Run Agent
# ---------------------------------------------------------------------------

if __name__ == "__main__":
    pass
```

## Run the Example

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

**1. Install dependencies**

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

**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. Run the example**

Save the code above as `audio_output_stream.py`, then run:

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

Full source: [cookbook/90\_models/openai/chat/audio\_output\_stream.py](https://github.com/agno-agi/agno/blob/v3.0.4/cookbook/90_models/openai/chat/audio_output_stream.py)


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