{"id":2573,"library":"livekit","title":"LiveKit Real-time SDK for Python","description":"The LiveKit Python SDK provides client-side functionality for building real-time audio, video, and data applications using the LiveKit platform. It supports connecting to LiveKit rooms, managing participants, publishing and subscribing to media tracks, and handling various room events. The current version is 1.1.5, with frequent releases addressing bug fixes, performance improvements, and new features.","status":"active","version":"1.1.5","language":"en","source_language":"en","source_url":"https://github.com/livekit/python-sdks/","tags":["real-time","webrtc","audio","video","data-stream","sdk","asyncio"],"install":[{"cmd":"pip install livekit","lang":"bash","label":"Install stable version"}],"dependencies":[],"imports":[{"symbol":"Room","correct":"from livekit import rtc"},{"symbol":"AccessToken","correct":"from livekit import access_token as at"},{"symbol":"VideoGrants","correct":"from livekit import access_token as at"}],"quickstart":{"code":"import asyncio\nimport os\nfrom livekit import rtc, access_token as at\n\nasync def main():\n    # Environment variables for LiveKit server URL and API credentials\n    livekit_url = os.environ.get('LIVEKIT_URL', 'ws://localhost:7880')\n    api_key = os.environ.get('LIVEKIT_API_KEY', 'devkey')\n    api_secret = os.environ.get('LIVEKIT_API_SECRET', 'secret')\n\n    # Participant identity and room name\n    identity = \"my-python-participant\"\n    room_name = \"my-test-room\"\n\n    # Create an access token with necessary grants\n    grants = at.VideoGrants(room_join=True, room=room_name)\n    token = at.AccessToken(api_key, api_secret).with_identity(identity).with_grants(grants).to_jwt()\n\n    # Initialize a Room\n    room = rtc.Room()\n\n    # Define event handlers\n    @room.on(\"participant_connected\")\n    def on_participant_connected(participant: rtc.RemoteParticipant):\n        print(f\"Participant connected: {participant.identity} (sid: {participant.sid})\")\n\n    @room.on(\"participant_disconnected\")\n    def on_participant_disconnected(participant: rtc.RemoteParticipant):\n        print(f\"Participant disconnected: {participant.identity}\")\n\n    @room.on(\"room_disconnected\")\n    def on_room_disconnected():\n        print(\"Room disconnected\")\n\n    try:\n        print(f\"Connecting to LiveKit room: {room_name} at {livekit_url}\")\n        await room.connect(livekit_url, token, rtc.RoomOptions())\n        print(f\"Connected to room {room.name} as {room.local_participant.identity}\")\n\n        # Keep the room alive for a short period or until manually disconnected\n        print(\"Staying in room for 10 seconds...\")\n        await asyncio.sleep(10)\n\n    except Exception as e:\n        print(f\"Error connecting to room: {e}\")\n    finally:\n        print(\"Disconnecting from room...\")\n        await room.disconnect()\n        print(\"Disconnected.\")\n\nif __name__ == \"__main__\":\n    # LiveKit requires Python 3.9+\n    if os.environ.get('LIVEKIT_URL') and os.environ.get('LIVEKIT_API_KEY') and os.environ.get('LIVEKIT_API_SECRET'):\n      asyncio.run(main())\n    else:\n      print(\"Please set LIVEKIT_URL, LIVEKIT_API_KEY, and LIVEKIT_API_SECRET environment variables.\")","lang":"python","description":"This quickstart demonstrates how to connect to a LiveKit room, generate an access token, listen for basic room events (participant connected/disconnected, room disconnected), and properly disconnect. Ensure you have `LIVEKIT_URL`, `LIVEKIT_API_KEY`, and `LIVEKIT_API_SECRET` environment variables set to connect to your LiveKit server."},"warnings":[{"fix":"Always use `await` when calling `async` methods (e.g., `await room.connect()`, `await room.disconnect()`). Run your main application logic within `asyncio.run(main_async_function())`.","message":"The LiveKit Python SDK is built asynchronously using `asyncio`. Forgetting to `await` async functions or not running the main application within an `asyncio` event loop is a common mistake that will prevent the SDK from functioning correctly.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Double-check the `api_key`, `api_secret`, `identity`, and `VideoGrants` (e.g., `room_join=True`, `room=room_name`) when creating tokens. LiveKit's server logs can provide insights into token validation failures.","message":"Incorrectly generating `AccessToken` with invalid `identity`, `name`, `grants`, or an expired time can lead to connection failures or unauthorized access to LiveKit rooms. Ensure participant identity is unique and grants match required permissions.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Review custom frame processing logic to ensure compatibility with `bytes` data types and proper memory handling. Explicitly convert `memoryview` to `bytes` if necessary to avoid unexpected data format issues.","message":"Versions 1.1.1 and 1.1.2 introduced changes to how `memoryview` and `bytes` are handled in `AudioFrame` and `VideoFrame` (e.g., `materialize sliced memoryviews`, `normalize memoryview format`). This primarily affects applications directly manipulating raw audio/video data.","severity":"breaking","affected_versions":"1.1.1, 1.1.2, and later"},{"fix":"Always ensure `await room.disconnect()` is called, ideally within a `finally` block or an appropriate cleanup routine, to gracefully terminate the connection.","message":"Failing to explicitly call `await room.disconnect()` will leave the client connected to the LiveKit server, consuming resources and potentially delaying cleanup or leading to unexpected behavior on subsequent connection attempts.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-10T00:00:00.000Z","next_check":"2026-07-09T00:00:00.000Z"}