Skip to main content

Introduction

The Realtime API provides low-latency text/audio real-time conversation capabilities through WebSocket long connections with event stream interactions. It supports both text and audio input/output modes, enabling real-time voice conversations, text conversations, and more. Endpoint:

Authentication

string
required
Bearer Token, e.g. Bearer sk-xxxxxxxxxx

Connection Parameters

string
required
Model name, supported models:
  • gpt-realtime - GPT Realtime Standard
  • gpt-realtime-mini - GPT Realtime Mini

Basic Information

Event Types

Client-Sent Events

Server-Returned Events

Session Configuration

After establishing a WebSocket connection, you must first send a session.update event to configure session parameters.

Session Configuration Example

Session Parameters

array
default:"[\"text\"]"
Supported interaction modes, optional values:
  • "text" - Text mode
  • "audio" - Audio mode Can include multiple modes, e.g. ["text", "audio"]
string
System prompt, used to set assistant behavior and role
string
default:"alloy"
Voice type, optional values: alloy, echo, fable, onyx, nova, shimmer
number
default:"1.0"
Temperature parameter, controls output randomness, range: 0.0 - 2.0
string
default:"pcm16"
Input audio format, currently only supports pcm16
string
default:"pcm16"
Output audio format, currently only supports pcm16
array
Tool function list, supports function calling
string
Tool selection strategy: auto, required, none

Sending Messages

Text Message Example

Audio Message Example

Audio messages require first pushing audio data via input_audio_buffer.append, then calling input_audio_buffer.commit to submit:

Request Response Generation

After sending a message, you need to call response.create to trigger generation:

Complete Examples

Python Example

JavaScript Example

Response Examples

Error Handling

Error Event Format

Common Errors

Audio Format Requirements

Input Audio

  • Format: PCM16 (16-bit PCM)
  • Channels: Mono
  • Sample Rate: 24000 Hz
  • Encoding: Base64 encoded, sent via input_audio_buffer.append

Output Audio

  • Format: PCM16 (16-bit PCM)
  • Channels: Mono
  • Sample Rate: 24000 Hz
  • Encoding: Base64 encoded, returned via response.audio.delta event

Usage Flow

  1. Establish Connection: Connect via WebSocket to wss://api.leapx-hub.com/v1/realtime?model={model}
  2. Configure Session: Send session.update event to configure session parameters
  3. Send Message:
    • Text mode: Send conversation.item.create event
    • Audio mode: First send input_audio_buffer.append to push audio, then input_audio_buffer.commit to submit, finally send conversation.item.create
  4. Request Response: Send response.create event to trigger generation
  5. Receive Response: Listen to response.text.delta or response.audio.delta events to receive incremental output
  6. Handle Completion: After receiving response.done event, check usage statistics

Notes

  • Required Step: After establishing connection, must first send session.update to configure session
  • Trigger Response: After sending message, must call response.create to trigger generation
  • Audio Format: Audio must be PCM16 mono 24000Hz, Base64 encoded
  • Event ID: Recommend setting unique event_id for each event for tracking and debugging
  • Connection Management: Keep WebSocket connection active, avoid frequent disconnections and reconnections
  • Error Handling: Listen to error events and implement appropriate error handling logic
  • Dependencies:
    • Python: pip install websocket-client
    • JavaScript: Use native WebSocket API or ws library

Best Practices

  1. Connection Reuse: Reuse the same WebSocket connection for multiple conversation rounds to reduce connection overhead
  2. Error Retry: Implement exponential backoff retry mechanism for network errors
  3. Audio Buffering: Recommend sending audio data in chunks to avoid sending too large at once
  4. Usage Statistics: Pay attention to usage information in response.done to control costs reasonably
  5. Timeout Handling: Set reasonable timeout to avoid long waits