ExamplescriptintermediateRunnableresearch-brief
File Search
Runnable example (intermediate) for script using openai, requests.
Key Facts
- Level
- intermediate
- Runtime
- Python • OpenAI API
- Pattern
- Context-backed research with explicit evidence
- Interaction
- Live sandbox • Script
- Updated
- 14 March 2026
Navigate this example
Library
Browse examplesReopen the wider library to compare adjacent patterns and linked learning paths.Interaction
Run sandbox nowTry the interaction directly in this example’s guided sandbox surface.Source
Open full sourceRead the real implementation, highlighted checkpoints, and runtime requirements.MCP
Call via MCPUse the same resource inside agents, deterministic exports, and MCP setup flows.
Linked principles
07-file-search.py
python
import requests
from io import BytesIO
from openai import OpenAI
import textwrap
client = OpenAI()
"""
https://platform.openai.com/storage/files/
"""
# --------------------------------------------------------------
# Upload a file
# --------------------------------------------------------------
def create_file(client, file_path):
if file_path.startswith("http://") or file_path.startswith("https://"):
# Download the file content from the URL
response = requests.get(file_path)
file_content = BytesIO(response.content)
file_name = file_path.split("/")[-1]
file_tuple = (file_name, file_content)
result = client.files.create(file=file_tuple, purpose="assistants")
else:
# Handle local file path
with open(file_path, "rb") as file_content:
result = client.files.create(file=file_content, purpose="assistants")
print(result.id)
return result.id
# Replace with your own file path or URL
file_id = create_file(client, "https://cdn.openai.com/API/docs/deep_research_blog.pdf")
# --------------------------------------------------------------
# Create a vector store
# --------------------------------------------------------------
"""
https://platform.openai.com/storage/vector_stores
Please be aware of costs!
"""
vector_store = client.vector_stores.create(name="knowledge_base")
print(vector_store.id)
# --------------------------------------------------------------
# Add a file to the vector store
# --------------------------------------------------------------
result = client.vector_stores.files.create(
vector_store_id=vector_store.id, file_id=file_id
)
print(result)
# --------------------------------------------------------------
# Check status
# --------------------------------------------------------------
result = client.vector_stores.files.list(vector_store_id=vector_store.id)
print(result)
# --------------------------------------------------------------
# Use file search
# --------------------------------------------------------------
"""
At the moment, you can search in only one vector store at a time,
so you can include only one vector store ID when calling the file search tool.
"""
response = client.responses.create(
model="gpt-4o",
input="What is deep research by OpenAI?",
tools=[{"type": "file_search", "vector_store_ids": [vector_store.id]}],
)
print(response)
print(textwrap.fill(response.output_text, width=80))
# --------------------------------------------------------------
# Limit results
# --------------------------------------------------------------
response = client.responses.create(
model="gpt-4o",
input="What is deep research by OpenAI?",
tools=[
{
"type": "file_search",
"vector_store_ids": [vector_store.id],
"max_num_results": 2,
}
],
include=["output[*].file_search_call.search_results"],
)
print(response.model_dump_json(indent=2))
# --------------------------------------------------------------
# Similarity search
# ----------------------§---------------------------------------
results = client.vector_stores.search(
vector_store_id=vector_store.id,
query="What is deep research by OpenAI?",
)
print(results.model_dump_json(indent=2))
Related principles
- P4trustApply progressive disclosure to system agencyProvide the minimum information necessary by default, while enabling users to inspect additional detail when confidence, understanding, or intervention is required.Open principle →
- P6visibilityExpose meaningful operational state, not internal complexityPresent the state of the system in language and structures that are relevant to the user, rather than exposing low-level internals that do not support action or understanding.Open principle →
- P7trustEstablish trust through inspectabilityUsers should be able to examine how a result was produced when confidence, accountability, or decision quality is important.Open principle →
- P9orchestrationRepresent delegated work as a system, not merely as a conversationWhere work involves multiple steps, agents, dependencies, or concurrent activities, it should be represented as a structured system rather than solely as a message stream.Open principle →