66import json
77import os
88from datetime import datetime
9- from typing import Any , Callable
9+ from typing import Any , Callable , Literal
1010
1111import websockets
1212from openai .types .beta .realtime .conversation_item import ConversationItem
2323from ..logger import logger
2424from .config import (
2525 RealtimeClientMessage ,
26+ RealtimeModelTracingConfig ,
2627 RealtimeSessionModelSettings ,
2728 RealtimeUserInput ,
2829)
@@ -73,6 +74,7 @@ def __init__(self) -> None:
7374 self ._audio_length_ms : float = 0.0
7475 self ._ongoing_response : bool = False
7576 self ._current_audio_content_index : int | None = None
77+ self ._tracing_config : RealtimeModelTracingConfig | Literal ["auto" ] | None = None
7678
7779 async def connect (self , options : RealtimeModelConfig ) -> None :
7880 """Establish a connection to the model and keep it alive."""
@@ -84,6 +86,11 @@ async def connect(self, options: RealtimeModelConfig) -> None:
8486 self .model = model_settings .get ("model_name" , self .model )
8587 api_key = await get_api_key (options .get ("api_key" ))
8688
89+ if "tracing" in model_settings :
90+ self ._tracing_config = model_settings ["tracing" ]
91+ else :
92+ self ._tracing_config = "auto"
93+
8794 if not api_key :
8895 raise UserError ("API key is required but was not provided." )
8996
@@ -96,6 +103,15 @@ async def connect(self, options: RealtimeModelConfig) -> None:
96103 self ._websocket = await websockets .connect (url , additional_headers = headers )
97104 self ._websocket_task = asyncio .create_task (self ._listen_for_messages ())
98105
106+ async def _send_tracing_config (
107+ self , tracing_config : RealtimeModelTracingConfig | Literal ["auto" ] | None
108+ ) -> None :
109+ """Update tracing configuration via session.update event."""
110+ if tracing_config is not None :
111+ await self .send_event (
112+ {"type" : "session.update" , "other_data" : {"session" : {"tracing" : tracing_config }}}
113+ )
114+
99115 def add_listener (self , listener : RealtimeModelListener ) -> None :
100116 """Add a listener to the model."""
101117 self ._listeners .append (listener )
@@ -343,8 +359,7 @@ async def _handle_ws_event(self, event: dict[str, Any]):
343359 self ._ongoing_response = False
344360 await self ._emit_event (RealtimeModelTurnEndedEvent ())
345361 elif parsed .type == "session.created" :
346- # TODO (rm) tracing stuff here
347- pass
362+ await self ._send_tracing_config (self ._tracing_config )
348363 elif parsed .type == "error" :
349364 await self ._emit_event (RealtimeModelErrorEvent (error = parsed .error ))
350365 elif parsed .type == "conversation.item.deleted" :
0 commit comments