Set connection parameters without updating session

I’m working with the realtime api, and when the connection is opened, I receive a session.created event back with some default config, after that I need to update the session with the config I want, receiving session.updated event. Is it possible to already stablish the connection with the desired config and do not need to update the session?

    async def connect(self) -> None:
        """Establish WebSocket connection with the Realtime API."""
        url = f"{self.base_url}?model={self.model}"
        headers = {"Authorization": f"Bearer {self.api_key}", "OpenAI-Beta": "realtime=v1"}

        self.ws = await websockets.connect(url, extra_headers=headers)

        if self.turn_detection_mode == TurnDetectionMode.MANUAL:
            await self.update_session(
                {
                    "modalities": ["text", "audio"],
                    "instructions": self.prompt.base,
                    "voice": self.voice,
                    "input_audio_format": "pcm16",
                    "output_audio_format": "pcm16",
                    "input_audio_transcription": {"model": "whisper-1"},
                    "turn_detection": None,
                    "temperature": self.temperature,
                }
            )
        elif self.turn_detection_mode == TurnDetectionMode.SERVER_VAD:
            await self.update_session(
                {
                    "modalities": ["text", "audio"],
                    "instructions": self.prompt.base,
                    "voice": self.voice,
                    "input_audio_format": "pcm16",
                    "output_audio_format": "pcm16",
                    "input_audio_transcription": {"model": "whisper-1"},
                    "turn_detection": {
                        "type": "server_vad",
                        "threshold": 0.5,
                        "prefix_padding_ms": 500,
                        "silence_duration_ms": 200,
                    },
                    "temperature": self.temperature,
                }
            )
        else:
            raise ValueError(f"Invalid turn detection mode: {self.turn_detection_mode}")

To my knowledge, this is not possible yet. It might be in the future however, session.update should suffice for now. I’ve only had some very rare edge cases where it would use the default config instead of the one i specified in a session.update.

1 Like