microsoft/semantic-kernel · critical · Exception

Audio track not initialized

Error message

Audio track not initialized

What it means

The OpenAIRealtimeWebRTCService.create_session() method requires an audio track (an aiortc MediaStreamTrack) to be set on self.audio_track before it can establish the WebRTC peer connection. If audio_track is falsy (None or unset), a bare Exception is raised. This is an internal-state error indicating the service was not properly initialized.

Source

Thrown at python/semantic_kernel/connectors/ai/open_ai/services/_open_ai_realtime.py:780

                    }
                    event_dict["session"] = {k: v for k, v in session_dict.items() if k in allowed_fields}

                self.data_channel.send(json.dumps(event_dict))
            else:
                self.data_channel.send(event.model_dump_json(exclude_none=True))
        except Exception as e:
            logger.error(f"Failed to send event {event} with error: {e!s}")

    @override
    async def create_session(
        self,
        chat_history: "ChatHistory | None" = None,
        settings: "PromptExecutionSettings | None" = None,
        **kwargs: Any,
    ) -> None:
        """Create a session in the service."""
        if not self.audio_track:
            raise Exception("Audio track not initialized")
        self.peer_connection = RTCPeerConnection(
            configuration=RTCConfiguration(iceServers=[RTCIceServer(urls="stun:stun.l.google.com:19302")])
        )

        # track is the audio track being returned from the service
        self.peer_connection.add_listener("track", self._on_track)

        # data channel is used to send and receive messages
        self.data_channel = self.peer_connection.createDataChannel("oai-events", protocol="json")
        self.data_channel.add_listener("message", self._on_data)

        # this is the incoming audio, which sends audio to the service
        self.peer_connection.addTransceiver(self.audio_track)

        offer = await self.peer_connection.createOffer()
        await self.peer_connection.setLocalDescription(offer)

        try:

View on GitHub (pinned to c028a0c7dc)

Solutions

  1. Ensure the audio track is set on the service instance before calling create_session().
  2. Verify the audio source (microphone, file reader) is initialized and provides a valid MediaStreamTrack.
  3. Check if you should be using the WebRTC service at all — the WebSocket realtime service may be more appropriate if you don't need WebRTC audio.

Example fix

// before
service = OpenAIRealtimeWebRTCService(...)
await service.create_session()
// after
service = OpenAIRealtimeWebRTCService(...)
service.audio_track = my_audio_stream_track  # set before create_session
await service.create_session()
Defensive patterns

Strategy: validation

Validate before calling

def ensure_audio_track(service) -> None:
    if not getattr(service, 'audio_track', None):
        raise RuntimeError(
            'audio_track is not set on the WebRTC realtime service. '
            'Set it before calling create_session().'
        )

Type guard

def has_audio_track(service) -> bool:
    return getattr(service, 'audio_track', None) is not None

Try / catch

try:
    await service.create_session()
except Exception as e:
    if 'Audio track not initialized' in str(e):
        service.audio_track = create_audio_track()
        await service.create_session()

Prevention

When it happens

Trigger: Calling create_session() on an OpenAIRealtimeWebRTCService instance where the audio track was never set — e.g. constructing the service without providing audio input capability, or calling create_session() before the audio pipeline is wired up.

Common situations: Initializing the WebRTC realtime service without a microphone or audio source; race condition where create_session is called before set_audio_track; using the WebRTC service class when you intended the WebSocket-based one.

Related errors


AI-assisted analysis of microsoft/semantic-kernel@c028a0c7dc (2026-08-13). Data as JSON: /api/errors/66566533b2b19cf0. Report an issue: GitHub.