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-xxxxxxxxxxConnection Parameters
string
required
Model name, supported models:
gpt-realtime- GPT Realtime Standardgpt-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 asession.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, shimmernumber
default:"1.0"
Temperature parameter, controls output randomness, range: 0.0 - 2.0
string
default:"pcm16"
Input audio format, currently only supports
pcm16string
default:"pcm16"
Output audio format, currently only supports
pcm16array
Tool function list, supports function calling
string
Tool selection strategy:
auto, required, noneSending Messages
Text Message Example
Audio Message Example
Audio messages require first pushing audio data viainput_audio_buffer.append, then calling input_audio_buffer.commit to submit:
Request Response Generation
After sending a message, you need to callresponse.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.deltaevent
Usage Flow
- Establish Connection: Connect via WebSocket to
wss://api.leapx-hub.com/v1/realtime?model={model} - Configure Session: Send
session.updateevent to configure session parameters - Send Message:
- Text mode: Send
conversation.item.createevent - Audio mode: First send
input_audio_buffer.appendto push audio, theninput_audio_buffer.committo submit, finally sendconversation.item.create
- Text mode: Send
- Request Response: Send
response.createevent to trigger generation - Receive Response: Listen to
response.text.deltaorresponse.audio.deltaevents to receive incremental output - Handle Completion: After receiving
response.doneevent, checkusagestatistics
Notes
- Required Step: After establishing connection, must first send
session.updateto configure session - Trigger Response: After sending message, must call
response.createto trigger generation - Audio Format: Audio must be PCM16 mono 24000Hz, Base64 encoded
- Event ID: Recommend setting unique
event_idfor each event for tracking and debugging - Connection Management: Keep WebSocket connection active, avoid frequent disconnections and reconnections
- Error Handling: Listen to
errorevents and implement appropriate error handling logic - Dependencies:
- Python:
pip install websocket-client - JavaScript: Use native
WebSocketAPI orwslibrary
- Python:
Best Practices
- Connection Reuse: Reuse the same WebSocket connection for multiple conversation rounds to reduce connection overhead
- Error Retry: Implement exponential backoff retry mechanism for network errors
- Audio Buffering: Recommend sending audio data in chunks to avoid sending too large at once
- Usage Statistics: Pay attention to
usageinformation inresponse.doneto control costs reasonably - Timeout Handling: Set reasonable timeout to avoid long waits
