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
platformIPlatformAdapterThe platform adapter used for URLs and authorization.
httpClientHttpClientOptional HTTP client.
loggerILoggerOptional 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
storeFileSearchStoreThe store definition.
cancellationTokenCancellationTokenToken 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
displayNamestringOptional human-readable display name (at most 512 characters).
embeddingModelstringOptional embedding model, e.g.
gemini-embedding-001ormodels/gemini-embedding-001. The service default is used when omitted.cancellationTokenCancellationTokenToken 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
documentNamestringThe full document name:
fileSearchStores/{store}/documents/{document}.forcebool?When
true, also deletes the document's chunks. Whenfalseor unset, the request fails if the document has chunks.cancellationTokenCancellationTokenToken to cancel the request.
Returns
- See Also
DeleteFileSearchStoreAsync(string, bool?, CancellationToken)
Deletes a File Search store.
public Task DeleteFileSearchStoreAsync(string name, bool? force = null, CancellationToken cancellationToken = default)
Parameters
namestringThe store name, either
fileSearchStores/{id}or just{id}.forcebool?When
true, also deletes all documents in the store. Whenfalseor unset, the request fails if the store contains documents.cancellationTokenCancellationTokenToken to cancel the request.
Returns
- See Also
DownloadMediaAsync(string, CancellationToken)
Downloads media referenced by File Search grounding metadata.
public Task<byte[]> DownloadMediaAsync(string mediaId, CancellationToken cancellationToken = default)
Parameters
mediaIdstringThe media name, in the form
fileSearchStores/{store}/media/{blob}.cancellationTokenCancellationTokenToken to cancel the request.
Returns
GetDocumentAsync(string, CancellationToken)
Gets a document from a File Search store.
public Task<FileSearchDocument> GetDocumentAsync(string documentName, CancellationToken cancellationToken = default)
Parameters
documentNamestringThe full document name:
fileSearchStores/{store}/documents/{document}.cancellationTokenCancellationTokenToken to cancel the request.
Returns
- See Also
GetFileSearchStoreAsync(string, CancellationToken)
Gets a File Search store.
public Task<FileSearchStore> GetFileSearchStoreAsync(string name, CancellationToken cancellationToken = default)
Parameters
namestringThe store name, either
fileSearchStores/{id}or just{id}.cancellationTokenCancellationTokenToken to cancel the request.
Returns
- 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
operationNamestringThe operation name, e.g.
fileSearchStores/{store}/operations/{id}orfileSearchStores/{store}/upload/operations/{id}.cancellationTokenCancellationTokenToken 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
fileSearchStoreNamestringThe store name, either
fileSearchStores/{id}or just{id}.fileNamestringThe Files API file name, either
files/{id}or just{id}.configImportFileConfigOptional custom metadata and chunking settings.
cancellationTokenCancellationTokenToken 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
fileSearchStoreNamestringThe store name, either
fileSearchStores/{id}or just{id}.pageSizeint?Optional page size used for each request.
cancellationTokenCancellationTokenToken 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
pageSizeint?Optional page size used for each request.
cancellationTokenCancellationTokenToken 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
fileSearchStoreNamestringThe store name, either
fileSearchStores/{id}or just{id}.pageSizeint?Maximum number of documents per page (the service caps this at 20).
pageTokenstringA NextPageToken from a previous call.
cancellationTokenCancellationTokenToken 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
pageSizeint?Maximum number of stores per page (the service caps this at 20).
pageTokenstringA NextPageToken from a previous call.
cancellationTokenCancellationTokenToken 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
fileSearchStoreNamestringThe store name, either
fileSearchStores/{id}or just{id}.streamStreamThe data to upload. Like UploadStreamAsync(Stream, string, string, Action<double>?, CancellationToken), the stream is disposed once the upload request completes.
mimeTypestringThe MIME type of the data, e.g.
application/pdfortext/plain.configUploadToFileSearchStoreConfigOptional display name, custom metadata and chunking settings.
mimeTypetakes precedence over MimeType.progressCallbackAction<double>Optional callback receiving upload progress as a percentage.
cancellationTokenCancellationTokenToken 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
fileSearchStoreNamestringThe store name, either
fileSearchStores/{id}or just{id}.filePathstringPath of the local file to upload.
configUploadToFileSearchStoreConfigOptional display name, MIME type (inferred from the extension when unset), custom metadata and chunking settings.
progressCallbackAction<double>Optional callback receiving upload progress as a percentage.
cancellationTokenCancellationTokenToken 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
operationFileSearchOperationThe operation returned by an upload or import call.
pollIntervalTimeSpan?Delay between polls (default 2 seconds).
timeoutTimeSpan?Maximum time to wait (default 10 minutes).
cancellationTokenCancellationTokenToken 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.