Sub topic 10
Preparing documents with vector stores
File search can only retrieve what you have indexed first. This chapter covers that preparation step. It assumes you can call the Responses API with an API key and read JSON responses. It also assumes you know the basic idea of tool calling.
What a vector store is
A vector store is a provider-hosted index of your documents. When you upload a file, the provider splits it into chunks and converts each chunk into an embedding, which is a list of numbers that represents its meaning. At query time, the provider embeds the question and returns the chunks whose embeddings are closest to it.
You pay for this work once, at upload time. Later requests only search the finished index.
Creating a store and adding a file
Create the store, then attach files to it.
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)upload_and_poll uploads the file, attaches it to the store, and waits until processing finishes. Indexing runs asynchronously. If you use a call that does not poll, a search made too early may return nothing, because the file is not ready yet.
Checking the status
Each file in a store has a status. Check it before you rely on the store.
files = client.vector_stores.files.list(vector_store_id=vector_store.id)
for item in files.data:
print(item.id, item.status)The status is in_progress, completed, failed, or cancelled. Only completed files are searchable.
Common errors
- Searching before indexing finishes. Use a polling call or check the status.
- Unsupported or unreadable files. Scanned PDFs without a text layer give the indexer little to work with. Use text-based formats where you can.
- Losing the store ID. The request needs
vector_store_ids. Save the ID when you create the store, and don't create a new store on every run. - Stale content. The store does not track changes to your local file. When a document changes, upload the new version and remove the old file.
- Mixed audiences. Everything in a store is searchable by any request that references it. Keep documents with different access levels in separate stores.
Using the store
Once files show completed, point the tool at the store. The next chapters cover file search itself and how to tune it.
response = client.responses.create(
model="gpt-4.1",
tools=[{"type": "file_search", "vector_store_ids": [vector_store.id]}],
input="How many vacation days do new employees get?",
)
print(response.output_text)Exercise
You upload a PDF and immediately run a file search request, but the answer ignores the document. Name the most likely cause and two ways to prevent it.
Show answer
The file was probably still being indexed, so it was not searchable yet. Use uploadandpoll so the call waits for processing, or list the store's files and confirm the status is completed before you send the request.
Exercise
Your HR documents and public product documents are in one vector store. Why is that a problem, and what should you do?
Show answer
Any request that references the store can retrieve chunks from every file in it. Put the HR documents in their own store and reference only the store that matches the user's access level.