in the code ctx = get_job_context() await ctx.api ...
# ask-ai
w
in the code ctx = get_job_context() await ctx.api returns a string. why?
t
Thanks for reaching out, Sunil. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
I'd love to help you out! Could you share a bit more about the context of this code? For instance: • What behavior are you expecting from
ctx.api
? • What kind of object do you think
ctx
is meant to be? • Are you working with a particular library or framework that uses
get_job_context()
? Having these details will really help me get to the bottom of why
ctx.api
is returning a string. Looking forward to your reply!
w
given below is my code try: logger.debug("*******************DebtCollectionAgentFirstContact - end_call INVOKED") await self.session.generate_reply(instructions=f"1. Thank the user for their time.") if workflow_status: self.session.userdata.workflow_status = workflow_status ctx = get_job_context() logger.debug(f"1 :{ctx}") logger.debug(f"2 :{ctx.room.name}") participants = await ctx.api.room.list_participants(ctx.room.name) logger.debug(f"3 :{participants}") logger.debug(f"3 :{participants.ListFields}") for participant in participants: logger.debug(f"removing user :{participant.identity} from room: {ctx.room.name}") await ctx.api.room.remove_participant(ctx.room.name,participant.identity) await self.session.drain() await self.session.aclose() except Exception as e: logger.error(f"######EXCEPTION IN DebtCollectionAgentFirstContact - end_call - {e}") return None the console prints the following: 2025-09-26 131127,012 - DEBUG id_verification_agent - 1 :<livekit.agents.job.JobContext object at 0x000001FC2D6DDB90> 2025-09-26 131127,012 - DEBUG id_verification_agent - 2 :playground-QEZV-exxx 2025-09-26 131127,013 - ERROR id_verification_agent - ######EXCEPTION IN DebtCollectionAgentFirstContact - end_call - 'str' object has no attribute 'room'
t
It looks like the error is happening because
ctx
is coming through as a string instead of a properly initialized
JobContext
object. In the LiveKit Python Agent SDK,
ctx
should be a
JobContext
instance, which includes a
room
attribute (an
rtc.Room
object) and an
api
attribute for server-side operations. To help you get this sorted out, here are a couple of steps you can follow: 1. First, double-check that
ctx
is correctly initialized as a
JobContext
object. It’s a good idea to verify how
ctx
is being passed into your function and make sure it’s not accidentally being overridden or redefined somewhere else in your code. 2. When you’re listing participants, be sure to use the
api
property of the
JobContext
along with the
ListParticipantsRequest
class like this:
Copy code
from livekit.api import ListParticipantsRequest

# Ensure `ctx` is a JobContext object
participants_response = await ctx.api.room.list_participants(
    ListParticipantsRequest(room=ctx.room.name)
)
Remember to pass the room name explicitly through the
room
parameter in
ListParticipantsRequest
. If you still find that
ctx
is unexpectedly a string, it’s worth confirming that it’s being passed correctly from the LiveKit agent framework. In a proper setup, your
entrypoint
function should receive a
JobContext
instance. Sources: livekit-agents/livekit/agents/job.py | livekit-rtc/livekit/rtc/_proto/room_pb2.pyi | telephony/sip_lifecycle.py
w
how to get list of participants in a room using jobcontext?
t
I'd be happy to guide you through getting a list of participants in a room using
JobContext
. Here’s a simple way to do it: 1. First, access the API client via
ctx.api
, which gives you the
LiveKitAPI
instance. 2. Then, use the
list_participants()
method to fetch participant details. Just make sure to pass the room name explicitly using the
ListParticipantsRequest
class. Here’s an example to help you visualize it:
Copy code
from livekit.api import ListParticipantsRequest

# Make sure `ctx` is a JobContext object
participants_response = await ctx.api.room.list_participants(
    ListParticipantsRequest(room=ctx.room.name)
)

# Now, let’s access the participant details
participants = participants_response.participants
for participant in participants:
    print(f"Participant Identity: {participant.identity}")
Each participant object contains useful fields like
identity
,
name
,
state
,
metadata
, and more. You can loop through the list to access all these details. One last thing to keep in mind: please ensure your API credentials have the right permissions to use the
list_participants
API. If you run into any issues or have questions, I’m here to help! Sources: livekit-agents/livekit/agents/job.py | livekit-api/livekit/api/room_service.py | mcp/server.py | Managing participants | LiveKit Docs
w
give me the code to remove all participants from a room
t
Hi there! If you'd like to remove all participants from a room but keep the room itself intact, you can use the
remove_participant
API method for each participant. Here's a simple Python example to guide you through it:
Copy code
from livekit import api

async def remove_all_participants(ctx):
    # First, get a list of all participants in the room
    participants_response = await ctx.api.room.list_participants(
        api.ListParticipantsRequest(room=ctx.room.name)
    )

    # Then, remove each participant one by one
    for participant in participants_response.participants:
        await ctx.api.room.remove_participant(
            api.RemoveParticipantRequest(
                room=ctx.room.name,
                identity=participant.identity,
            )
        )
On the other hand, if your goal is to disconnect everyone by deleting the room completely, you can use the
delete_room
method like this:
Copy code
from livekit import api

async def delete_room(ctx):
    await ctx.api.room.delete_room(
        api.DeleteRoomRequest(room=ctx.room.name)
    )
Feel free to choose the approach that best fits what you want to achieve—whether it's keeping the room ready for future use or ending the session entirely. If you have any questions or need further help, I'm here for you! Sources: livekit-api/livekit/api/room_service.py | packages/livekit-server-sdk/src/RoomServiceClient.ts | Inside a session | LiveKit Docs
w
ImportError: cannot import name 'RemoveParticipantRequest' from 'livekit.api'
t
I don't have the answer you're looking for. You could also try asking your question: • in one of the other Slack channels or • to https://deepwiki.com/livekit/livekit_composite which is trained on all LiveKit source code If you find the answer, please post it here to help others!
w
there is no api.RemoveParticipantRequest