Table of Contents

Class FileSearchStoresClient

Namespace
GenerativeAI.Clients
Assembly
GenerativeAI.dll

A client for the Gemini API File Search stores and documents endpoints (managed retrieval-augmented generation).

public class FileSearchStoresClient : BaseClient
Inheritance
FileSearchStoresClient
Inherited Members
Extension Methods

Remarks

Typical flow: create a store with CreateFileSearchStoreAsync(string?, string?, CancellationToken), add files with UploadToFileSearchStoreAsync(string, string, UploadToFileSearchStoreConfig?, Action<double>?, CancellationToken) or ImportFileAsync(string, string, ImportFileConfig?, CancellationToken), wait for indexing with WaitForOperationAsync(FileSearchOperation, TimeSpan?, TimeSpan?, CancellationToken), then pass the store name to the FileSearch tool (for example via UseFileSearch(params string[])).

File Search is only available on the Gemini Developer API; every method throws NotSupportedException when the client is configured for Vertex AI.

Constructors

FileSearchStoresClient(IPlatformAdapter, HttpClient?, ILogger?)

Initializes a new instance of the FileSearchStoresClient class.

public FileSearchStoresClient(IPlatformAdapter platform, HttpClient? httpClient = null, ILogger? logger = null)

Parameters

platform IPlatformAdapter

The platform adapter used for URLs and authorization.

httpClient HttpClient

Optional HTTP client.

logger ILogger

Optional logger.

Methods

CreateFileSearchStoreAsync(FileSearchStore, CancellationToken)

Creates a File Search store from the given definition (only DisplayName and EmbeddingModel are sent).

public Task<FileSearchStore> CreateFileSearchStoreAsync(FileSearchStore store, CancellationToken cancellationToken = default)

Parameters

store FileSearchStore

The store definition.

cancellationToken CancellationToken

Token to cancel the request.

Returns

Task<FileSearchStore>

The created FileSearchStore.

See Also

CreateFileSearchStoreAsync(string?, string?, CancellationToken)

Creates an empty File Search store.

public Task<FileSearchStore> CreateFileSearchStoreAsync(string? displayName = null, string? embeddingModel = null, CancellationToken cancellationToken = default)

Parameters

displayName string

Optional human-readable display name (at most 512 characters).

embeddingModel string

Optional embedding model, e.g. gemini-embedding-001 or models/gemini-embedding-001. The service default is used when omitted.

cancellationToken CancellationToken

Token to cancel the request.

Returns

Task<FileSearchStore>

The created FileSearchStore.

See Also

DeleteDocumentAsync(string, bool?, CancellationToken)

Deletes a document from a File Search store.

public Task DeleteDocumentAsync(string documentName, bool? force = null, CancellationToken cancellationToken = default)

Parameters

documentName string

The full document name: fileSearchStores/{store}/documents/{document}.

force bool?

When true, also deletes the document's chunks. When false or unset, the request fails if the document has chunks.

cancellationToken CancellationToken

Token to cancel the request.

Returns

Task
See Also

DeleteFileSearchStoreAsync(string, bool?, CancellationToken)

Deletes a File Search store.

public Task DeleteFileSearchStoreAsync(string name, bool? force = null, CancellationToken cancellationToken = default)

Parameters

name string

The store name, either fileSearchStores/{id} or just {id}.

force bool?

When true, also deletes all documents in the store. When false or unset, the request fails if the store contains documents.

cancellationToken CancellationToken

Token to cancel the request.

Returns

Task
See Also

DownloadMediaAsync(string, CancellationToken)

Downloads media referenced by File Search grounding metadata.

public Task<byte[]> DownloadMediaAsync(string mediaId, CancellationToken cancellationToken = default)

Parameters

mediaId string

The media name, in the form fileSearchStores/{store}/media/{blob}.

cancellationToken CancellationToken

Token to cancel the request.

Returns

Task<byte[]>

The raw media bytes.

GetDocumentAsync(string, CancellationToken)

Gets a document from a File Search store.

public Task<FileSearchDocument> GetDocumentAsync(string documentName, CancellationToken cancellationToken = default)

Parameters

documentName string

The full document name: fileSearchStores/{store}/documents/{document}.

cancellationToken CancellationToken

Token to cancel the request.

Returns

Task<FileSearchDocument>

The FileSearchDocument.

See Also

GetFileSearchStoreAsync(string, CancellationToken)

Gets a File Search store.

public Task<FileSearchStore> GetFileSearchStoreAsync(string name, CancellationToken cancellationToken = default)

Parameters

name string

The store name, either fileSearchStores/{id} or just {id}.

cancellationToken CancellationToken

Token to cancel the request.

Returns

Task<FileSearchStore>

The FileSearchStore.

See Also

GetOperationAsync(string, CancellationToken)

Gets the latest state of an upload or import operation.

public Task<FileSearchOperation> GetOperationAsync(string operationName, CancellationToken cancellationToken = default)

Parameters

operationName string

The operation name, e.g. fileSearchStores/{store}/operations/{id} or fileSearchStores/{store}/upload/operations/{id}.

cancellationToken CancellationToken

Token to cancel the request.

Returns

Task<FileSearchOperation>

The current FileSearchOperation.

ImportFileAsync(string, string, ImportFileConfig?, CancellationToken)

Imports a file previously uploaded with the Files API (see FileClient) into a File Search store.

public Task<FileSearchOperation> ImportFileAsync(string fileSearchStoreName, string fileName, ImportFileConfig? config = null, CancellationToken cancellationToken = default)

Parameters

fileSearchStoreName string

The store name, either fileSearchStores/{id} or just {id}.

fileName string

The Files API file name, either files/{id} or just {id}.

config ImportFileConfig

Optional custom metadata and chunking settings.

cancellationToken CancellationToken

Token to cancel the request.

Returns

Task<FileSearchOperation>

The long-running FileSearchOperation that tracks indexing; see WaitForOperationAsync(FileSearchOperation, TimeSpan?, TimeSpan?, CancellationToken).

See Also

ListAllDocumentsAsync(string, int?, CancellationToken)

Enumerates all documents in a File Search store, fetching subsequent pages as needed.

public IAsyncEnumerable<FileSearchDocument> ListAllDocumentsAsync(string fileSearchStoreName, int? pageSize = null, CancellationToken cancellationToken = default)

Parameters

fileSearchStoreName string

The store name, either fileSearchStores/{id} or just {id}.

pageSize int?

Optional page size used for each request.

cancellationToken CancellationToken

Token to cancel the enumeration.

Returns

IAsyncEnumerable<FileSearchDocument>

An async sequence of every document in the store.

ListAllFileSearchStoresAsync(int?, CancellationToken)

Enumerates all File Search stores, fetching subsequent pages as needed.

public IAsyncEnumerable<FileSearchStore> ListAllFileSearchStoresAsync(int? pageSize = null, CancellationToken cancellationToken = default)

Parameters

pageSize int?

Optional page size used for each request.

cancellationToken CancellationToken

Token to cancel the enumeration.

Returns

IAsyncEnumerable<FileSearchStore>

An async sequence of every store.

ListDocumentsAsync(string, int?, string?, CancellationToken)

Lists the documents in a File Search store, one page at a time.

public Task<ListFileSearchDocumentsResponse> ListDocumentsAsync(string fileSearchStoreName, int? pageSize = null, string? pageToken = null, CancellationToken cancellationToken = default)

Parameters

fileSearchStoreName string

The store name, either fileSearchStores/{id} or just {id}.

pageSize int?

Maximum number of documents per page (the service caps this at 20).

pageToken string

A NextPageToken from a previous call.

cancellationToken CancellationToken

Token to cancel the request.

Returns

Task<ListFileSearchDocumentsResponse>

One page of documents.

See Also

ListFileSearchStoresAsync(int?, string?, CancellationToken)

Lists the File Search stores owned by the project, one page at a time.

public Task<ListFileSearchStoresResponse> ListFileSearchStoresAsync(int? pageSize = null, string? pageToken = null, CancellationToken cancellationToken = default)

Parameters

pageSize int?

Maximum number of stores per page (the service caps this at 20).

pageToken string

A NextPageToken from a previous call.

cancellationToken CancellationToken

Token to cancel the request.

Returns

Task<ListFileSearchStoresResponse>

One page of stores.

See Also

UploadStreamToFileSearchStoreAsync(string, Stream, string, UploadToFileSearchStoreConfig?, Action<double>?, CancellationToken)

Uploads the contents of a stream directly into a File Search store as a new document. The stream is read from its current position to the end.

public Task<FileSearchOperation> UploadStreamToFileSearchStoreAsync(string fileSearchStoreName, Stream stream, string mimeType, UploadToFileSearchStoreConfig? config = null, Action<double>? progressCallback = null, CancellationToken cancellationToken = default)

Parameters

fileSearchStoreName string

The store name, either fileSearchStores/{id} or just {id}.

stream Stream

The data to upload. Like UploadStreamAsync(Stream, string, string, Action<double>?, CancellationToken), the stream is disposed once the upload request completes.

mimeType string

The MIME type of the data, e.g. application/pdf or text/plain.

config UploadToFileSearchStoreConfig

Optional display name, custom metadata and chunking settings. mimeType takes precedence over MimeType.

progressCallback Action<double>

Optional callback receiving upload progress as a percentage.

cancellationToken CancellationToken

Token to cancel the upload.

Returns

Task<FileSearchOperation>

The long-running FileSearchOperation that tracks indexing; see WaitForOperationAsync(FileSearchOperation, TimeSpan?, TimeSpan?, CancellationToken).

See Also

UploadToFileSearchStoreAsync(string, string, UploadToFileSearchStoreConfig?, Action<double>?, CancellationToken)

Uploads a local file directly into a File Search store, where it is chunked, embedded and indexed as a new document.

public Task<FileSearchOperation> UploadToFileSearchStoreAsync(string fileSearchStoreName, string filePath, UploadToFileSearchStoreConfig? config = null, Action<double>? progressCallback = null, CancellationToken cancellationToken = default)

Parameters

fileSearchStoreName string

The store name, either fileSearchStores/{id} or just {id}.

filePath string

Path of the local file to upload.

config UploadToFileSearchStoreConfig

Optional display name, MIME type (inferred from the extension when unset), custom metadata and chunking settings.

progressCallback Action<double>

Optional callback receiving upload progress as a percentage.

cancellationToken CancellationToken

Token to cancel the upload.

Returns

Task<FileSearchOperation>

The long-running FileSearchOperation that tracks indexing; see WaitForOperationAsync(FileSearchOperation, TimeSpan?, TimeSpan?, CancellationToken).

See Also

WaitForOperationAsync(FileSearchOperation, TimeSpan?, TimeSpan?, CancellationToken)

Polls an upload or import operation until it is done.

public Task<FileSearchOperation> WaitForOperationAsync(FileSearchOperation operation, TimeSpan? pollInterval = null, TimeSpan? timeout = null, CancellationToken cancellationToken = default)

Parameters

operation FileSearchOperation

The operation returned by an upload or import call.

pollInterval TimeSpan?

Delay between polls (default 2 seconds).

timeout TimeSpan?

Maximum time to wait (default 10 minutes).

cancellationToken CancellationToken

Token to cancel waiting.

Returns

Task<FileSearchOperation>

The completed operation, whose Response names the created document.

Exceptions

GenerativeAIException

The operation finished with an error.

TimeoutException

The operation did not finish within timeout.

See Also