The ElevenLabs Python SDK provides a comprehensive set of exception types that map to HTTP status codes. Proper error handling ensures your application can gracefully handle API errors and provide meaningful feedback to users.
Raised when the API key is valid but doesn’t have permission to access the resource.
from elevenlabs import ElevenLabs, ForbiddenErrorclient = ElevenLabs(api_key="YOUR_API_KEY")try: # Attempting to access a premium feature without subscription result = client.some_premium_feature()except ForbiddenError as e: print("Access denied. Upgrade your plan to use this feature.") print(f"Status code: {e.status_code}") # 403
Raised when a request is made too early, typically when a resource is not yet ready.
from elevenlabs import ElevenLabs, TooEarlyErrorimport timeclient = ElevenLabs(api_key="YOUR_API_KEY")try: result = client.some_resource.get(resource_id="id")except TooEarlyError as e: print("Resource not ready yet. Retrying...") time.sleep(5) # Retry logic here
Raised when there’s a conflict with the current state of the resource.
from elevenlabs import ElevenLabs, ConflictErrorclient = ElevenLabs(api_key="YOUR_API_KEY")try: # Attempting to create a resource that already exists voice = client.voices.ivc.create( name="Existing Voice Name", files=["sample.mp3"] )except ConflictError as e: print("A voice with this name already exists.") print(f"Status code: {e.status_code}") # 409
Raised when the request is well-formed but contains semantic errors.
from elevenlabs import ElevenLabs, UnprocessableEntityErrorclient = ElevenLabs(api_key="YOUR_API_KEY")try: audio = client.text_to_speech.convert( text="Test", voice_id="valid_id", model_id="invalid_model" # Valid format but doesn't exist )except UnprocessableEntityError as e: print("The model ID is not valid or not available.") print(f"Status code: {e.status_code}") # 422