0
# Batch Operations
1
2
Create and manage batch jobs for processing multiple requests efficiently with reduced costs and optimized throughput.
3
4
## Core Imports
5
6
```java
7
import com.google.genai.Batches;
8
import com.google.genai.AsyncBatches;
9
import com.google.genai.Pager;
10
import com.google.genai.types.BatchJob;
11
import com.google.genai.types.BatchJobSource;
12
import com.google.genai.types.BatchJobDestination;
13
import com.google.genai.types.CreateBatchJobConfig;
14
import com.google.genai.types.CreateEmbeddingsBatchJobConfig;
15
import com.google.genai.types.EmbeddingsBatchJobSource;
16
import com.google.genai.types.InlinedRequest;
17
import com.google.genai.types.DeleteResourceJob;
18
```
19
20
## Batches Service
21
22
```java { .api }
23
package com.google.genai;
24
25
public final class Batches {
26
public BatchJob create(String model, BatchJobSource src, CreateBatchJobConfig config);
27
public BatchJob createEmbeddings(String model, EmbeddingsBatchJobSource src, CreateEmbeddingsBatchJobConfig config);
28
public BatchJob get(String name, GetBatchJobConfig config);
29
public void cancel(String name, CancelBatchJobConfig config);
30
public DeleteResourceJob delete(String name, DeleteBatchJobConfig config);
31
public Pager<BatchJob> list(ListBatchJobsConfig config);
32
}
33
```
34
35
## Batch Job Type
36
37
```java { .api }
38
package com.google.genai.types;
39
40
public final class BatchJob {
41
public Optional<String> name();
42
public Optional<String> displayName();
43
public Optional<String> model();
44
public Optional<String> state();
45
public Optional<String> createTime();
46
public Optional<String> updateTime();
47
public Optional<String> endTime();
48
public Optional<String> startTime();
49
public Optional<BatchJobSource> src();
50
public Optional<BatchJobDestination> dest();
51
public Optional<CompletionStats> completionStats();
52
public Optional<Status> error();
53
}
54
```
55
56
## Batch Job Source/Destination
57
58
```java { .api }
59
package com.google.genai.types;
60
61
public final class BatchJobSource {
62
public static Builder builder();
63
64
public Optional<String> gcsUri();
65
public Optional<String> bigqueryUri();
66
public Optional<String> fileName();
67
public Optional<List<InlinedRequest>> inlinedRequests();
68
public Optional<String> format();
69
}
70
71
public final class BatchJobDestination {
72
public static Builder builder();
73
74
public Optional<String> gcsUri();
75
public Optional<String> bigqueryUri();
76
public Optional<String> fileName();
77
public Optional<List<InlinedResponse>> inlinedResponses();
78
public Optional<String> format();
79
}
80
```
81
82
## Create Batch Job (GCS)
83
84
```java
85
BatchJobSource source = BatchJobSource.builder()
86
.gcsUri("gs://my-bucket/input/requests.jsonl")
87
.build();
88
89
CreateBatchJobConfig config = CreateBatchJobConfig.builder()
90
.dest(BatchJobDestination.builder()
91
.gcsUri("gs://my-bucket/output/")
92
.build())
93
.build();
94
95
BatchJob job = client.batches.create(
96
"gemini-2.0-flash",
97
source,
98
config
99
);
100
101
System.out.println("Batch job created: " + job.name().orElse("N/A"));
102
```
103
104
## Create Batch Job (Inlined - Gemini API)
105
106
```java
107
import com.google.genai.types.InlinedRequest;
108
109
List<InlinedRequest> requests = ImmutableList.of(
110
InlinedRequest.builder()
111
.contents(ImmutableList.of(Content.fromParts(Part.fromText("Request 1"))))
112
.build(),
113
InlinedRequest.builder()
114
.contents(ImmutableList.of(Content.fromParts(Part.fromText("Request 2"))))
115
.build(),
116
InlinedRequest.builder()
117
.contents(ImmutableList.of(Content.fromParts(Part.fromText("Request 3"))))
118
.build()
119
);
120
121
BatchJobSource source = BatchJobSource.builder()
122
.inlinedRequests(requests)
123
.build();
124
125
CreateBatchJobConfig config = CreateBatchJobConfig.builder()
126
.dest(BatchJobDestination.builder()
127
.fileName("files/output-batch")
128
.build())
129
.build();
130
131
BatchJob job = client.batches.create("gemini-2.0-flash", source, config);
132
```
133
134
## Monitor Batch Job
135
136
```java
137
BatchJob job = client.batches.get(jobName, null);
138
139
while (!"COMPLETE".equals(job.state().orElse(""))) {
140
if ("FAILED".equals(job.state().orElse(""))) {
141
System.err.println("Job failed: " + job.error().orElse(null));
142
break;
143
}
144
145
Thread.sleep(10000);
146
job = client.batches.get(job.name().get(), null);
147
System.out.println("State: " + job.state().orElse("N/A"));
148
}
149
150
job.completionStats().ifPresent(stats -> {
151
System.out.println("Successful: " + stats.successfulRequestCount().orElse(0));
152
System.out.println("Failed: " + stats.failedRequestCount().orElse(0));
153
});
154
```
155
156
## Create Embeddings Batch
157
158
```java
159
EmbeddingsBatchJobSource source = EmbeddingsBatchJobSource.builder()
160
.fileName("files/embedding-requests")
161
.build();
162
163
CreateEmbeddingsBatchJobConfig config = CreateEmbeddingsBatchJobConfig.builder()
164
.dest(BatchJobDestination.builder()
165
.fileName("files/embedding-outputs")
166
.build())
167
.build();
168
169
BatchJob job = client.batches.createEmbeddings(
170
"text-embedding-004",
171
source,
172
config
173
);
174
```
175
176
## List Batch Jobs
177
178
```java
179
Pager<BatchJob> pager = client.batches.list(null);
180
181
for (BatchJob job : pager) {
182
System.out.println("Job: " + job.displayName().orElse("N/A"));
183
System.out.println(" State: " + job.state().orElse("N/A"));
184
System.out.println(" Created: " + job.createTime().orElse("N/A"));
185
}
186
```
187
188
## Cancel Batch Job
189
190
```java
191
client.batches.cancel(jobName, null);
192
System.out.println("Batch job cancelled");
193
```
194
195
## Delete Batch Job
196
197
```java
198
DeleteResourceJob result = client.batches.delete(jobName, null);
199
System.out.println("Batch job deleted");
200
```
201
202
## JSONL Format
203
204
Input file format (`requests.jsonl`):
205
206
```json
207
{"contents":[{"parts":[{"text":"Request 1"}]}]}
208
{"contents":[{"parts":[{"text":"Request 2"}]}]}
209
{"contents":[{"parts":[{"text":"Request 3"}]}]}
210
```
211
212
Output format:
213
214
```json
215
{"response":{"candidates":[{"content":{"parts":[{"text":"Response 1"}]}}]}}
216
{"response":{"candidates":[{"content":{"parts":[{"text":"Response 2"}]}}]}}
217
{"response":{"candidates":[{"content":{"parts":[{"text":"Response 3"}]}}]}}
218
```
219