request_handler

SmallWebRTC request handler for managing peer connections.

This module provides a client for handling web requests and managing WebRTC connections.

class pipecat.transports.smallwebrtc.request_handler.SmallWebRTCRequest(sdp: str, type: str, pc_id: str | None = None, restart_pc: bool | None = None, request_data: Any | None = None)[source]

Bases: object

Small WebRTC transport session arguments for the runner.

Parameters:
  • sdp – The SDP string (Session Description Protocol).

  • type – The type of the SDP, either “offer” or “answer”.

  • pc_id – Optional identifier for the peer connection.

  • restart_pc – Optional whether to restart the peer connection.

  • request_data – Optional custom data sent by the customer.

sdp: str
type: str
pc_id: str | None = None
restart_pc: bool | None = None
request_data: Any | None = None
classmethod from_dict(data: dict)[source]

Accept both snake_case and camelCase for the request_data field.

class pipecat.transports.smallwebrtc.request_handler.IceCandidate(candidate: str, sdp_mid: str, sdp_mline_index: int)[source]

Bases: object

The remote ice candidate object received from the peer connection.

Parameters:
  • candidate – The ice candidate patch SDP string (Session Description Protocol).

  • sdp_mid – The SDP mid for the candidate patch.

  • sdp_mline_index – The SDP mline index for the candidate patch.

candidate: str
sdp_mid: str
sdp_mline_index: int
class pipecat.transports.smallwebrtc.request_handler.SmallWebRTCPatchRequest(pc_id: str, candidates: list[IceCandidate])[source]

Bases: object

Small WebRTC transport session arguments for the runner.

Parameters:
  • pc_id – Identifier for the peer connection.

  • candidates – A list of ICE candidate patches.

pc_id: str
candidates: list[IceCandidate]
class pipecat.transports.smallwebrtc.request_handler.ConnectionMode(*values)[source]

Bases: Enum

Enum defining the connection handling modes.

SINGLE = 'single'
MULTIPLE = 'multiple'
class pipecat.transports.smallwebrtc.request_handler.SmallWebRTCRequestHandler(ice_servers: list[RTCIceServer] | None = None, esp32_mode: bool = False, host: str | None = None, connection_mode: ConnectionMode = ConnectionMode.MULTIPLE)[source]

Bases: object

SmallWebRTC request handler for managing peer connections.

This class is responsible for:
  • Handling incoming SmallWebRTC requests.

  • Creating and managing WebRTC peer connections.

  • Supporting ESP32-specific SDP munging if enabled.

  • Invoking callbacks for newly initialized connections.

  • Supporting both single and multiple connection modes.

__init__(ice_servers: list[RTCIceServer] | None = None, esp32_mode: bool = False, host: str | None = None, connection_mode: ConnectionMode = ConnectionMode.MULTIPLE) None[source]

Initialize a SmallWebRTC request handler.

Parameters:
  • ice_servers (Optional[List[IceServer]]) – List of ICE servers to use for WebRTC connections.

  • esp32_mode (bool) – If True, enables ESP32-specific SDP munging.

  • host (Optional[str]) – Host address used for SDP munging in ESP32 mode. Ignored if esp32_mode is False.

  • connection_mode (ConnectionMode) – Mode of operation for handling connections. SINGLE allows only one active connection, MULTIPLE allows several.

update_ice_servers(ice_servers: list[RTCIceServer] | None = None)[source]

Update the list of ICE servers used for WebRTC connections.

async handle_web_request(request: SmallWebRTCRequest, webrtc_connection_callback: Callable[[Any], Awaitable[None]]) dict[str, str] | None[source]

Handle a SmallWebRTC request and resolve the pending answer.

This method will:
  • Reuse an existing WebRTC connection if pc_id exists.

  • Otherwise, create a new SmallWebRTCConnection.

  • Invoke the provided callback with the connection.

  • Manage ESP32-specific munging if enabled.

  • Enforce single/multiple connection mode constraints.

Parameters:
  • request (SmallWebRTCRequest) – The incoming WebRTC request, containing SDP, type, and optionally a pc_id.

  • webrtc_connection_callback (Callable[[Any], Awaitable[None]]) – An asynchronous callback function that is invoked with the WebRTC connection.

Returns:

Dictionary containing SDP answer, type, and peer connection ID, or None if no answer is available.

Raises:
  • HTTPException – If connection mode constraints are violated

  • Exception – Any exception raised during request handling or callback execution will be logged and propagated.

async handle_patch_request(request: SmallWebRTCPatchRequest)[source]

Handle a SmallWebRTC patch candidate request.

async close()[source]

Clear the connection map.