How can I get the string representation of rtc.Dis...
# ask-ai
p
How can I get the string representation of rtc.DisconnectReason on room disconnected event? Right now I get an integer, which is not useful when debugging.
t
Thanks for reaching out, Mario. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
p
I'm using Python SDK btw.
t
To help you get the string representation of
rtc.DisconnectReason
in the Python SDK, you can use the
Name()
method provided by protobuf. Here’s a little example to guide you:
Copy code
from livekit import rtc

# If you have a DisconnectReason value
reason = rtc.DisconnectReason.CLIENT_INITIATED
reason_string = rtc.DisconnectReason.Name(reason)
# This should return "CLIENT_INITIATED"
If you find that the
Name()
method isn’t available, no worries! You can simply try converting it directly to a string like this:
Copy code
reason_string = str(reason)
Please feel free to reach out if you have any more questions or need a hand with this—I’m here to help!
p
Name() is not an attribute of the
rtc.DisconnectReason
that is sent from the room. When I convert to string, I stil get a number.
t
It looks like the
Name()
method isn’t available in your situation. No worries! You can still map the integer values of
rtc.DisconnectReason
to their string names manually. Here’s a little function that can help you do just that:
Copy code
from livekit.rtc import DisconnectReason

def disconnect_reason_to_string(reason_value):
    reason_map = {
        DisconnectReason.UNKNOWN_REASON: "UNKNOWN_REASON",
        DisconnectReason.CLIENT_INITIATED: "CLIENT_INITIATED",
        DisconnectReason.DUPLICATE_IDENTITY: "DUPLICATE_IDENTITY",
        DisconnectReason.SERVER_SHUTDOWN: "SERVER_SHUTDOWN",
        DisconnectReason.PARTICIPANT_REMOVED: "PARTICIPANT_REMOVED",
        DisconnectReason.ROOM_DELETED: "ROOM_DELETED",
        DisconnectReason.STATE_MISMATCH: "STATE_MISMATCH",
        DisconnectReason.JOIN_FAILURE: "JOIN_FAILURE",
        DisconnectReason.MIGRATION: "MIGRATION",
        DisconnectReason.SIGNAL_CLOSE: "SIGNAL_CLOSE",
        DisconnectReason.ROOM_CLOSED: "ROOM_CLOSED",
        DisconnectReason.USER_UNAVAILABLE: "USER_UNAVAILABLE",
        DisconnectReason.USER_REJECTED: "USER_REJECTED",
        DisconnectReason.SIP_TRUNK_FAILURE: "SIP_TRUNK_FAILURE",
        DisconnectReason.CONNECTION_TIMEOUT: "CONNECTION_TIMEOUT",
        DisconnectReason.MEDIA_FAILURE: "MEDIA_FAILURE"
    }
    return reason_map.get(reason_value, "UNKNOWN_REASON")

# Example usage
reason = rtc.DisconnectReason.CLIENT_INITIATED
print(disconnect_reason_to_string(reason))  # Output: "CLIENT_INITIATED"
This way, you’ll be able to cover all the disconnect reasons smoothly. If anything’s unclear or you want me to walk you through it, just let me know—I’m here to help! Sources: livekit-protocol/livekit/protocol/models.pyi | livekit-protocol/src/livekit.rs | agent-console/lib/event-types.ts | packages/livekit-rtc/src/proto/participant_pb.ts
p
I can't believe each user has to do this. Is there a method provided by livekit to at least get the list of enums, so I can map based on the int I get?
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!