async def entrypoint(_ctx_: JobContext): """Ma...
# ask-ai
c
async def entrypoint(_ctx_: JobContext): """Main entrypoint for the LiveKit agent""" _try_: # Connect to the job context await ctx.connect() logger.info("Connected to LiveKit job context") # Check if room is connected before proceeding if not ctx.room.isconnected(): logger.warning("Room is not connected, waiting for connection...") # Wait a bit for connection to establish await asyncio.sleep(1) if not ctx.room.isconnected(): logger.error("Room connection failed") return logger.info(f"Room connected: {ctx.room.name}") # Initialize restaurant agent await restaurant_agent.initialize() # Wait for participant to join with timeout _try_: await asyncio.wait_for(ctx.wait_for_participant(), timeout=60.0) except asyncio.TimeoutError: logger.warning("No participant joined within 60 seconds") return # Check if we have participants if not ctx.room.remote_participants: logger.warning("No remote participants found") return # Get the first participant participant = ctx.room.remote_participants[0] logger.info(f"Participant joined: {participant.identity}") # Extract phone number from participant identity if available phone_number = None if hasattr(participant, 'identity') and participant.identity: # Try to extract phone number from identity (format: "phone:+1234567890") if participant.identity.startswith('phone:'): phone_number = participant.identity[6:] # Remove "phone:" prefix # Check for spam if restaurant_agent.is_spam_call(phone=phone_number, _room_name_=ctx.room.name): await restaurant_agent.handle_spam_call(phone=phone_number, _room_name_=ctx.room.name) return # Record this call attempt restaurant_agent.record_call_attempt(phone=phone_number, _room_name_=ctx.room.name) # Reset activity timer restaurant_agent.last_customer_activity = time.time() restaurant_agent.conversation_start_time = time.time() # Create agent session with specific models session = AgentSession( llm=openai.LLM(model="gpt-4o"), tts=inworld.TTS(model="Ashley"), stt=deepgram.STT(model="nova-3"), ) # Create the assistant agent with function tools assistant = Agent( _entrypoint_fnc_=restaurant_agent.get_system_prompt(), tools=[ restaurant_agent.create_reservation, restaurant_agent.cancel_reservation, restaurant_agent.update_reservation, restaurant_agent.create_order, restaurant_agent.get_customer_info, restaurant_agent.check_availability, restaurant_agent.end_call, restaurant_agent.thank_you_and_end, ] ) # Start auto-hangup monitoring task async def monitor_activity(): while True: _try_: await asyncio.sleep(5) # Check every 5 seconds # Check if room is still connected if not ctx.room.isconnected(): logger.info("Room disconnected, stopping monitoring") break # Check if we should auto hangup if await restaurant_agent.check_and_hangup_if_needed(): break # Check if conversation has been going too long conversation_duration = time.time() - restaurant_agent.conversation_start_time if conversation_duration > settings.max_conversation_duration: logger.info("Conversation timeout reached, hanging up") await restaurant_agent.end_call_with_thanks() break except asyncio.CancelledError: break except Exception as e: logger.error(f"Error in activity monitor: {e}") # Start the assistant await session.start(agent=assistant, room=ctx.room) # Start monitoring task monitor_task = asyncio.create_task(monitor_activity()) _try_: # Keep running until participant leaves or auto-hangup await ctx.wait_for_disconnect() _finally_: # Clean up monitoring task monitor_task.cancel() _try_: await monitor_task except asyncio.CancelledError: pass # Properly shutdown the job context await ctx.shutdown() logger.info("Job context shutdown completed") except Exception as e: logger.error(f"Error in entrypoint: {e}") # Ensure shutdown even on error _try_: await ctx.shutdown() except Exception as shutdown_error: logger.error(f"Error during shutdown: {shutdown_error}") raise ERROR main Error during shutdown: object NoneType can't be used in 'await' expression
t
Thanks for reaching out, Hai. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
The error "object NoneType can't be used in 'await' expression" usually happens when you try to use
await
on something that is actually
None
, which means it’s not a coroutine or something you can await. Let’s walk through some common situations where this might occur: • Awaiting a Non-Awaitable Function: Make sure all your asynchronous SDK methods are properly awaited and defined with
async def
. • Incorrect Handling of RPC Methods: Double-check that methods like
perform_rpc
are being awaited correctly. • Missing
async
in Registered Callbacks:
Event handlers should be declared as
async def
to avoid them returning
None
unexpectedly. • Awaiting Non-Coroutine Objects: Confirm that objects like
SpeechHandle
are fully initialized before you try to await them. Here are some steps that can help you resolve this: 1. Make sure all asynchronous methods are awaited and actually return valid coroutine objects. 2. Check that your event handlers and callbacks are defined with
async def
. 3. Use logging or debugging tools to see if any function might be returning
None
by mistake. 4. Ensure that objects are properly initialized before you await them. If you’re still running into trouble, don’t hesitate to share your relevant code snippet, along with your LiveKit and Python versions, and any logs you have. I’m here to help you get this sorted out! Sources: livekit-rtc/livekit/rtc/audio_stream.py | livekit-agents/livekit/agents/voice/speech_handle.py | Still Have Questions