agents icon indicating copy to clipboard operation
agents copied to clipboard

Room metadata occasionally missing

Open sw956 opened this issue 1 year ago • 1 comments

I am occasionally running into empty room metadata when the agent connects to a room. The code which creates the room looks something like:

Call<LivekitModels.Room> room_create_call = client.createRoom(id, emptyTimeout, null /* maxParticipants */, null /* nodeId */, room_metadata);
Response<LivekitModels.Room> response = room_create_call.execute();
LivekitModels.Room room = response.body();

The log will correctly display the metadata (metadata: \"{\\\"id\\\": \\\"66e040841d9cdd45f4350930\\\"}\") when logging the job request:

{"message": "registered worker", "level": "INFO", "id": "AW_MaW2CtKGRcz4", "server_info": "edition: Cloud\nversion: \"1.7.2\"\nprotocol: 15\nregion: \"UK\"\nnode_id: \"NC_OLONDON1A_H7YCbSveaF7b\"\n", "timestamp": "2024-09-19T10:11:16.446614+00:00"}
{"message": "received job request", "level": "INFO", "job_request": "id: \"AJ_Kt8zXFmF4cNZ\"
room {
  sid: \"RM_ydvT2weubqVX\"
  name: \"66ebf8c884f7033217395090\"
  empty_timeout: 20
  creation_time: 1726740680
  enabled_codecs {
    mime: \"video/H264\"
  }
  enabled_codecs {
    mime: \"video/VP8\"
  }
  enabled_codecs {
    mime: \"video/VP9\"
  }
  enabled_codecs {
    mime: \"video/AV1\"
  }
  enabled_codecs {
    mime: \"audio/red\"
  }
  enabled_codecs {
    mime: \"audio/opus\"
  }
  metadata: \"{\\\"id\\\": \\\"66e040841d9cdd45f4350930\\\"}\"
  version {
    unix_micro: 1726740680392110
  }
  departure_timeout: 20
}
state {
  updated_at: 1726740681359963211
}
", "resuming": false, "agent_name": "", "timestamp": "2024-09-19T10:11:21.362849+00:00"}

The agent code (assistant.py):

def preload(proc: JobProcess):
    proc.userdata["vad"] = silero.VAD.load()

async def entrypoint(ctx: JobContext):
    await ctx.connect(auto_subscribe=AutoSubscribe.AUDIO_ONLY)

    try:
        room_metadata = json.loads(ctx.room.metadata)
    except Exception as e:
        logger.info(ctx.room.metadata) # logs empty string

if __name__ == "__main__":
    cli.run_app(WorkerOptions(entrypoint_fnc=entrypoint, prewarm_fnc=preload))

Run with

python assistant.py start

Python 3.12.3 with package versions:

livekit                    0.16.2
livekit-agents             0.8.12
livekit-api                0.7.0
livekit-plugins-cartesia   0.4.2
livekit-plugins-deepgram   0.6.7
livekit-plugins-elevenlabs 0.7.4
livekit-plugins-openai     0.8.3
livekit-plugins-silero     0.6.4
livekit-protocol           0.6.0

sw956 avatar Sep 19 '24 10:09 sw956

Is there any update on this issue ?

firattamurlc avatar Oct 27 '24 22:10 firattamurlc

Hello,

Is there any update on this issue ?

firattamurlc avatar Nov 08 '24 11:11 firattamurlc

Hello,

Is there any update on this issue ?

firattamurlc avatar Nov 26 '24 14:11 firattamurlc

@firattamurlc the room info provided in the create request is loaded asynchronously to speed up join time. room metadata may not be available immediately after the client connects.

to reliably receive the room's metadata register a callback for room_metadata_changed events before calling ctx.connect() ex:

def room_metadata_changed(old_metadata, metadata):
    print(metadata)

ctx.room.on("room_metadata_changed", room_metadata_changed)

once the room metadata is available your callback will be invoked with the value you expect.

paulwe avatar Nov 26 '24 16:11 paulwe