When i create room session via api and join a sess...
# ask-ai
p
When i create room session via api and join a session with agent and the session being recorded and then i leave the room , the session not being closed and the recording still working
t
Thanks for reaching out, Yassine. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
To ensure that a room session is closed and the recording stops when all participants leave, you can use the RoomComposite option for composite recording. This feature automatically ties the recording's lifecycle to the room's activity. Here’s how you can address this: • RoomComposite Recording: This type of recording starts when the room is created and stops automatically when all participants leave the room. • Event Handling: Add event listeners to monitor when participants leave the room. When the last participant leaves, ensure the room is closed to stop the recording. For implementation, you can configure the
RoomCompositeEgressRequest
with the desired parameters (e.g., room name, layout, and storage configuration). Additionally, ensure that the room is closed when the last participant leaves by calling
room.Close()
. If you need further assistance with the setup or code examples, let me know!
p
Yes give me example of setup code
t
Here’s a friendly example to help you set up RoomComposite recording in LiveKit:
Copy code
import os
from livekit import api

async def entrypoint(ctx):
    # Set up RoomComposite recording
    req = api.RoomCompositeEgressRequest(
        room_name="my-room",
        layout="speaker",  # Choose layout (e.g., "speaker", "grid")
        preset=api.EncodingOptionsPreset.H264_720P_30,  # Video encoding preset
        audio_only=False,  # Set to True if you only need audio
        segment_outputs=[
            api.SegmentedFileOutput(
                filename_prefix="my-output",
                playlist_name="my-playlist.m3u8",
                live_playlist_name="my-live-playlist.m3u8",
                segment_duration=5,  # Duration of each segment in seconds
                gcp=api.GCPUpload(
                    bucket="my-bucket",  # Your Google Cloud Storage bucket
                    credentials="path/to/credentials.json",  # Path to GCP credentials
                ),
            )
        ],
    )

    # Initialize LiveKit API
    lkapi = api.LiveKitAPI()

    # Start RoomComposite recording
    res = await lkapi.egress.start_room_composite_egress(req)

    # Listen for participant disconnections
    async def on_participant_disconnected(participant):
        # Check if this is the last participant
        if len(ctx.room.GetParticipants()) == 1:
            # Close the room to stop recording
            await ctx.room.CloseIfEmpty()

    ctx.room.on("participant_disconnected", on_participant_disconnected)

    # Start the agent's logic here
    # ...
Here’s a quick rundown: • RoomCompositeEgressRequest: This sets up the recording with your room name, layout, and where you want to save it. • Event Listener: Keeps an eye on when participants leave and closes the room to stop recording if it’s empty. • Storage: This example uses Google Cloud Storage, but feel free to swap it out for Amazon S3 or Azure Blob Storage if that’s what you prefer. If anything’s unclear or you want me to tailor this example to your specific needs, just let me know—I’m happy to help!
p
But why when i left and the agent still on the room it not being closed auto
t
Just a heads up—the room won’t close automatically if there are still participants inside, like the agent. The RoomComposite recording is set up to stop only when the room is completely empty. So, if the agent is still there, the session and recording will keep going. To handle this, you can add some logic to close the room when certain conditions are met, like when all non-agent users leave. For example, you might keep track of participant roles and close the room once only the agent remains:
Copy code
async def on_participant_disconnected(participant):
    # Check if only the agent remains in the room
    non_agent_participants = [p for p in ctx.room.GetParticipants() if p.role != "agent"]
    if len(non_agent_participants) == 0:
        # Close the room to stop recording
        await ctx.room.CloseIfEmpty()

ctx.room.on("participant_disconnected", on_participant_disconnected)
This way, the room will close and the recording will stop as soon as all the non-agent participants have left, even if the agent is still there. If you want, I’m happy to help you set this up or explain it further—just let me know!
p
i try this but i get this error ception was never retrieved future: <Task finished name='Task-292' coro=<entrypoint.<locals>.on_participant_disconnected_wrapper.<locals>.on_participant_disconnected_async() done, defined at /home/client/astroTest/voice-pipeline-agent-python/main.py:130> exception=AttributeError("'Room' object has no attribute 'GetParticipants'")> Traceback (most recent call last): File "/home/client/astroTest/voice-pipeline-agent-python/main.py", line 132, in on_participant_disconnected_async participants = ctx.room.GetParticipants() ^^^^^^^^^^^^^^^^^^^^^^^^ AttributeError: 'Room' object has no attribute 'GetParticipants'. Did you mean: 'num_participants'? {"pid": 47988, "job_id": "AJ_nHBHzuYAWF8z"} 2025-08-03 032245,153 - DEBUG livekit.agents - session closed {"reason": "participant_disconnected", "error": null, "pid": 47988, "job_id": "AJ_nHBHzuYAWF8z"} 2025-08-03 032306,120 - INFO livekit.agents - process exiting {"reason": "", "pid": 47988, "job_id": "AJ_nHBHzuYAWF8z"} 2025-08-03 032306,120 - DEBUG livekit.agents - shutting down job task {"reason": "", "user_initiated": false, "pid": 47988, "job_id": "AJ_nHBHzuYAWF8z"} 2025-08-03 032306,124 - WARNING livekit - livekit:rtc engine453livekit:rtc_engine - received session close: "signal client closed: \"stream closed\"" UnknownReason Resume {"pid": 47988, "job_id": "AJ_nHBHzuYAWF8z"}
t
Hey there! The error you're seeing happens because the
Room
object doesn’t have a method called
GetParticipants
. No worries though! You can use the
listParticipants
method from the
RoomServiceClient
to get the list of participants in a room. Here’s a simple example in Python to help you out: