Perplexity Free code examples in Python and JavaScript
Chat models with live web search and citations built in.
WHAT THIS PAGE COVERS
Reading citations back out of the response, which is why you would use it.
openai/gpt-5.6-sol
perplexityai
Model ids change. If a sample returns a 404 on the model, that is the first thing to check against the official docs.
A first-party SDK, not the OpenAI client. If you find a tutorial setting base_url to api.perplexity.ai and calling chat.completions, it predates the Agent API.
pip install perplexityai
export PERPLEXITY_API_KEY="pplx-..." # macOS / Linux
setx PERPLEXITY_API_KEY "pplx-..." # Windows, then open a new shell
responses.create with input, not chat.completions.create with messages. output_text is the convenience accessor that flattens the response for you.
from perplexity import Perplexity
client = Perplexity() # reads PERPLEXITY_API_KEY
response = client.responses.create(
model="openai/gpt-5.6-sol",
input="What changed in the EU AI Act in 2026?",
)
print(response.output_text)
Any model can write a confident paragraph. The value here is the sources behind it, and they are in the output array, not in output_text. A page that ignores them is using an expensive way to get an uncited answer.
response = client.responses.create(
model="openai/gpt-5.6-sol",
input="What changed in the EU AI Act in 2026?",
)
# The response is a list of typed items, not one blob. Search results
# and the assistant message are separate entries.
for item in response.output:
if item.type == "search_results":
print("Searched:", item.queries)
for rank, result in enumerate(item.results, start=1):
print(f" [web:{rank}] {result.title}")
print(f" {result.url}")
elif item.type == "message":
# Citations appear inline as [web:1], [web:2] — they index the
# results above, so show both or neither.
print(item.content)
Four things that will catch you out coming from any other provider on this page.
# 1. IT IS NOT CHAT COMPLETIONS ANY MORE. Anything pointing an OpenAI
# client at api.perplexity.ai/chat/completions is the deprecated
# shape. Use the SDK.
# 2. input IS A STRING OR A LIST — not the messages array you are used
# to. Conversation state is built differently; check the API
# reference rather than assuming the OpenAI shape.
# 3. MODEL IDS ARE NAMESPACED and are not Perplexity's own models —
# "openai/gpt-5.6-sol" routes an OpenAI model through Perplexity's
# search. The prefix is part of the id.
MODEL = "openai/gpt-5.6-sol"
# 4. EVERY CALL DOES A WEB SEARCH, so latency and cost are dominated by
# retrieval, not generation. It is the wrong tool for a request that
# does not need the live web.
A first-party SDK. Server-side only — this key buys web searches, so a leaked one is billable.
npm install @perplexity-ai/perplexity_ai
export PERPLEXITY_API_KEY="pplx-..."
responses.create with input, not chat.completions.create with messages.
import Perplexity from "@perplexity-ai/perplexity_ai";
const client = new Perplexity(); // reads PERPLEXITY_API_KEY
const response = await client.responses.create({
model: "openai/gpt-5.6-sol",
input: "What changed in the EU AI Act in 2026?",
});
console.log(response.output_text);
The sources are in the output array, not in output_text. Rendering the answer without them wastes what you paid for.
const response = await client.responses.create({
model: "openai/gpt-5.6-sol",
input: "What changed in the EU AI Act in 2026?",
});
for (const item of response.output) {
if (item.type === "search_results") {
console.log("Searched:", item.queries);
item.results.forEach((result, i) => {
console.log(` [web:${i + 1}] ${result.title}`);
console.log(` ${result.url}`);
});
} else if (item.type === "message") {
// Inline citations read [web:1], [web:2] and index the list above.
console.log(item.content);
}
}
Four things that catch people coming from any other provider here.
// 1. NOT CHAT COMPLETIONS. An OpenAI client pointed at
// api.perplexity.ai/chat/completions is the deprecated shape.
// 2. input IS A STRING OR A LIST, not a messages array.
// 3. MODEL IDS ARE NAMESPACED and name the underlying model, not a
// Perplexity one: "openai/gpt-5.6-sol" is an OpenAI model routed
// through Perplexity's search.
const MODEL = "openai/gpt-5.6-sol";
// 4. EVERY CALL SEARCHES THE WEB. Latency and cost are dominated by
// retrieval — the wrong tool when the request does not need the
// live web.
QUESTIONS
Which languages are the Perplexity examples in?
Python and JavaScript. Every sample runs as pasted and reads its credentials from an environment variable — no key is ever written into the code.
Which package do I install for Perplexity?
The samples use perplexityai. The install step on each language tab gives the exact command.
Which Perplexity model do these examples use?
openai/gpt-5.6-sol, checked against the official documentation on 25 September 2026. Model ids change often; if a call returns a model-not-found error, check the current list in the official docs.
Where is the official Perplexity documentation?
https://docs.perplexity.ai/ — it is linked at the bottom of this page, and it is the authority whenever it disagrees with a sample here.
ALSO IN LANGUAGE MODELS
The authority
Official Perplexity documentation
Model names, pricing and rate limits change faster than any page here can. When this page and the official docs disagree, the official docs are right.
https://docs.perplexity.ai/