Perplexity
Search-grounded answers with citations as sources.
let model = PerplexityModel("sonar-pro")Key from PERPLEXITY_API_KEY; base URL https://api.perplexity.ai.
Features
- Every answer is search-grounded; citations and the richer
search_results(title + URL) surface asStreamPart.sourceand collect onresult.sources. - Structured output and image input ride the chat-completions wire on models that support them.
- No tool calling — Perplexity's API doesn't offer it, so the library
doesn't pretend otherwise. The
reasoningparameter is ignored the same way. - Image results and related questions arrive on
result.providerMetadata["perplexity"](images,related_questions) when you request them viaproviderOptions(return_images,return_related_questions).
let result = try await generateText(
model: PerplexityModel("sonar-pro"),
prompt: "Show me photos of the aurora.",
providerOptions: ["return_images": true, "return_related_questions": true]
)
let meta = result.providerMetadata?["perplexity"]
print(meta?["images"] ?? .null)
print(meta?["related_questions"] ?? .null)let result = try await generateText(
model: PerplexityModel("sonar-pro"),
prompt: "What changed in Swift 6.2?"
)
for source in result.sources {
print(source.title ?? source.url)
}Models
Three ids cover the lineup: sonar (fast grounded answers),
sonar-pro (deeper multi-hop search), and sonar-reasoning-pro
(search plus visible reasoning).