Use Desearch AI Search with OpenAI
Use Desearch AI Search to fetch fresh web results and X posts, then pass them to an OpenAI chat completion as context. This page uses the async desearch-py client and the openai Python package.
Get Started
1. Installation
2. Set Up Clients
3. Search with Desearch
ai_search is async. It calls POST /desearch/ai/search and returns search (web results) and tweets (X posts) for the tools you pass.
4. Pass Results to OpenAI
Turn the results into a context block and send it with the user question:
ai_search can return a typed object or a plain dict, so to_context handles both.
5. Complete Code Example
Copy this into a script or notebook:
6. Handling Multiple Questions
The snippets below use await, so run them inside an async function like fetch_sources above.
Information Source Selection
Pick which sources to search with the tools parameter of ai_search:
Supported tools: web, twitter. Other values are unsupported and may be rejected (for example, youtube returns 422).
Time Relevance
Filter results by date with the date_filter parameter. On AI Search, date filtering applies to X results.
Response Format
Control how results are returned with the result_type parameter:
With LINKS_WITH_FINAL_SUMMARY, the response also includes a written summary in completion.