Shape the output - Perplexity
Summary
Perplexity's API offers robust controls for managing output, addressing common needs beyond a single text block. Developers can enable streaming by setting "stream: true", receiving incremental Server-Sent Events with distinct types like "response.output_text.delta" for text chunks and "response.completed" for final usage data. For long-running tasks, "background: true" allows server-side execution and polling, supporting durable streams that can be reconnected using "GET /v1/responses/{id}?stream=true&starting_after=N". Response length is managed via "max_output_tokens" for a hard token cap and "text.verbosity" (low, medium, high) for soft preferences. Crucially, structured JSON output can be enforced using "response_format" with a defined "json_schema", ensuring machine-readable results and improving downstream system integration.
Key takeaway
For AI Engineers integrating the Perplexity API, understanding output control is crucial for optimizing application performance and user experience. You should implement streaming for interactive interfaces or long answers, and utilize background runs for computationally intensive tasks to maintain client responsiveness. Use structured JSON output with a defined schema to ensure seamless integration with downstream systems, avoiding brittle parsing and improving data reliability.
Key insights
Perplexity API offers granular control over response delivery and format, including streaming, background processing, and structured JSON.
Principles
- Stream responses for interactive UIs.
- Use background runs for long tasks.
- Define JSON schema for structured output.
Method
To control Perplexity API output, set "stream: true" for incremental events, "background: true" for server-side processing, or define "response_format" with a "json_schema" object. Manage length with "max_output_tokens" or "text.verbosity".
In practice
- Implement chat UIs with streaming.
- Run deep research tasks in background.
- Integrate model output directly into systems.
Topics
- Perplexity API
- Streaming API
- JSON Schema
- Background Processing
- Output Control
- Server-Sent Events
Best for: AI Engineer, Machine Learning Engineer, Software Engineer
Related on AIssential
Editorial summary, takeaway, and curation by AIssential. Original article published by perplexity.ai via Google News.