Class GoogleAiGeminiBatchChatModel
-
Nested Class Summary
Nested Classes -
Method Summary
Modifier and TypeMethodDescriptionvoidCancels a batch operation that is currently pending or running.createBatchFromFile(String displayName, GeminiFiles.GeminiFile file) Creates a batch of chat requests from an uploaded file.createBatchInline(String displayName, @Nullable Long priority, List<ChatRequest> requests) Creates and enqueues a batch of content generation requests for asynchronous processing.voidDeletes a batch job from the system.listBatchJobs(@Nullable Integer pageSize, @Nullable String pageToken) Lists batch jobs with optional pagination.Retrieves the current state and results of a batch operation.voidwriteBatchToFile(JsonLinesWriter writer, Iterable<BatchRequestResponse.BatchFileRequest<ChatRequest>> requests) Writes a batch of chat requests to a JSONL file for later upload and processing.
-
Method Details
-
createBatchInline
public BatchRequestResponse.BatchResponse<ChatResponse> createBatchInline(String displayName, @Nullable Long priority, List<ChatRequest> requests) Creates and enqueues a batch of content generation requests for asynchronous processing.This method submits multiple chat requests as a single batch operation to the Gemini API. All requests in the batch must use the same model. The batch will be processed asynchronously, and the initial response will typically be in a
BatchRequestResponse.BatchIncompletestate.Batch processing offers a 50% cost reduction compared to real-time requests and has a 24-hour turnaround SLO, making it ideal for large-scale, non-urgent tasks.
Note: The inline API allows for a total request size of 20MB or under. Larger requests should use the File API
- Parameters:
displayName- a user-defined name for the batch, used for identificationpriority- optional priority for the batch; batches with higher priority values are processed before those with lower values; negative values are allowed; defaults to 0 if nullrequests- a list of chat requests to be processed in the batch; all requests must use the same model- Returns:
- a
BatchRequestResponse.BatchResponserepresenting the initial state of the batch operation, typicallyBatchRequestResponse.BatchIncomplete - Throws:
IllegalArgumentException- if the requests contain different models
-
createBatchFromFile
public BatchRequestResponse.BatchResponse<ChatResponse> createBatchFromFile(String displayName, GeminiFiles.GeminiFile file) Creates a batch of chat requests from an uploaded file.This method allows you to create a batch job using a JSONL file that has been previously uploaded to the Gemini Files API. This is useful for larger batches that exceed the 20 MB inline request limit.
The file must contain batch requests in JSONL format, where each line is a JSON object with a "key" and "request" field. You can use
writeBatchToFile(JsonLinesWriter, Iterable)to create properly formatted JSONL files.- Parameters:
displayName- a user-defined name for the batch, used for identificationfile- the GeminiFile object representing the uploaded file containing batch requests- Returns:
- a
BatchRequestResponse.BatchResponserepresenting the initial state of the batch operation, typicallyBatchRequestResponse.BatchIncomplete - See Also:
-
writeBatchToFile
public void writeBatchToFile(JsonLinesWriter writer, Iterable<BatchRequestResponse.BatchFileRequest<ChatRequest>> requests) throws IOException Writes a batch of chat requests to a JSONL file for later upload and processing.This method serializes chat requests into JSONL (JSON Lines) format, where each line contains a single request wrapped in a
BatchRequestResponse.BatchFileRequestwith a unique key. The resulting file can be uploaded using the Gemini Files API and then used to create a batch job viacreateBatchFromFile(String, GeminiFile).Each request is converted to the internal Gemini format before being written to the file.
Example usage:
Path batchFile = Files.createTempFile("batch", ".jsonl"); try (JsonLinesWriter writer = new StreamingJsonLinesWriter(batchFile)) { List<BatchFileRequest<ChatRequest>> requests = List.of( new BatchFileRequest<>("request-1", ChatRequest.builder() .messages(UserMessage.from("Question 1")) .build()), new BatchFileRequest<>("request-2", ChatRequest.builder() .messages(UserMessage.from("Question 2")) .build()) ); batchModel.writeBatchToFile(writer, requests); }- Parameters:
writer- the JsonLinesWriter to which the batch requests will be writtenrequests- an iterable collection of BatchFileRequest objects containing ChatRequest instances, each with a unique key identifier- Throws:
IOException- if an I/O error occurs while writing to the writer- See Also:
-
retrieveBatchResults
public BatchRequestResponse.BatchResponse<ChatResponse> retrieveBatchResults(BatchRequestResponse.BatchName name) Retrieves the current state and results of a batch operation.This method polls the Gemini API to get the latest state of a previously created batch. The response can be:
BatchRequestResponse.BatchIncomplete- if the batch is still pending or runningBatchRequestResponse.BatchSuccess- if the batch completed successfully, containing all responsesBatchRequestResponse.BatchError- if the batch failed, containing error details
Clients should poll this method at intervals to check the operation status until completion.
- Parameters:
name- the name of the batch operation to retrieve, obtained from the initialcreateBatchInline(String, Long, List)call- Returns:
- a
BatchRequestResponse.BatchResponserepresenting the current state of the batch operation
-
cancelBatchJob
Cancels a batch operation that is currently pending or running.This method attempts to cancel a batch job. Cancellation is only possible for batches that are in
BatchRequestResponse.BatchJobState.BATCH_STATE_PENDINGorBatchRequestResponse.BatchJobState.BATCH_STATE_RUNNINGstate. Batches that have already completed, failed, or been cancelled cannot be cancelled.- Parameters:
name- the name of the batch operation to cancel- Throws:
HttpException- if the batch cannot be cancelled (e.g., already completed, already cancelled, or does not exist)
-
deleteBatchJob
Deletes a batch job from the system.This removes the batch job but does not cancel it if still running. Use
cancelBatchJob(BatchName)to cancel a running batch.- Parameters:
name- the name of the batch job to delete- Throws:
RuntimeException- if the batch job cannot be deleted or does not exist
-
listBatchJobs
public BatchRequestResponse.BatchList<ChatResponse> listBatchJobs(@Nullable Integer pageSize, @Nullable String pageToken) Lists batch jobs with optional pagination.- Parameters:
pageSize- the maximum number of batch jobs to return; if null, uses server defaultpageToken- token for retrieving a specific page fromBatchRequestResponse.BatchList.pageToken(); if null, returns the first page- Returns:
- a
BatchRequestResponse.BatchListcontaining batch responses and a token for the next page - Throws:
RuntimeException- if the server does not support this operation
-