Use the Perplexity SDKs to access the Search API with type safety and async support.
This page focuses on the Search API. For shared setup (installation, authentication, configuration, error handling, performance, and type safety), see the SDK overview.
from perplexity import Perplexityclient = Perplexity()search = client.search.create( query="latest AI developments 2024", max_results=5)for result in search.results: print(f"{result.title}: {result.url}")
Example Output
Copy
Ask AI
The Top Artificial Intelligence Trends - IBM: https://www.ibm.com/think/insights/artificial-intelligence-trendsYear in review: Google's biggest AI advancements of 2024: https://blog.google/technology/ai/2024-ai-extraordinary-progress-advancement/AI Pulse: Top AI Trends from 2024 - A Look Back | Trend Micro (US): https://www.trendmicro.com/en_us/research/25/a/top-ai-trends-from-2024-review.htmlThe State of AI: Global survey - McKinsey: https://www.mckinsey.com/capabilities/quantumblack/our-insights/the-state-of-aiGenerative AI Developments & Trends in 2024: A Timeline: https://www.channelinsider.com/managed-services/generative-ai-developments-trends-year-in-review/
Run multiple related searches in a single request:
Copy
Ask AI
search = client.search.create( query=[ "latest AI developments 2024", "solar power innovations", "wind energy developments" ])# Results are combined and rankedfor i, result in enumerate(search.results): print(f"{i + 1}. {result.title}") print(f" URL: {result.url}") print(f" Date: {result.date}\n")
search = client.search.create( query="latest AI developments 2024", max_results=5 # Get only top 5 results)print(f"Found {len(search.results)} results")
The country parameter only supports ISO 3166-1 alpha-2 country codes (e.g., “US”, “GB”, “DE”).
Copy
Ask AI
search = client.search.create( query="local restaurants", country="US" # ISO country code)# Search in different countriesuk_search = client.search.create( query="tech events", country="GB" # United Kingdom)canada_search = client.search.create( query="hockey news", country="CA" # Canada)
Common ISO country codes: US (United States), GB (United Kingdom), CA (Canada), DE (Germany), FR (France), JP (Japan), AU (Australia). See the full list at ISO 3166-1 alpha-2.
from perplexity.types import SearchCreateParamsadvanced_search_params = SearchCreateParams( query="renewable energy research", max_results=15, return_images=True, return_snippets=True, country="US" # ISO country code)search = client.search.create(**advanced_search_params.dict())# Process results with metadatafor result in search.results: print(f"Title: {result.title}") print(f"URL: {result.url}") print(f"Date: {result.date}") print(f"Snippet: {result.snippet}") if result.images and len(result.images) > 0: print(f"Images: {len(result.images)} found") print("---")