1010from fastapi import FastAPI , HTTPException , Request , Response
1111from openai import APIStatusError , AsyncOpenAI , InvalidWebhookSignatureError
1212
13+ from agents .realtime .config import RealtimeSessionModelSettings
1314from agents .realtime .items import (
1415 AssistantAudio ,
1516 AssistantMessageItem ,
@@ -89,9 +90,7 @@ async def accept_call(call_id: str) -> None:
8990 except Exception : # noqa: BLE001
9091 detail = str (exc .response )
9192
92- logger .error (
93- "Failed to accept call %s: %s %s" , call_id , exc .status_code , detail
94- )
93+ logger .error ("Failed to accept call %s: %s %s" , call_id , exc .status_code , detail )
9594 raise HTTPException (status_code = 500 , detail = "Failed to accept call" ) from exc
9695
9796 logger .info ("Accepted call %s" , call_id )
@@ -103,17 +102,16 @@ async def observe_call(call_id: str) -> None:
103102 runner = RealtimeRunner (assistant_agent , model = OpenAIRealtimeSIPModel ())
104103
105104 try :
106- initial_settings = {
105+ initial_model_settings : RealtimeSessionModelSettings = {
107106 "turn_detection" : {
108107 "type" : "semantic_vad" ,
109108 "interrupt_response" : True ,
110109 }
111110 }
112-
113111 async with await runner .run (
114112 model_config = {
115113 "call_id" : call_id ,
116- "initial_model_settings" : initial_settings ,
114+ "initial_model_settings" : initial_model_settings ,
117115 }
118116 ) as session :
119117 # Trigger an initial greeting so callers hear the agent right away.
@@ -124,12 +122,14 @@ async def observe_call(call_id: str) -> None:
124122 RealtimeModelSendRawMessage (
125123 message = {
126124 "type" : "response.create" ,
127- "response" : {
128- "instructions" : (
129- "Say exactly '"
130- f"{ WELCOME_MESSAGE } "
131- "' now before continuing the conversation."
132- )
125+ "other_data" : {
126+ "response" : {
127+ "instructions" : (
128+ "Say exactly '"
129+ f"{ WELCOME_MESSAGE } "
130+ "' now before continuing the conversation."
131+ )
132+ }
133133 },
134134 }
135135 )
@@ -139,15 +139,21 @@ async def observe_call(call_id: str) -> None:
139139 if event .type == "history_added" :
140140 item = event .item
141141 if isinstance (item , UserMessageItem ):
142- for content in item .content :
143- if isinstance (content , InputText ) and content .text :
144- logger .info ("Caller: %s" , content .text )
142+ for user_content in item .content :
143+ if isinstance (user_content , InputText ) and user_content .text :
144+ logger .info ("Caller: %s" , user_content .text )
145145 elif isinstance (item , AssistantMessageItem ):
146- for content in item .content :
147- if isinstance (content , AssistantText ) and content .text :
148- logger .info ("Assistant (text): %s" , content .text )
149- elif isinstance (content , AssistantAudio ) and content .transcript :
150- logger .info ("Assistant (audio transcript): %s" , content .transcript )
146+ for assistant_content in item .content :
147+ if isinstance (assistant_content , AssistantText ) and assistant_content .text :
148+ logger .info ("Assistant (text): %s" , assistant_content .text )
149+ elif (
150+ isinstance (assistant_content , AssistantAudio )
151+ and assistant_content .transcript
152+ ):
153+ logger .info (
154+ "Assistant (audio transcript): %s" ,
155+ assistant_content .transcript ,
156+ )
151157 elif event .type == "error" :
152158 logger .error ("Realtime session error: %s" , event .error )
153159
0 commit comments