Learning resourcesReviewed · Perplexity

Perplexity SDK Error Handling (Python and TypeScript)

This is a documentation page on docs.perplexity.ai that teaches developers how to catch and recover from errors when calling the Perplexity API through its Python and TypeScript SDKs. Its main catch is that it is a single page that states no SDK version, pricing or rate-limit numbers. Runtime correctness and compatibility with current SDK releases remain unverified from this page. It lists the common exception classes, explains the SDKs' default retries and how they handle the Retry-After header, and gives examples for retries, timeouts, logging and fallback responses.

Researched 15 Nov 2026

In short.

This is Perplexity's documentation page on SDK error handling, written for developers who call the Perplexity API from Python or TypeScript. Its main catch is that it is one page with no SDK version, pricing, rate-limit figures or licence terms. Runtime correctness and compatibility with current SDK releases remain unverified from this page. The page lists the common exception types, which inherit from PerplexityError, and maps the HTTP-status exceptions to status codes. It says the SDKs already retry connection errors, 408, 409, 429 and 5xx responses twice by default. It then shows matching Python and TypeScript examples for: custom retries using Retry-After and exponential backoff; debugging details such as the X-Request-ID header; fallback responses when a call fails; logging; timeouts; and authentication errors.

Researched and written by an AI model and checked by a second AI model. How it works

The scope is the complete single documentation page at [https://docs.perplexity.ai/docs/sdk/error-handling.md]. It excludes the contents of the linked Configuration page [https://docs.perplexity.ai/docs/sdk/configuration.md], the Best Practices page [https://docs.perplexity.ai/docs/sdk/best-practices.md] and the documentation index at [https://docs.perplexity.ai/llms.txt]. The embedded sample search responses are illustrative data only. Runtime correctness and compatibility with current SDK releases remain unverified from this page. The page does not give pricing, billing for failed or retried requests, rate-limit numbers, SDK versions, installation commands, the Python distribution name or licence terms, so these remain unknown.

The overview

What this resource is

This resource is a single documentation page titled Error Handling, at [https://docs.perplexity.ai/docs/sdk/error-handling.md]. The page describes itself as a guide to handling API errors gracefully with the Perplexity SDKs for Python and TypeScript. It is documentation, not a library, so there is nothing to install from this page itself.

The page opens with a note pointing to a documentation index at [https://docs.perplexity.ai/llms.txt]. The contents of that index and of the other linked pages are outside the supplied evidence. The page gives the TypeScript package name, @perplexity-ai/perplexity_ai. For Python, its examples use the perplexity module and the Perplexity client constructor (import perplexity and from perplexity import Perplexity). The page gives no installation commands, does not name the Python distribution (the package name you would install) and states no SDK version number.

Overall assessment

Judged by its text, this is a focused, practical page. The page lists common SDK exception types, which inherit from PerplexityError, and maps HTTP-status exceptions to status codes. It also explains what the SDK does automatically before you write any retry code. Its examples cover several real calls (client.search.create and client.responses.create) in both languages.

Its limits are scope and verification. It is one page, it gives no numeric rate limits or quotas, and it does not say which SDK versions its statements apply to. Runtime correctness and compatibility with current SDK releases remain unverified from this page. A few of the examples are deliberately simple, and the limitations section notes where.

Who it is for

The audience is developers who already call, or plan to call, the Perplexity API through the official Python or TypeScript SDK. A beginner can follow it because the examples are short try/except blocks in Python and try/catch blocks in TypeScript. An experienced developer gets the specifics that matter: the retry defaults, how each SDK treats Retry-After, how to read request IDs, and how to set timeouts.

It is not aimed at people who use Perplexity only as a chat app, or who call the REST API directly without an SDK. The page does not cover other languages.

What the page teaches

Exception types. The page lists common SDK exception types, which inherit from PerplexityError, and maps HTTP-status exceptions to status codes. APIConnectionError covers network problems. Timeouts raise APITimeoutError in Python and APIConnectionTimeoutError in TypeScript. The page does not say outright how those timeout classes relate to APIConnectionError. HTTP status failures use APIStatusError in Python and APIError in TypeScript. More specific classes map to status codes: BadRequestError (400), AuthenticationError (401), PermissionDeniedError (403), NotFoundError (404), ConflictError (409), UnprocessableEntityError (422), RateLimitError (429) and InternalServerError (500 and above). APIResponseValidationError exists in Python only and signals a response that did not match the expected schema. Connection, timeout and response-validation errors are not assigned an HTTP status.

Built-in retries. The page says the SDKs retry connection errors, 408, 409, 429 and 5xx responses twice by default, and wait for the Retry-After interval when the server sends one. The Python SDK follows Retry-After only when it is 60 seconds or less and otherwise uses its own backoff. The TypeScript SDK follows any value.

Custom handling. It shows a retry wrapper that honours Retry-After on a 429 and falls back to exponential backoff with jitter when the header is missing. It also shows a shorter jittered delay of approximately one to two seconds for connection errors. Other examples cover pulling error details (message, body, raw response text and the X-Request-ID header), returning a fallback response, structured logging, setting timeouts and showing a friendly message when authentication fails.

Using it

Requirements

To use the examples you need a Perplexity API key and one of the two SDKs. The Python examples use the perplexity module and the Perplexity client constructor. For TypeScript, the page gives the package name as @perplexity-ai/perplexity_ai. The page's authentication advice refers to a PERPLEXITY_API_KEY environment variable, and the examples create the client without passing a key, which implies the SDK reads the key from the environment. The page does not spell out that behaviour.

The Python timeout example also imports httpx, which suggests the Python SDK uses httpx for HTTP. The page does not state installation commands, the Python distribution name or supported language and runtime versions.

How the error handling works

The SDKs raise typed exceptions, and you catch them in order from most specific to least specific. In the first Python example, APIConnectionError and RateLimitError are caught before the general APIStatusError. In TypeScript you test the error with instanceof against classes on the Perplexity import, for example Perplexity.RateLimitError.

Under the documented default settings, the SDK has already retried eligible failures twice before an exception reaches your code. So an error that reaches your code either kept failing after those attempts or is not in the retried set, such as 400, 401, 403, 404 and 422. This follows from the page's retry list.

For a custom retry on 429, the examples read the retry-after header from the error. Python converts it with float(). If the header is missing, it waits 2 to the power of the attempt number plus a random 0 to 1 second. TypeScript converts it with Number() and uses the same kind of fallback in milliseconds. Connection errors get a jittered delay of about 1 to 2 seconds. The TypeScript wrapper does not retry other errors. The wrapper makes up to three SDK calls in total, and under the documented default settings each of those calls can itself include an initial request plus two automatic retries.

Setup

The page gives no installation steps. It points to a separate Configuration page for timeouts and retries and a Best Practices page for environment variables and rate limiting. The contents of those pages are outside the supplied evidence.

Going by the examples, setup has four steps: import the SDK, create a client, wrap calls in try/except (Python) or try/catch with instanceof checks (TypeScript), and optionally set a timeout on the client. The Python timeout example uses httpx.Timeout with connect 5.0, read 30.0, write 5.0 and pool 10.0 seconds. The TypeScript example sets timeout to 30000, which the page labels as 30 seconds.

Worked example from the page

The core Python example creates a client, calls client.search.create with a query, and prints search.results. It then catches perplexity.APIConnectionError (printing e.__cause__), perplexity.RateLimitError and finally perplexity.APIStatusError (printing e.status_code and e.response). The TypeScript version does the same with Perplexity.APIConnectionError, RateLimitError and APIError, reading error.cause, error.status and error.error, and re-throwing anything else.

The retry example wraps the same call in a loop of up to three attempts. On a RateLimitError it waits the number of seconds in Retry-After if the header is present, and otherwise waits an exponential delay with jitter. On the last attempt it re-raises. The page presents this as a pattern. Runtime correctness and compatibility with current SDK releases remain unverified from this page.

The page also embeds two sample search responses. They are plain JSON with an id, a list of results (title, url, snippet, date, last_updated) and a server_time field of null. They illustrate the shape of a successful search call and are not part of the error-handling behaviour.

Assessment & evidence

Limitations and cautions

Scope: the page covers error handling only, not the full API. It gives no numeric rate limits, no error-body schema and no list of error codes beyond HTTP statuses. It does not say which SDK versions it describes.

Retries can stack. The custom wrapper makes up to three SDK calls in total. Under the documented default settings, each call can itself make an initial request plus two retries. So a persistent failure of a retried type, such as repeated 429s or connection errors, could produce up to nine underlying request attempts. The page does not say this; it follows from combining its statement about default retries with the wrapper code. The page links to Configuration for tuning retries, but that page's contents are outside the supplied evidence.

Retry-After parsing: both custom examples read the header as a number of seconds. The page does not show handling for a Retry-After value given as an HTTP date. In the examples, a non-numeric value falls back to exponential backoff.

The fallback-response examples return the same text for rate limits, connection errors and, in the catch-all branch, everything else. In the Python version that includes authentication errors, which would be hidden behind the fallback apart from a printed message. Each call also builds a new client. Treat these as simplified teaching samples.

The Python timeout example sets four httpx timeouts, but the page does not explain how they interact with SDK retries. The behaviour described is what the documentation claims. Runtime correctness and compatibility with current SDK releases remain unverified from this page.

Reasoning behind the assessment

The page is useful because it states concrete behaviour that can be checked: named exception types, with HTTP-status exceptions mapped to status codes, a default of two retries on a stated set of statuses, and different Retry-After rules in Python and TypeScript. It then shows how to build on that behaviour. The Python and TypeScript versions line up closely, so a team using both gets consistent guidance.

The assessment is limited to what this single page shows. It gives no version numbers, pricing or licence text. Runtime correctness and compatibility with current SDK releases remain unverified from this page. So the page shows what the documentation claims, not that every claim holds in current SDK releases.

Alternatives

The page does not compare any alternative tools or approaches. It points to two related Perplexity documentation pages: Configuration [https://docs.perplexity.ai/docs/sdk/configuration.md], for timeouts and retries, and Best Practices [https://docs.perplexity.ai/docs/sdk/best-practices.md], for environment variables and rate limiting. Their full contents are outside the supplied evidence. If you call the API directly without an SDK, the exception classes on this page would not apply.

Evidence base

The evidence covers this single documentation page at [https://docs.perplexity.ai/docs/sdk/error-handling.md]. The contents of the linked Configuration and Best Practices pages and of the documentation index are excluded. Points that go beyond the page text are marked as inferences. The page's two embedded JSON sample responses are search results on unrelated topics (reasoning-model benchmarks and the discovery of penicillin) and only illustrate the shape of a response. Runtime correctness and compatibility with current SDK releases remain unverified from this page.

Access & freshness

Costs

The page states no prices, quotas or rate-limit numbers. From this page alone it is unknown whether reading the documentation costs anything, what API calls cost or what an API key costs.

The page also does not say whether failed or automatically retried requests are billed. So the cost of the SDK's default retries, and of any custom retry wrapper, is unknown. It does warn that aggressive retry loops without delays can worsen rate limiting.

Rights and licence

The page contains no licence or reuse statement for the documentation text or its code samples, so the terms for copying them into your own projects are unknown. The page also does not state the SDK packages' own licences. No library files are distributed from this source.

Freshness

The page text was captured on 2026-11-15. Its publication date, its revision date and the SDK versions it applies to are unknown. The last_updated values in the embedded sample responses describe the search results themselves, so they do not date the page or the samples. Retry defaults and the Python 60-second Retry-After rule are the kind of detail that can change between SDK releases, so check the current page and your installed SDK version before relying on them.

Suggested actions

If you use the Perplexity SDKs, read this page before writing retry code and decide whether the built-in two retries are enough. If you add custom retries, honour Retry-After, add jitter and cap the number of attempts, as the page's guidance and examples do.

Catch specific exceptions before the base class. Log the status, message and X-Request-ID. Set explicit timeouts. Show a clear message on AuthenticationError instead of hiding it behind a fallback.

Confirm the behaviour against your installed SDK version and the linked Configuration page, and test the examples in your own environment. Check pricing and rate limits elsewhere, since this page does not give them.

Evidence for this report

selected-page.txtselected · checked 2026-11-15