Skip to main content

File search tool for agents

Use the file search tool to enable Microsoft Foundry agents to search through your documents and retrieve relevant information. File search augments agents with knowledge from outside their model, such as proprietary product information or user-provided documents. In this article, you learn how to:
  • Upload files and create a vector store
  • Configure an agent with file search enabled
  • Query your documents through the agent
By using the standard agent setup, the improved file search tool ensures your files remain in your own storage. Your Azure AI Search resource ingests the files, so you maintain complete control over your data.
File search has additional charges beyond the token-based fees for model usage.

Usage support

Microsoft Foundry supportPython SDKC# SDKJavaScript SDKJava SDKREST APIBasic agent setupStandard agent setup
✔️✔️✔️✔️-✔️✔️✔️

Prerequisites

  • A basic or standard agent environment
  • The latest prerelease SDK package:
    • Python: pip install azure-ai-projects azure-identity python-dotenv --pre
    • C#: dotnet add package Azure.AI.Projects.OpenAI --prerelease
    • TypeScript: npm install @azure/ai-projects @azure/identity dotenv
  • Storage Blob Data Contributor role on your project’s storage account (required for uploading files to your project’s storage)
  • Azure AI Owner role on your Foundry resource (required for creating agent resources)
  • Environment variables configured: FOUNDRY_PROJECT_ENDPOINT, MODEL_DEPLOYMENT_NAME

Create an agent with the file search tool

The following code sample shows how to create an agent with the file search tool enabled. You need to upload files and create a vector store before running this code. See the sections below for details.
import os
from pathlib import Path

from dotenv import load_dotenv

from azure.ai.projects import AIProjectClient
from azure.ai.projects.models import FileSearchTool, PromptAgentDefinition
from azure.identity import DefaultAzureCredential

load_dotenv()

# Load the file to be indexed for search.
asset_file_path = (Path(__file__).parent / "../assets/product_info.md").resolve()

with (
  DefaultAzureCredential() as credential,
  AIProjectClient(
    endpoint=os.environ["FOUNDRY_PROJECT_ENDPOINT"],
    credential=credential,
  ) as project_client,
  project_client.get_openai_client() as openai_client,
):
  print("Creating vector store...")
  vector_store = openai_client.vector_stores.create(name="ProductInfoStore")
  print(f"Vector store created (id: {vector_store.id})")

  print("Uploading file to vector store...")
  with asset_file_path.open("rb") as file_handle:
    vector_store_file = openai_client.vector_stores.files.upload_and_poll(
      vector_store_id=vector_store.id,
      file=file_handle,
    )
  print(f"File uploaded to vector store (id: {vector_store_file.id})")

  print("Creating agent with the file search tool...")
  agent = project_client.agents.create_version(
    agent_name="MyAgent",
    definition=PromptAgentDefinition(
      model=os.environ["MODEL_DEPLOYMENT_NAME"],
      instructions=(
        "You are a helpful agent that can search through product information. "
        "Use file search to answer questions from the uploaded files."
      ),
      tools=[FileSearchTool(vector_store_ids=[vector_store.id])],
    ),
    description="File search agent for product information queries.",
  )
  print(
    "Agent created "
    f"(id: {agent.id}, name: {agent.name}, version: {agent.version})"
  )

  print("Creating conversation...")
  conversation = openai_client.conversations.create()
  print(f"Created conversation (id: {conversation.id})")

  print("Creating response...")
  response = openai_client.responses.create(
    conversation=conversation.id,
    input="Tell me about Contoso products",
    extra_body={"agent": {"name": agent.name, "type": "agent_reference"}},
  )
  print(response.output_text)

  print("Cleaning up...")
  project_client.agents.delete_version(
    agent_name=agent.name,
    agent_version=agent.version,
  )
  openai_client.vector_stores.delete(vector_store.id)

Expected output

The following output comes from the preceding code sample:
Creating vector store...
Vector store created (id: vs_abc123)
Uploading file to vector store...
File uploaded to vector store (id: file-xyz789)
Creating agent with the file search tool...
Agent created (id: agent_001, name: MyAgent, version: 1)
Creating conversation...
Created conversation (id: conv_456)
Creating response...
[Response text grounded in your uploaded document content]
Cleaning up...

References

Upload files and add them to a vector store

To access your files, the file search tool uses the vector store object. Upload your files and create a vector store. Then poll the store’s status until all files are out of the in_progress state to ensure that all content is fully processed. The SDK provides helpers for uploading and polling.

Upload a file

curl --request POST \
  --url $FOUNDRY_PROJECT_ENDPOINT/openai/files?api-version=$API_VERSION \
  -H "Authorization: Bearer $AGENT_TOKEN" \
  -F purpose="assistants" \
  -F file="@c:\\path_to_file\\sample_file_for_upload.txt"

Create a vector store

curl --request POST \
  --url $FOUNDRY_PROJECT_ENDPOINT/openai/vector_stores?api-version=$API_VERSION \
  -H "Authorization: Bearer $AGENT_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "name": "my_vector_store",
    "file_ids": ["{{filesUpload.id}}"]
  }'
curl --request POST \
  --url $FOUNDRY_PROJECT_ENDPOINT/agents/$AGENTVERSION_NAME/versions?api-version=$API_VERSION \
  -H "Authorization: Bearer $AGENT_TOKEN" \
  -H 'Content-Type: application/json' \
  -d '{
  "description": "Test agent version description",
  "definition": {
    "kind": "prompt",
    "model": "{{model}}",
    "tools": [
      {
        "type": "file_search",
        "vector_store_ids": ["{{vectorStore.id}}"],
        "max_num_results": 20
      }
    ],
    "instructions": "You are a customer support chatbot. Use file search results from the vector store to answer questions based on the uploaded files."
  }
}'
curl --request POST \
  --url $FOUNDRY_PROJECT_ENDPOINT/openai/responses?api-version=$API_VERSION \
  -H "Authorization: Bearer $AGENT_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
  "agent": {
    "type": "agent_reference",
    "name": "{{agentVersion.name}}",
    "version": "{{agentVersion.version}}"
  },
  "metadata": {
    "test_response": "file_search_enabled",
    "vector_store_id": "{{vectorStore.id}}"
  },
  "input": [{
    "type": "message",
    "role": "user",
    "content": [
      {
        "type": "input_text",
        "text": "Can you search the uploaded file and tell me about Azure TV instructions?"
      }
    ]
  }],
  "stream": true
}'
The response returns streaming output containing the agent’s answer based on information retrieved from the vector store. The agent searches through your uploaded file to answer the query about Azure TV instructions.

Clean up

Delete the agent version.
curl --request DELETE \
  --url $FOUNDRY_PROJECT_ENDPOINT/agents/$AGENTVERSION_NAME/versions/$AGENTVERSION_VERSION?api-version=$API_VERSION \
  -H "Authorization: Bearer $AGENT_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{}'
Delete the vector store.
curl --request DELETE \
  --url $FOUNDRY_PROJECT_ENDPOINT/openai/vector_stores/$VECTORSTORE_ID?api-version=$API_VERSION \
  -H "Authorization: Bearer $AGENT_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{}'
Delete the file.
curl --request DELETE \
  --url $FOUNDRY_PROJECT_ENDPOINT/openai/files/$FILE_ID?api-version=$API_VERSION \
  -H "Authorization: Bearer $AGENT_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{}'

References

Verify file search results

After running a code sample in this article, verify that file search is working:
  • Confirm that the vector store and file are created.
    • In the Python and TypeScript samples, the upload-and-poll helpers complete only after ingestion finishes.
  • Ask a question that you can answer only from your uploaded content.
  • Confirm that the response is grounded in your documents.

File sources

  • Upload local files (Basic and Standard agent setup)
  • Azure Blob Storage (Standard setup only)

File search behavior by agent setup type

Basic agent setup

The file search tool has the same functionality as Azure OpenAI Responses API. The tool uses Microsoft managed search and storage resources.
  • You store uploaded files in Microsoft managed storage.
  • You create a vector store by using a Microsoft managed search resource.

Standard agent setup

The file search tool uses the Azure AI Search and Azure Blob Storage resources you connect to during agent setup.
  • You store uploaded files in your connected Azure Blob Storage account.
  • You create vector stores by using your connected Azure AI Search resource.
For both agent setups, the service handles the entire ingestion process, which includes:
  • Automatically parsing and chunking documents.
  • Generating and storing embeddings.
  • Utilizing both vector and keyword searches to retrieve relevant content for user queries.
The code is identical for both setups. The only variation is where your files and vector stores are stored. Choose file search when you need to:
  • Search through documents you upload directly (PDFs, Word docs, code files)
  • Enable agents to answer questions from proprietary or confidential content
  • Process files up to 512 MB without managing external search infrastructure
Consider alternatives for these scenarios:
ScenarioRecommended tool
Search existing Azure AI Search indexesAzure AI Search tool
Search the public web for current informationWeb search tool
Combine multiple data sources in one queryUse multiple tools together

How file search works

The file search tool uses retrieval best practices to extract relevant data from your files and improve model responses.

Query processing

When you send a query, file search:
  1. Rewrites your query to optimize it for search.
  2. Breaks down complex queries into parallel searches.
  3. Runs hybrid search combining keyword and semantic matching across vector stores.
  4. Reranks results to select the most relevant content for the response.

Default chunking settings

SettingDefault value
Chunk size800 tokens
Chunk overlap400 tokens
Embedding modeltext-embedding-3-large (256 dimensions)
Max chunks in context20

Vector stores

Vector store objects give the file search tool the ability to search your files. When you add a file to a vector store, the process automatically parses, chunks, embeds, and stores the file in a vector database that supports both keyword and semantic search. Each vector store can hold up to 10,000 files. You can attach vector stores to both agents and conversations. Currently, you can attach at most one vector store to an agent and at most one vector store to a conversation. For background concepts and lifecycle guidance (readiness, deletion behavior, and expiration policies), see Vector stores for file search. Remove files from a vector store by:
  • Deleting the vector store file object.
  • Deleting the underlying file object. This action removes the file from all vector_store and code_interpreter configurations across all agents and conversations in your organization.
The maximum file size is 512 MB. Each file should contain no more than 5,000,000 tokens (computed automatically when you attach a file).

Ensuring vector store readiness before creating runs

Ensure the system fully processes all files in a vector store before you create a run. This ensures all data in your vector store is searchable. Check for vector store readiness by using the polling helpers in the SDKs, or by manually polling the vector store object to ensure the status is completed. As a fallback, the run object includes a 60-second maximum wait when the conversation’s vector store contains files that are still processing. This wait ensures that any files your users upload in a conversation are fully searchable before the run proceeds. This fallback wait doesn’t apply to the agent’s vector store.

Conversation vector stores have default expiration policies

Vector stores that you create by using conversation helpers (like tool_resources.file_search.vector_stores in conversations or message.attachments in Messages) have a default expiration policy of seven days after they were last active (defined as the last time the vector store was part of a run). When a vector store expires, the runs on that conversation fail. To fix this problem, recreate a new vector store with the same files and reattach it to the conversation.

Supported file types

For text MIME types, the encoding must be UTF-8, UTF-16, or ASCII.
File formatMIME Type
.ctext/x-c
.cstext/x-csharp
.cpptext/x-c++
.docapplication/msword
.docxapplication/vnd.openxmlformats-officedocument.wordprocessingml.document
.htmltext/html
.javatext/x-java
.jsonapplication/json
.mdtext/markdown
.pdfapplication/pdf
.phptext/x-php
.pptxapplication/vnd.openxmlformats-officedocument.presentationml.presentation
.pytext/x-python
.pytext/x-script.python
.rbtext/x-ruby
.textext/x-tex
.txttext/plain
.csstext/css
.jstext/javascript
.shapplication/x-sh
.tsapplication/typescript

Limitations

Keep these limits in mind when you plan your file search integration:
  • File search supports specific file formats and encodings. See Supported file types.
  • Each vector store can hold up to 10,000 files.
  • You can attach at most one vector store to an agent and at most one vector store to a conversation.
  • Features and availability vary by region. See Azure AI Foundry region support.

Troubleshooting

IssueLikely causeResolution
401 UnauthorizedThe access token is missing, expired, or scoped incorrectly.Get a fresh token and retry the request. For REST calls, confirm you set AGENT_TOKEN correctly.
403 ForbiddenThe signed-in identity doesn’t have the required roles.Confirm the roles in Prerequisites and retry after role assignment finishes propagating.
404 Not FoundThe project endpoint or resource identifiers are incorrect.Confirm FOUNDRY_PROJECT_ENDPOINT and IDs such as agent name, version, vector store ID, and file ID.
Responses ignore your filesThe agent isn’t configured with file_search, or the vector store isn’t attached.Confirm the agent definition includes file_search and the vector_store_ids list contains your vector store ID.
File upload times outLarge file or slow network connection.Use upload_and_poll to handle large files. Consider chunking very large documents.
Vector store creation failsQuota exceeded or invalid file format.Check vector store limits (10,000 files per store). Verify file format is supported.
Search returns irrelevant resultsFile content not properly indexed or query too broad.Wait for indexing to complete (check vector_store.status). Use more specific queries.
No citations in responseModel didn’t use file search or content not found.Use tool_choice="required" to force file search. Verify the file content matches your query topic.