Overview
TikTokLive provides a comprehensive error hierarchy to help you handle different failure scenarios when connecting to and interacting with TikTok LIVE streams.Error Hierarchy
All TikTokLive errors inherit from the baseTikTokLiveError class:
Base Error
TikTokLiveError
Inherits from:RuntimeError
Base error class for all TikTokLive errors. All error messages are automatically prefixed with the package version.
Connection Errors
AlreadyConnectedError
Inherits from:TikTokLiveError
Raised when: Attempting to connect to a user that is already connected to.
UserOfflineError
Inherits from:TikTokLiveError
Raised when: The requested streamer is offline.
UserNotFoundError
Inherits from:TikTokLiveError
Raised when: The user does not have a livestream account (e.g., accounts with less than 1,000 followers).
Attributes:
unique_id(str): The unique ID of the user that was not found
AgeRestrictedError
Inherits from:TikTokLiveError
Raised when: A LIVE stream is age-restricted.
Solution: Pass a valid sessionid cookie to bypass age restrictions.
Blocking & Detection Errors
InitialCursorMissingError
Inherits from:TikTokLiveError
Raised when: The cursor for connecting to TikTok is missing, indicating that you may be blocked.
WebsocketURLMissingError
Inherits from:TikTokLiveError
Raised when: The websocket URL to connect to TikTok is missing, indicating that you may be blocked.
WebcastBlocked200Error
Inherits from:TikTokLiveError
Raised when: The webcast is blocked by TikTok with a 200 status code (detected connection).
Sign API Errors
SignAPIError
Inherits from:TikTokLiveError
Raised when: A fetch to the Sign API fails.
Attributes:
reason(ErrorReason): The specific reason for the failureresponse(httpx.Response | None): The HTTP response object from the Sign APIlog_id(str | None): The log ID from the response headersagent_id(str | None): The agent ID from the response headers
RATE_LIMIT: Sign API rate limit exceededCONNECT_ERROR: Connection error to Sign APIEMPTY_PAYLOAD: Empty payload received from Sign APISIGN_NOT_200: Sign API returned non-200 status codeEMPTY_COOKIES: Empty cookies received from Sign APIPREMIUM_ENDPOINT: Premium endpoint access requiredAUTHENTICATED_WS: Authenticated WebSocket connection error
SignatureRateLimitError
Inherits from:SignAPIError
Raised when: You hit the Sign API rate limit.
Attributes:
retry_after(int): Number of seconds to wait before retryingreset_time(int): Unix timestamp when the rate limit resets
UnexpectedSignatureError
Inherits from:SignAPIError
Raised when: The Sign API returns an unexpected response (non-200 status code).
SignatureMissingTokensError
Inherits from:SignAPIError
Raised when: The Sign API returns an empty payload (missing tokens).
PremiumEndpointError
Inherits from:SignAPIError
Raised when: Attempting to access a premium endpoint without proper authorization.
AuthenticatedWebSocketConnectionError
Inherits from:SignAPIError
Raised when: Sending a session ID to the sign server. This is a risky operation that could lead to account bans.
Complete Error Handling Example
Best Practices
-
Always catch specific errors first: Handle specific error types before catching the base
TikTokLiveError. -
Respect rate limits: When catching
SignatureRateLimitError, always wait forretry_afterseconds before retrying. -
Handle blocking gracefully: Blocking errors (
InitialCursorMissingError,WebsocketURLMissingError,WebcastBlocked200Error) indicate TikTok has detected your connection. Implement exponential backoff or switch to a different connection method. -
Use sessionid for age-restricted streams: Always provide a valid
sessionidcookie when connecting to age-restricted content. -
Log error details: For
SignAPIErrorinstances, log thelog_idandagent_idfor debugging purposes. - Don’t ignore errors: Each error type provides important information about why the connection failed. Use this information to improve your connection strategy.