Sub topic 11
File search: retrieval over your own documents
This chapter assumes you can call the Responses API with an API key, read JSON response bodies, and know the basic idea of tool calling. Web search covers public pages. File search covers your private documents, such as a handbook, contracts, or support articles.
How file search works
File search is a built-in tool. The provider indexes your files in a vector store, then searches that index when the model decides it needs facts from your documents. A vector store splits each file into chunks and stores a numeric embedding for each chunk. At query time, the provider embeds the model's search query and returns the closest chunks. The model then writes its answer from those chunks.
Your code makes one request. The provider runs the search and the answer generation in a single response.
Step 1: Index your documents
Create a vector store, then upload a file into it. The upload_and_poll helper waits until indexing finishes. If you query before then, the search can miss content.
from openai import OpenAI
client = OpenAI()
vector_store = client.vector_stores.create(name="company-handbook")
with open("handbook.pdf", "rb") as f:
client.vector_stores.files.upload_and_poll(
vector_store_id=vector_store.id,
file=f,
)
print(vector_store.id)You index once and reuse the store across many requests. Save the ID.
Step 2: Query with the tool
Declare the tool with the store's ID. The max_num_results field caps how many chunks the search returns.
response = client.responses.create(
model="gpt-4.1",
tools=[
{
"type": "file_search",
"vector_store_ids": [vector_store.id],
"max_num_results": 3,
}
],
input="How many vacation days do new employees get?",
)
print(response.output_text)A lower cap means fewer tokens and faster answers. A higher cap raises the chance that the right passage is included, but it adds noise and cost.
Inspecting what happened
The response output is a list of items. A file_search_call item records that the provider searched. The answer message carries annotations that point back to the source files.
for item in response.output:
if item.type == "file_search_call":
print("search status:", item.status)
elif item.type == "message":
for part in item.content:
for ann in getattr(part, "annotations", []) or []:
if ann.type == "file_citation":
print("source file:", ann.filename)Check these items when an answer looks wrong. If no file_search_call appears, the model answered without searching. If a search ran but the answer is still wrong, the right chunk probably was not retrieved.
Common errors
- Querying before indexing completes. Use the polling helper, or check the file's status.
- Expecting a whole-file read. The model sees only the retrieved chunks, so questions that need a full document, such as a complete summary, may get partial answers.
- Result cap too low. A cap of 1 fails when the answer spans two sections.
- Vague documents. Scanned PDFs without extractable text produce empty chunks.
Exercise
A user asks a question that your handbook clearly answers, but the response has no filesearchcall item and the answer is a guess. Name two things you could change in the request to make the model search.
Show answer
Add instructions telling the model to search the handbook before answering, and set toolchoice to require the filesearch tool. Also confirm the vectorstoreids value is correct and that indexing has finished.