Skip to main content

Streaming Responses

Streaming allows you to receive partial responses from the Perplexity API as they are generated, rather than waiting for the complete response. This is particularly useful for real-time user experiences, long responses, and interactive applications.
Streaming is supported across all models available through the Agent API.
To enable streaming, set stream=True (Python) or stream: true (TypeScript) when creating responses:

Error Handling

Handle errors gracefully during streaming:
If you need search results immediately for your user interface, consider using non-streaming requests for use cases where search result display is critical to the real-time user experience.

Background Runs

Streaming keeps a connection open for the lifetime of the run. For runs that take minutes — deep research, heavy sandbox work — submit with background: true instead, then poll for the result by ID. The run continues server-side even if your client disconnects.
For the full background-run lifecycle — submitting, polling on terminal status, streaming with reconnect, and cancelling — see Background mode.
Background runs are durable, so you can also stream one live and reconnect after a drop. Request GET /v1/agent/{id}?stream=true&starting_after=N to resume from the event after sequence number N. Reconnect is only valid within the response’s reconnect window; once that window expires, the endpoint returns 400, and you fall back to a plain GET /v1/agent/{id} for the final snapshot. See the Agent API reference.

Structured Outputs

Structured outputs enable you to enforce specific response formats from Perplexity’s models, ensuring consistent, machine-readable data that can be directly integrated into your applications without manual parsing. We currently support JSON Schema structured outputs. To enable structured outputs, add a response_format field to your request:
The name field is required and must be 1-64 alphanumeric characters. The schema should be a valid JSON schema object. LLM responses will match the specified format unless the output exceeds max_tokens.
Improve Schema Compliance: Give the LLM some hints about the output format in your prompts to improve adherence to the structured format. For example, include phrases like “Please return the data as a JSON object with the following structure…” or “Extract the information and format it as specified in the schema.”
The first request with a new JSON Schema expects to incur delay on the first token. Typically, it takes 10 to 30 seconds to prepare the new schema, and may result in timeout errors. Once the schema has been prepared, the subsequent requests will not see such delay.

Example

Links in JSON Responses: Requesting links as part of a JSON response may not always work reliably and can result in hallucinations or broken links. Models may generate invalid URLs when forced to include links directly in structured outputs.To ensure all links are valid, use the links returned in the citations or search_results fields from the API response. Never count on the model to return valid links directly as part of the JSON response content.

Next Steps

Structure the output

Back to the build flow: enforce structured JSON your code can parse.

Agent API Models

Explore direct model selection and third-party models.