1111)
1212from .webrtc_conversation import WebRTCConversation
1313from .base_connection import ConnectionType
14+ from .location_utils import Location , get_origin_for_location , get_livekit_url_for_location
1415
1516
1617def create_conversation (
@@ -21,6 +22,7 @@ def create_conversation(
2122 connection_type : ConnectionType = ConnectionType .WEBSOCKET ,
2223 conversation_token : Optional [str ] = None ,
2324 requires_auth : bool = True ,
25+ location : Optional [Location ] = None ,
2426 audio_interface : Optional [Union [AudioInterface , AsyncAudioInterface ]] = None ,
2527 config : Optional [ConversationInitiationData ] = None ,
2628 client_tools : Optional [ClientTools ] = None ,
@@ -46,6 +48,7 @@ def create_conversation(
4648 connection_type: Type of connection (websocket or webrtc)
4749 conversation_token: Token for WebRTC authentication
4850 requires_auth: Whether authentication is required
51+ location: Data residency location (us, eu-residency, in-residency, global)
4952 audio_interface: Audio interface for the conversation
5053 config: Conversation configuration
5154 client_tools: Client tools for handling agent calls
@@ -63,23 +66,22 @@ def create_conversation(
6366 audio_interface=your_audio_interface
6467 )
6568
66- # WebRTC conversation
69+ # WebRTC conversation with EU residency
6770 conversation = create_conversation(
6871 client=client,
6972 agent_id="your-agent-id",
7073 connection_type=ConnectionType.WEBRTC,
71- conversation_token="your-token", # Optional, will fetch if not provided
74+ location=Location.EU_RESIDENCY,
7275 audio_interface=your_async_audio_interface,
7376 async_callback_agent_response=your_response_handler
7477 )
7578
76- # Public agent (no auth required)
79+ # WebSocket conversation with specific location
7780 conversation = create_conversation(
7881 client=client,
79- agent_id="public-agent-id",
80- connection_type=ConnectionType.WEBRTC,
81- requires_auth=False,
82- audio_interface=your_async_audio_interface
82+ agent_id="your-agent-id",
83+ location=Location.IN_RESIDENCY,
84+ audio_interface=your_audio_interface
8385 )
8486 """
8587
@@ -90,17 +92,29 @@ def create_conversation(
9092 config .connection_type = connection_type
9193 if conversation_token :
9294 config .conversation_token = conversation_token
95+ if location is not None :
96+ config .location = location
9397
9498 if connection_type == ConnectionType .WEBRTC :
9599 # Create WebRTC conversation
96100 if not isinstance (audio_interface , AsyncAudioInterface ) and audio_interface is not None :
97101 raise ValueError ("WebRTC conversations require an AsyncAudioInterface" )
98102
103+ # Determine URLs based on location
104+ livekit_url = None
105+ api_origin = None
106+ if location is not None :
107+ livekit_url = get_livekit_url_for_location (location )
108+ # Convert WSS to HTTPS for API origin
109+ api_origin = get_origin_for_location (location ).replace ("wss://" , "https://" )
110+
99111 return WebRTCConversation (
100112 client = client ,
101113 agent_id = agent_id ,
102114 user_id = user_id ,
103115 conversation_token = conversation_token ,
116+ livekit_url = livekit_url ,
117+ api_origin = api_origin ,
104118 audio_interface = audio_interface ,
105119 config = config ,
106120 client_tools = client_tools ,
@@ -169,6 +183,7 @@ def create_webrtc_conversation(
169183 user_id : Optional [str ] = None ,
170184 * ,
171185 conversation_token : Optional [str ] = None ,
186+ location : Optional [Location ] = None ,
172187 livekit_url : Optional [str ] = None ,
173188 api_origin : Optional [str ] = None ,
174189 webrtc_overrides : Optional [dict ] = None ,
@@ -185,7 +200,17 @@ def create_webrtc_conversation(
185200 """Create a WebRTC conversation.
186201
187202 Convenience function for creating WebRTC conversations with type safety.
203+
204+ Args:
205+ location: Data residency location. If provided, overrides livekit_url and api_origin.
206+ livekit_url: Custom LiveKit URL (overridden by location if provided).
207+ api_origin: Custom API origin (overridden by location if provided).
188208 """
209+ # Determine URLs based on location if provided
210+ if location is not None :
211+ livekit_url = get_livekit_url_for_location (location )
212+ api_origin = get_origin_for_location (location ).replace ("wss://" , "https://" )
213+
189214 return WebRTCConversation (
190215 client = client ,
191216 agent_id = agent_id ,
@@ -212,6 +237,7 @@ def create_websocket_conversation(
212237 user_id : Optional [str ] = None ,
213238 * ,
214239 requires_auth : bool = True ,
240+ location : Optional [Location ] = None ,
215241 audio_interface : Optional [AudioInterface ] = None ,
216242 config : Optional [ConversationInitiationData ] = None ,
217243 client_tools : Optional [ClientTools ] = None ,
@@ -224,13 +250,17 @@ def create_websocket_conversation(
224250 """Create a WebSocket conversation.
225251
226252 Convenience function for creating WebSocket conversations with type safety.
253+
254+ Args:
255+ location: Data residency location (us, eu-residency, in-residency, global)
227256 """
228257 result = create_conversation (
229258 client = client ,
230259 agent_id = agent_id ,
231260 user_id = user_id ,
232261 connection_type = ConnectionType .WEBSOCKET ,
233262 requires_auth = requires_auth ,
263+ location = location ,
234264 audio_interface = audio_interface ,
235265 config = config ,
236266 client_tools = client_tools ,
0 commit comments