rules

A shared tag with AI prompts and code snippets

From workspace: Perplexity

Team: Main

Total snippets: 30

Perplexity

rules

30 snippets

Structured Outputs for Reasoning Models

When using structured outputs with reasoning models like sonar-reasoning-pro, the response will include a <think> section containing reasoning tokens, immediately followed by the structured output. The response_format parameter does not remove these reasoning tokens from the output, so the final response will need to be parsed manually. Sample Response:

<think> I need to provide information about France in a structured JSON format with specific fields: country, capital, population, official_language. For France: - Country: France - Capital: Paris - Population: About 67 million (as of 2023) -...

Generating responses using a regex

###Supported Regex - Characters: \d, \w, \s , . - Character classes: [0-9A-Fa-f] , [^x] - Quantifiers: *, ? , +, {3}, {2,4} , {3,} - Alternation: | - Group: ( ... ) - Non-capturing group: (?: ... ) - Positive lookahead: (?= ... ) - Negative...

Unsupported JSON Schemas

Recursive JSON schema is not supported. As a result of that, unconstrained objects are not supported either. Here’s a few example of unsupported schemas:

# UNSUPPORTED! from typing import Any class UnconstrainedDict(BaseModel): unconstrained: dict[str, Any] class RecursiveJson(BaseModel): value: str child: list["RecursiveJson"]

Use a regex to output the format

Request

import requests url = "https://api.perplexity.ai/chat/completions" headers = {"Authorization": "Bearer YOUR_API_KEY"} payload = { "model": "sonar", "messages": [ {"role": "system", "content": "Be precise and concise."}, ...

Get a response in JSON format

Request

import requests from pydantic import BaseModel class AnswerFormat(BaseModel): first_name: str last_name: str year_of_birth: int num_seasons_in_nba: int url = "https://api.perplexity.ai/chat/completions" headers =...

Overview

We currently support two types of structured outputs: JSON Schema and Regex. LLM responses will work to match the specified format, except for the following cases: The output exceeds max_tokens Enabling the structured outputs can be done by...

Best Practices

- Domain Filtering • Use simple domain names like example.com or -gettyimages.com • Do not include http://, https://, or subdomains - Format Filtering • File extensions must be lowercase: [“jpg”], not [“JPG”] • Omit dot prefix: use gif, not...

Combine Domain and Format Filters

You can combine both filters for precise control.

import requests url = "https://api.perplexity.ai/chat/completions" headers = {"Authorization": "Bearer YOUR_API_KEY"} payload = { "model": "sonar", "return_images": True, "image_domain_filter": ["-gettyimages.com"], "image_format_filter":...

Only Return GIFs

This request restricts results to GIF images.

import requests url = "https://api.perplexity.ai/chat/completions" headers = {"Authorization": "Bearer YOUR_API_KEY"} payload = { "model": "sonar", "return_images": True, "image_format_filter": ["gif"], "messages": [ {"role": "user",...

Exclude Specific Image Domains

This request filters out images from Getty Images.

import requests url = "https://api.perplexity.ai/chat/completions" headers = {"Authorization": "Bearer YOUR_API_KEY"} payload = { "model": "sonar", "return_images": True, "image_domain_filter": ["-gettyimages.com"], "messages": [ ...

Overview

The image_domain_filter and image_format_filter parameters give you fine-grained control over image results returned by the Sonar models. Use these filters to: Exclude specific image providers (e.g., Getty Images) Restrict to a specific file format (e.g., .gif) To use these filters, your request must include "return_images": true. • Prefix domains with - to exclude them • Use lowercase file extensions like gif, jpg, or png ⸻

{ "image_domain_filter": ["-gettyimages.com"], "image_format_filter": ["gif"] }

Image Filters Guide

Control image results with domain and format filters using the Perplexity API.

.

Performance Considerations

- Adding domain filters may slightly increase response time as the search engine needs to apply additional filtering. - Overly restrictive domain filters might result in fewer search results, potentially affecting the quality of the response.

Filter Optimization

- Be specific: Use domains that are most relevant to your query to get the best results. - Combine approaches: You can mix inclusion and exclusion in the same request (e.g., ["wikipedia.org", "-pinterest.com"]). - Limit filter size: Although you...

Domain Specification

- Use simple domain names: Specify domains in their simplest form (e.g., example.com) without protocol prefixes (http://, https://) or subdomain specifiers (www.). - Main domains only: Using the main domain (e.g., nytimes.com) will filter all...

Denylist Specific Domains

This example shows how to exclude specific domains from search results by prefixing the domain name with a minus sign (-). Best Practice: Use simple domain names with a minus prefix (e.g., -pinterest.com) to exclude domains from search results.

import requests url = "https://api.perplexity.ai/chat/completions" headers = {"Authorization": "Bearer YOUR_API_KEY"} payload = { "model": "sonar-deep-research", "messages": [ {"role": "system", "content": "You are a helpful...

Allowlist Specific Domains

This example shows how to limit search results to only include content from specific domains. Best Practice: Use simple domain names (e.g., wikipedia.org) without additional elements like https:// or www. prefixes.

import requests url = "https://api.perplexity.ai/chat/completions" headers = {"Authorization": "Bearer YOUR_API_KEY"} payload = { "model": "sonar-reasoning-pro", "messages": [ {"role": "system", "content": "You are a helpful...

Overview

The search_domain_filter parameter allows you to control which websites are included in or excluded from the search results used by the Sonar models. This feature is particularly useful when you want to. Each entry in the list should be a simple domain name. To exclude a domain, prefix it with a minus sign (-). Restrict search results to trusted sources Filter out specific domains from search results Focus research on particular websites Enabling domain filtering can be done by adding a search_domain_filter field in the request:

"search_domain_filter": [ "<domain1>", "<domain2>", ... ]

Tips for Different Query Types

.

Parameter Optimization

Adjust model parameters based on your specific needs: Search Domain Filter: Limit results to trusted sources for research-heavy queries. Search Context Size: Use “high” for comprehensive research questions and “low” for simple factual queries. Example configuration for technical documentation:

{ "search_domain_filter": ["wikipedia.org", "docs.python.org"], "web_search_options": { "search_context_size": "medium" } }

Assuming Search Intent

Don’t assume the model will search for what you intended without specific direction. Be explicit about exactly what information you need. ❌ Avoid: “Tell me about the latest developments.” ✅ Instead: “What are the latest developments in offshore...

Complex Multi-Part Requests

Complex prompts with multiple unrelated questions can confuse the search component. Focus on one topic per query. ❌ Avoid: “Explain quantum computing, and also tell me about regenerative agriculture, and provide stock market predictions.” ✅...

Traditional LLM Techniques

Prompting strategies designed for traditional LLMs often don’t work well with web search models. Adapt your approach accordingly. ❌ Avoid: “Act as an expert chef and give me a recipe for sourdough bread. Start by explaining the history of...

Overly Generic Questions

Generic prompts lead to scattered web search results and unfocused responses. Always narrow your scope. ❌ Avoid: “What’s happening in AI?” ✅ Instead: “What are the three most significant commercial applications of generative AI in healthcare in...

Provide Relevant Context

Include critical context to guide the web search toward the most relevant content, but keep prompts concise and focused. ✅ Good Example: “Explain the impact of the 2023 EU digital markets regulations on app store competition for small...

Think Like a Web Search User

Craft prompts with search-friendly terms that would appear on relevant web pages. Consider how experts in the field would describe the topic online. ✅ Good Example: “Compare the energy efficiency ratings of heat pumps vs. traditional HVAC systems...

Avoid Few-Shot Prompting

While few-shot prompting works well for traditional LLMs, it confuses web search models by triggering searches for your examples rather than your actual query. ✅ Good Example: “Summarize the current research on mRNA vaccine technology” ❌ Poor...

Be Specific and Contextual

Unlike traditional LLMs, our web search models require specificity to retrieve relevant search results. Adding just 2-3 extra words of context can dramatically improve performance. ✅ Good Example: “Explain recent advances in climate prediction...

User Prompt

You should use the user prompt to pass in the actual query for which you need an answer for. The user prompt will be used to kick off a real-time web search to make sure the answer has the latest and the most relevant information needed.

What are the best sushi restaurants in the world currently?

System Prompt

You can use the system prompt to provide instructions related to style, tone, and language of the response.

You are a helpful AI assistant. Rules: 1. Provide only the final answer. It is important that you do not include any explanation on the steps below. 2. Do not show the intermediate steps information. Steps: 1. Decide if the answer should be a...

Perplexity - rules - AI Prompts & Code Snippets | Snippets AI