How to use file search with Responses API
Quick answer
Use the
OpenAI SDK's responses.search method to perform file search by specifying the file ID and a query. This returns relevant document matches from the uploaded file. Authenticate with your API key and provide the file ID to search within.PREREQUISITES
Python 3.8+OpenAI API key (free tier works)pip install openai>=1.0
Setup
Install the official openai Python package (v1+) and set your API key as an environment variable.
- Install package:
pip install openai - Set environment variable:
export OPENAI_API_KEY='your_api_key'(Linux/macOS) orsetx OPENAI_API_KEY "your_api_key"(Windows)
pip install openai Step by step
This example demonstrates how to search an uploaded file using the responses.search endpoint. Replace file_id with your actual file ID obtained after uploading your documents.
import os
from openai import OpenAI
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
# Replace with your uploaded file ID
file_id = "file-xxxxxxxxxxxxxxxxxxxx"
query = "Find information about AI models"
response = client.responses.search(
file=file_id,
query=query,
max_rerank=10 # Optional: number of top matches to return
)
for match in response.data:
print(f"Score: {match.score:.4f}")
print(f"Text snippet: {match.text}\n") output
Score: 0.9876 Text snippet: GPT-4o is a powerful AI model designed for chat and code generation. Score: 0.8765 Text snippet: File search allows you to retrieve relevant document sections efficiently.
Common variations
You can customize your file search by:
- Adjusting
max_rerankto control how many top results are returned. - Using different
querystrings for varied search intents. - Performing asynchronous calls with
asynciofor high throughput. - Searching multiple files by calling
responses.searchseparately for each file.
import asyncio
import os
from openai import OpenAI
async def async_file_search(file_id: str, query: str):
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
response = await client.responses.search(
file=file_id,
query=query,
max_rerank=5
)
for match in response.data:
print(f"Score: {match.score:.4f}")
print(f"Text snippet: {match.text}\n")
if __name__ == "__main__":
file_id = "file-xxxxxxxxxxxxxxxxxxxx"
query = "Explain retrieval augmented generation"
asyncio.run(async_file_search(file_id, query)) output
Score: 0.9543 Text snippet: Retrieval augmented generation (RAG) combines search with generation to improve accuracy.
Troubleshooting
- File not found error: Ensure your
file_idis correct and the file is uploaded successfully. - Empty results: Try refining your
queryor increasemax_rerankto get more matches. - Authentication errors: Verify your
OPENAI_API_KEYenvironment variable is set and valid. - API version mismatch: Use the latest
openaiSDK (v1+) to avoid deprecated endpoints.
Key Takeaways
- Use
client.responses.searchwith your file ID and query to retrieve relevant document snippets. - Adjust
max_rerankto control the number of search results returned. - Always authenticate with your API key via
os.environand use the latestopenaiSDK. - For high throughput, use asynchronous calls with
asyncio. - Verify your file is uploaded and accessible before performing searches.