Skip to main content
This is an interactive notebook. You can run it locally or use the following links:
This notebook shows you how to use Weave to log and trace audio generated by OpenAI’s audio APIs, so you can inspect prompts, audio outputs, and transcripts alongside the rest of your LLM application data. The notebook targets developers who already build with OpenAI’s audio models and want observability into their audio traces. The notebook first uses the OpenAI chat completions API with GPT 4o Audio Preview to generate audio responses to text prompts and track these in Weave. OpenAI chat completions API interface with GPT 4o Audio Preview integration and audio response generation workflow For the advanced use case, the notebook uses the OpenAI Realtime API to stream audio in real time, so you can see how Weave captures both sides of a live conversation. Click the following thumbnail to view the video demonstration. Video thumbnail for the Weave Realtime audio demonstration

Setup

This section installs the Python packages, loads API credentials, and imports the libraries needed for the chat completions example. Start by installing the OpenAI (openai) and Weave (weave) dependencies, as well as the API key management dependency set-env.
Next, load the required API keys for OpenAI and Weave. This example uses set_env, which is compatible with Google Colab’s secret keys manager and serves as an alternative to Colab’s specific google.colab.userdata. See the set-env-colab-kaggle-dotenv usage instructions.
Finally, import the required libraries.

Audio streaming and storage example

With the dependencies installed and credentials loaded, you can now set up a call to OpenAI’s completions endpoint with audio modality enabled. First, create the OpenAI client and initiate a Weave project so that Weave logs subsequent calls to your workspace.
Now, define the OpenAI completions request and add the Weave decorator (op). The @weave.op() decorator instructs Weave to capture the function’s inputs, outputs, and audio file in a trace. The following code defines the function prompt_endpoint_and_log_trace. This function has three primary steps:
  1. Make a completion object using the gpt-4o-audio-preview model, which supports text and audio inputs and outputs.
    • Prompt the model to count to 13 slowly with varying accents.
    • Set the completion to stream.
  2. Open a new output file to receive the streamed data chunk by chunk.
  3. Return an open file handler to the audio file so Weave logs the audio data in the trace.

Testing

After defining the function, run the following cell to invoke it end-to-end and confirm that audio is generated and logged. Weave stores the system and user prompts in a trace along with the output audio. After running the cell, click the trace link printed in the cell output to view your trace. At this point, you have a complete, traced chat-completions audio call in Weave.

Advanced usage: Realtime API with Weave

The remainder of this cookbook walks through a more advanced example that uses OpenAI’s Realtime API together with Weave to trace a live, bidirectional audio conversation. Realtime Audio API integration with Weave and streaming audio conversation interface OpenAI’s Realtime API is a conversational API for building real-time audio and text assistants. Before you run the Realtime example, review the following requirements:
  • Review the cells in Microphone configuration.
  • Due to limitations of the Google Colab execution environment, you must run this on your host machine as a Jupyter Notebook. You can’t run it in the browser.
    • On macOS, you must install portaudio through Brew for PyAudio to function.
  • The enable_audio_playback toggle plays back the audio that the assistant outputs. Headphones are required if you enable this, because echo detection requires a complex implementation.

Requirements setup

The Realtime example requires additional packages for audio I/O and websocket communication. Install them and reload your environment variables.

Microphone configuration

Because the Realtime example records from your microphone and plays audio back through your speakers, you must tell PyAudio which devices to use. Run the following cell to find all available audio devices. Then, populate the INPUT_DEVICE_INDEX and the OUTPUT_DEVICE_INDEX based on the devices listed. Your input device has at least one input channel, and your output device has at least one output channel.

OpenAI Realtime API schema implementation

The next sections build up the Realtime client piece by piece: the message schema, an audio writer, the Weave-instrumented model, and the recorder. The OpenAI Python SDK doesn’t yet provide Realtime API support. This example implements the complete OpenAI Realtime API schema in Pydantic for greater legibility, and may deprecate it once official support ships.

Pydantic schema for the OpenAI Realtime API

Audio stream writer (to disk and in memory)

The following helper class buffers streamed audio chunks into a WAV file (or an in-memory buffer) so the audio can later be passed to Weave for logging.

Realtime audio model

The realtime (RT) audio model uses a websocket to send events to OpenAI’s Realtime API. The model works as follows:
  1. init: Initialize local buffers (input audio) and streams (assistant playback stream, user audio disk writer stream) and open a connection to the Realtime API.
  2. receive_messages_thread: A thread handles receiving messages from the API. The code handles four primary event types:
    • RESPONSE_AUDIO_TRANSCRIPT_DONE: The server signals that the assistant’s response is complete and provides the transcript.
    • CONVERSATION_ITEM_INPUT_AUDIO_TRANSCRIPTION_COMPLETED: The server signals that the user’s audio is transcribed, and sends the transcript of the user’s audio. The code logs the transcript to Weave and prints it for the user.
    • RESPONSE_AUDIO_DELTA: The server sends a new chunk of assistant response audio. The code appends this to the ongoing response data by the response ID, and adds this to the output stream for playback.
    • RESPONSE_DONE: The server signals completion of an assistant response. The code gets all audio chunks associated with the response, as well as the transcript, and logs them in Weave.
  3. send_audio: A handler appends user audio chunks to a buffer, and sends chunks of audio when the audio buffer reaches a certain size.

Audio recorder

With the model defined, you need a way to feed it microphone input. This example uses a PyAudio input stream with a handler linked to the send_audio method of the RTAudio model. The code returns the stream to the main thread so it can exit safely upon program completion.

Main thread

This final cell ties the previous components together and runs the realtime assistant. The main thread initiates a Realtime Audio Model with Weave integrated. Next, the code opens a recording and waits for a keyboard interrupt from the user. When you stop the cell, you have a complete Weave trace of the conversation, including user and assistant transcripts and audio.