createRealtimeSession method
Future<BetaRealtimeSession>
createRealtimeSession({
- RealtimeModel? model,
- List<
Modality> modalities = const [Modality.audio, Modality.text], - String? instructions,
- SpeechVoice? voice,
- BetaAudioFormat inputAudioFormat = BetaAudioFormat.pcm16,
- BetaAudioFormat outputAudioFormat = BetaAudioFormat.pcm16,
- InputAudioTranscription? inputAudioTranscription,
- NoiseReduction? inputAudioNoiseReduction,
- TurnDetection? turnDetection,
- List<
RealtimeFunctionTool> ? tools, - ToolChoice? toolChoice,
- num? temperature,
- int? maxResponseOutputTokens,
- num? speed,
- Tracing? tracing,
- String? clientSecretAnchor,
- int? clientSecretSeconds,
Implementation
Future<BetaRealtimeSession> createRealtimeSession({
// ── Core params ──────────────────────────────────────────────────────
RealtimeModel? model,
List<Modality> modalities = const [Modality.audio, Modality.text],
String? instructions,
SpeechVoice? voice,
BetaAudioFormat inputAudioFormat = BetaAudioFormat.pcm16,
BetaAudioFormat outputAudioFormat = BetaAudioFormat.pcm16,
InputAudioTranscription? inputAudioTranscription,
NoiseReduction? inputAudioNoiseReduction,
TurnDetection? turnDetection,
List<RealtimeFunctionTool>? tools,
ToolChoice? toolChoice,
num? temperature,
int? maxResponseOutputTokens,
num? speed,
Tracing? tracing, // "auto", Map<String,dynamic>, or null
// ── Client-secret options ───────────────────────────────────────────
String? clientSecretAnchor, // created_at
int? clientSecretSeconds, // 10 – 7200
}) async {
Map<String, dynamic> payload = {
if (model != null) 'model': model.toJson(),
'modalities': modalities.map((m) => m.toJson()).toList(),
if (instructions != null) 'instructions': instructions,
if (voice != null) 'voice': voice.toJson(),
if (inputAudioFormat != BetaAudioFormat.pcm16) 'input_audio_format': inputAudioFormat.toJson(),
if (outputAudioFormat != BetaAudioFormat.pcm16) 'output_audio_format': outputAudioFormat.toJson(),
if (inputAudioTranscription != null) 'input_audio_transcription': inputAudioTranscription.toJson(),
if (inputAudioNoiseReduction != null) 'input_audio_noise_reduction': inputAudioNoiseReduction.toJson(),
if (turnDetection != null) 'turn_detection': turnDetection.toJson(),
if (tools != null) 'tools': tools.map((t) => t.toJson()).toList(),
if (toolChoice != null) 'tool_choice': toolChoice.toJson(),
if (temperature != null) 'temperature': temperature,
if (maxResponseOutputTokens != null) 'max_response_output_tokens': maxResponseOutputTokens,
if (speed != null) 'speed': speed,
if (tracing != null) 'tracing': tracing.toJson(),
// client_secret nested block
if (clientSecretAnchor != null || clientSecretSeconds != null)
'client_secret': {
if (clientSecretAnchor != null || clientSecretSeconds != null)
'expires_after': {
if (clientSecretAnchor != null) 'anchor': clientSecretAnchor,
if (clientSecretSeconds != null) 'seconds': clientSecretSeconds,
},
},
};
final res = await postJson('/realtime/sessions', payload);
if (res.statusCode == 200) {
return BetaRealtimeSession.fromJson(jsonDecode(res.body));
}
throw OpenAIRequestException.fromHttpResponse(res);
}