createRealtimeSession method

Future<BetaRealtimeSession> createRealtimeSession({
  1. RealtimeModel? model,
  2. List<Modality> modalities = const [Modality.audio, Modality.text],
  3. String? instructions,
  4. SpeechVoice? voice,
  5. BetaAudioFormat inputAudioFormat = BetaAudioFormat.pcm16,
  6. BetaAudioFormat outputAudioFormat = BetaAudioFormat.pcm16,
  7. InputAudioTranscription? inputAudioTranscription,
  8. NoiseReduction? inputAudioNoiseReduction,
  9. TurnDetection? turnDetection,
  10. List<RealtimeFunctionTool>? tools,
  11. ToolChoice? toolChoice,
  12. num? temperature,
  13. int? maxResponseOutputTokens,
  14. num? speed,
  15. Tracing? tracing,
  16. String? clientSecretAnchor,
  17. 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);
}