Sub topic 12
Tuning Retrieval: Result Limits, Filters, and File Citations
This chapter assumes you have indexed documents in a vector store and called the file_search tool once. It covers three levers that change what the model sees and how you show sources: the result limit, attribute filters, and file citations.
Why tune retrieval
File search splits your documents into chunks and returns the chunks that best match the query. The model answers from those chunks. If the chunks are too few, the answer misses facts. If they are too many or off topic, you pay for extra input tokens and the model may latch onto the wrong passage.
Limit the number of results
The max_num_results field caps how many chunks come back. A lower value cuts tokens and latency. A higher value helps when answers are spread across many passages.
from openai import OpenAI
client = OpenAI()
response = client.responses.create(
model="gpt-4.1",
tools=[
{
"type": "file_search",
"vector_store_ids=": ["vs_example"],
}
],
input="How many vacation days do new employees get?",
)The block above is not the pattern to copy. Use the correct field names, as shown next.
from openai import OpenAI
client = OpenAI()
response = client.responses.create(
model="gpt-4.1",
tools=[
{
"type": "file_search",
"vector_store_ids": ["vs_example"],
"max_num_results": 3,
}
],
input="How many vacation days do new employees get?",
include=["file_search_call.results"],
)
print(response.output_text)The include option asks the provider to return the retrieved chunks inside the file_search_call item. Use it while tuning so you can see what the model saw.
Filter by attributes
Attach key-value attributes to each file when you add it to the vector store. Then restrict a search to matching files with a filters object.
filters = {
"type": "eq",
"key": "department",
"value": "hr",
}
response = client.responses.create(
model="gpt-4.1",
tools=[
{
"type": "file_search",
"vector_store_ids": ["vs_example"],
"max_num_results": 3,
"filters": filters,
}
],
input="How many vacation days do new employees get?",
)Filters run before ranking, so they remove unrelated files from the candidate pool. This improves precision and also keeps one team's documents out of another team's results. Comparison filters such as eq, gt, and in can be combined with and and or.
Read file citations
The answer text carries annotations. For file search, they have type file_citation and name the source file.
for item in response.output:
if item.type == "message":
for part in item.content:
for ann in getattr(part, "annotations", []) or []:
if ann.type == "file_citation":
print(ann.filename, ann.file_id)Show these filenames next to the answer so readers can check the source.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Answer misses a known fact | `max_num_results` too low | Raise it gradually and compare |
| Answer cites the wrong team's file | No filter applied | Add an attribute filter |
| No citations printed | Model did not use the tool, or you read the wrong item | Check for a `file_search_call` item |
| Filter returns nothing | Attribute key or value mismatch on the file | Check the attributes set at upload |
Exercise
A support bot answers from a vector store holding documents for several customers. A user from customer A sometimes sees answers drawn from customer B's files. Which retrieval setting fixes this, and why is lowering maxnumresults not enough?
Show answer
Add an attribute filter, for example eq on a customerid attribute set on each file at upload. Filters remove other customers' files before ranking. Lowering maxnum_results only returns fewer chunks, and the top few could still come from customer B.