Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(data-imports): classify ElevenLabs 402 as non-retryable (#123039)

Tom Owers341e0733e16d
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/elevenlabs/elevenlabs.py b/products/warehouse_sources/backend/temporal/data_imports/sources/elevenlabs/elevenlabs.py
index a320c6040bae..2897e34d7689 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/elevenlabs/elevenlabs.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/elevenlabs/elevenlabs.py
@@ -205,6 +205,11 @@ _INVALID_API_KEY_MESSAGE = (
"ElevenLabs account settings, then try again."
)
+_PAYMENT_REQUIRED_MESSAGE = (
+ "Your ElevenLabs account is out of credits, or this data needs a plan upgrade. Check your "
+ "ElevenLabs billing and plan, then try again."
+)
+
def _is_missing_permission(response: Response) -> bool:
try:
@@ -257,9 +262,13 @@ def validate_credentials(api_key: str, schema_name: Optional[str] = None) -> tup
)
if status == 401:
return False, _INVALID_API_KEY_MESSAGE
- # A 4xx other than the 401/403 above (a 400 is what users actually hit) is a deterministic client
- # error: the key was rejected and retrying sends the same request, so treat it as an invalid key
- # rather than a transient "try again". 429 is a rate limit and stays retryable below.
+ if status == 402:
+ # Payment required is a billing/plan problem, not a bad key — retrying or rotating the key
+ # can't fix it. Sync-time 402s are handled separately by `get_non_retryable_errors`.
+ return False, _PAYMENT_REQUIRED_MESSAGE
+ # A 4xx other than the 401/402/403 above (a 400 is what users actually hit) is a deterministic
+ # client error: the key was rejected and retrying sends the same request, so treat it as an
+ # invalid key rather than a transient "try again". 429 is a rate limit and stays retryable below.
if 400 <= status < 500 and status != 429:
return False, _INVALID_API_KEY_MESSAGE
# A 429/5xx/unexpected status leaves the key unverified. Don't accept it — surface it so the user
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/elevenlabs/source.py b/products/warehouse_sources/backend/temporal/data_imports/sources/elevenlabs/source.py
index a686d72ad900..5b26ebd8416a 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/elevenlabs/source.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/elevenlabs/source.py
@@ -100,10 +100,11 @@ Grant the following read permissions when creating the key:
def get_non_retryable_errors(self) -> dict[str, str | None]:
return {
- # 401/403 surface as a requests HTTPError when `_fetch_page` calls `raise_for_status()`.
- # Retrying can never satisfy a credential/permission problem, so fail the sync. Match the
- # stable status text and base host, not the per-request path/query.
+ # 401/402/403 surface as a requests HTTPError when `_fetch_page` calls `raise_for_status()`.
+ # Retrying can never satisfy a credential/billing/permission problem, so fail the sync.
+ # Match the stable status text and base host, not the per-request path/query.
"401 Client Error: Unauthorized for url: https://api.elevenlabs.io": "Your ElevenLabs API key is invalid or has been revoked. Create a new key in your ElevenLabs account settings, then reconnect.",
+ "402 Client Error: Payment Required for url: https://api.elevenlabs.io": "Your ElevenLabs account is out of credits, or this data needs a plan upgrade. Check your ElevenLabs billing and plan, then reconnect.",
"403 Client Error: Forbidden for url: https://api.elevenlabs.io": "Your ElevenLabs API key is missing the read permissions needed to sync this data. Grant the required read scopes in your ElevenLabs account settings, then reconnect.",
}
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/elevenlabs/tests/test_elevenlabs.py b/products/warehouse_sources/backend/temporal/data_imports/sources/elevenlabs/tests/test_elevenlabs.py
index e7c4be5c2290..69377fe25555 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/elevenlabs/tests/test_elevenlabs.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/elevenlabs/tests/test_elevenlabs.py
@@ -356,6 +356,8 @@ class TestValidateCredentials:
("bad_key", 401, None, _INVALID_KEY_BODY, False),
# A 400 (the status users actually hit) is a deterministic client error, not transient.
("bad_request_at_create", 400, None, None, False),
+ # Payment required is a billing/plan problem, not a bad key — must not be accepted as valid.
+ ("payment_required_at_create", 402, None, None, False),
("missing_scope_at_create", 403, None, None, True),
("missing_scope_at_schema", 403, "history", None, False),
("missing_permission_401_at_create", 401, None, _MISSING_PERMISSION_BODY, True),
@@ -397,6 +399,15 @@ class TestValidateCredentials:
assert ok is False
assert "Create a new key in your ElevenLabs account settings" in (msg or "")
+ def test_payment_required_message_points_to_billing_not_the_key(self) -> None:
+ # A 402 is a billing/plan problem, not a bad key, so it must not tell the user to rotate the
+ # key the way the 400/401 message above does.
+ with scripted_network([ScriptedResponse(status=402)]):
+ ok, msg = validate_credentials("k")
+ assert ok is False
+ assert "billing" in (msg or "").lower()
+ assert "Create a new key" not in (msg or "")
+
def test_session_does_not_follow_redirects(self) -> None:
# requests keeps the custom xi-api-key header across a cross-origin 3xx; following one would
# replay the key to the redirect target, so the credentialed session must refuse redirects.