Allow models to search your files for relevant information before generating a response.
File search is a tool available in the Responses API.
It enables models to retrieve information in a knowledge base of previously uploaded files through semantic and keyword search.
By creating vector stores and uploading files to them, you can augment the modelsâ inherent knowledge by giving them access to these knowledge bases or vector_stores.
To learn more about how vector stores and semantic search work, refer to our
retrieval guide.
This is a hosted tool managed by OpenAI, meaning you donât have to implement code on your end to handle its execution.
When the model decides to use it, it will automatically call the tool, retrieve information from your files, and return an output.
How to use
Prior to using file search with the Responses API, you need to have set up a knowledge base in a vector store and uploaded files to it.
Follow these steps to create a vector store and upload a file to it. You can use this example file or upload your own.
Upload the file to the File API
Upload a file
Python
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34import fs from "fs";import OpenAI from "openai";const openai = new OpenAI();async function createFile(filePath) { let result; if (filePath.startsWith("http://") || filePath.startsWith("https://")) { // Download the file content from the URL const res = await fetch(filePath); const buffer = await res.arrayBuffer(); const urlParts = filePath.split("/"); const fileName = urlParts[urlParts.length - 1]; const file = new File([buffer], fileName); result = await openai.files.create({ file: file, purpose: "assistants", }); } else { // Handle local file path const fileContent = fs.createReadStream(filePath); result = await openai.files.create({ file: fileContent, purpose: "assistants", }); } return result.id;}// Replace with your own file path or URLconst fileId = await createFile( "https://cdn.openai.com/API/docs/deep_research_blog.pdf");console.log(fileId);
Once your knowledge base is set up, you can include the file_search tool in the list of tools available to the model, along with the list of vector stores in which to search.
File search tool
Python
1
2
3
4
5
6
7
8
9
10
11
12
13
14import OpenAI from "openai";const openai = new OpenAI();const response = await openai.responses.create({ model: "gpt-5.6", input: "What is deep research by OpenAI?", tools: [ { type: "file_search", vector_store_ids: ["<vector_store_id>"], }, ],});console.log(response);
1
2
3
4
5
6
7
8
9
10from openai import OpenAIclient = OpenAI()response = client.responses.create(model="gpt-5.6",input="What is deep research by OpenAI?",tools=[{"type": "file_search", "vector_store_ids": ["<vector_store_id>"]}],)print(response)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18using OpenAI.Responses;#pragma warning disable OPENAI001string key = Environment.GetEnvironmentVariable("OPENAI_API_KEY")!;string vectorStoreId = "<vector_store_id>";ResponsesClient client = new(key);CreateResponseOptions options = new() { Model = "gpt-5.6" };options.Tools.Add( ResponseTool.CreateFileSearchTool([vectorStoreId]));options.InputItems.Add( ResponseItem.CreateUserMessageItem("What is deep research by OpenAI?"));ResponseResult response = await client.CreateResponseAsync(options);Console.WriteLine(response.GetOutputText());
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16require "openai"openai = OpenAI::Client.newresponse = openai.responses.create( model: "gpt-5.6", input: "What is deep research by OpenAI?", tools: [ { type: "file_search", vector_store_ids: ["<vector_store_id>"] } ])puts(response)
When this tool is called by the model, you will receive a response with multiple outputs:
A file_search_call output item, which contains the id of the file search call.
A message output item, which contains the response from the model, along with the file citations.
File search response
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48{"output": [ {"type": "file_search_call","id": "fs_67c09ccea8c48191ade9367e3ba71515","status": "completed","queries": ["What is deep research?"],"search_results": null }, {"id": "msg_67c09cd3091c819185af2be5d13d87de","type": "message","role": "assistant","content": [ {"type": "output_text","text": "Deep research is a sophisticated capability that allows for extensive inquiry and synthesis of information across various domains. It is designed to conduct multi-step research tasks, gather data from multiple online sources, and provide comprehensive reports similar to what a research analyst would produce. This functionality is particularly useful in fields requiring detailed and accurate information...","annotations": [ {"type": "file_citation","index": 992,"file_id": "file-2dtbBZdjtDKS8eqWxqbgDi","filename": "deep_research_blog.pdf" }, {"type": "file_citation","index": 992,"file_id": "file-2dtbBZdjtDKS8eqWxqbgDi","filename": "deep_research_blog.pdf" }, {"type": "file_citation","index": 1176,"file_id": "file-2dtbBZdjtDKS8eqWxqbgDi","filename": "deep_research_blog.pdf" }, {"type": "file_citation","index": 1176,"file_id": "file-2dtbBZdjtDKS8eqWxqbgDi","filename": "deep_research_blog.pdf" } ] } ] } ]}
Retrieval customization
Limiting the number of results
Using the file search tool with the Responses API, you can customize the number of results you want to retrieve from the vector stores. This can help reduce both token usage and latency, but may come at the cost of reduced answer quality.
Limit the number of results
Python
1
2
3
4
5
6
7
8
9
10
11
12const response = await openai.responses.create({ model: "gpt-5.6", input: "What is deep research by OpenAI?", tools: [ { type: "file_search", vector_store_ids: ["<vector_store_id>"], max_num_results: 2, }, ],});console.log(response);
1
2
3
4
5
6
7
8
9
10
11
12response = client.responses.create(model="gpt-5.6",input="What is deep research by OpenAI?",tools=[ {"type": "file_search","vector_store_ids": ["<vector_store_id>"],"max_num_results": 2, } ],)print(response)