Commit 4c27179
feat(google): allow using Gemini image models with
## Background
Gemini image models (e.g., `gemini-2.5-flash-image`) are multimodal
output models that are primarily known for their image generation and
editing capabilities. While they can technically be used via
`generateText()`, `generateImage` provides a more intuitive API when
working with these models for image generation tasks. The lack of
dedicated `generateImage()` support meant users had to parse through
result parts to find images, and there was no guarantee an image would
be produced (as reported in #10674).
## Summary
This PR adds support for using Gemini image models with
`generateImage()` in the Google provider. The implementation internally
calls the language model API with `responseModalities: ['IMAGE']` to
generate images, then parses the response into the format expected by
`generateImage()`.
Key changes:
- Added support for Gemini image models (e.g., `gemini-2.5-flash-image`,
`gemini-3-pro-image-preview`) in `GoogleGenerativeAIImageModel`
- Implemented `doGenerateGemini()` method that uses
`GoogleGenerativeAILanguageModel` internally with `responseModalities:
['IMAGE']`
- Added support for image editing by passing input files/URLs through to
the language model
- Proper error handling for unsupported options:
- Throws error when `n` is explicitly used (Gemini doesn't support
generating a set number of images per call)
- Throws error when `mask` is provided (Gemini doesn't support
mask-based inpainting)
- Returns warning when `size` is used, in accordance with existing
behavior for Imagen models (should use `aspectRatio` instead)
- Updated documentation with dedicated "Gemini Image Models" section
explaining usage and capabilities
- Added comprehensive test coverage for Gemini image generation and
editing scenarios
- Updated image model ID types to include `gemini-2.5-flash-image` and
`gemini-3-pro-image-preview`
- Updated examples to use `generateImage()` instead of `generateText()`
for Gemini image models
The implementation is backward compatible - existing Imagen models
continue to work as before, and Gemini image models can continue to be
used with `generateText`.
## Manual Verification
Ran the updated example scripts to verify:
```bash
# Generate image
pnpm tsx examples/ai-functions/src/generate-image/google-gemini-image.ts
# Image editing with local file
pnpm tsx examples/ai-functions/src/generate-image/google-gemini-editing.ts
# Image editing with URL
pnpm tsx examples/ai-functions/src/generate-image/google-gemini-editing-url.ts
```
All examples successfully generated and saved images to the `output/`
directory.
## Checklist
- [x] Tests have been added / updated (for bug fixes / features)
- [x] Documentation has been added / updated (for bug fixes / features)
- [x] A _patch_ changeset for relevant packages has been added (for bug
fixes / features - run `pnpm changeset` in the project root)
- [x] I have reviewed this pull request (self-review)
## Future Work
We probably want to add the same handling to the `google-vertex`
provider for these models.
## Related Issues
Fixes #12252
---------
Co-authored-by: Gregor Martynus <39992+gr2m@users.noreply.github.com>generateImage (#12267)1 parent 8f7a309 commit 4c27179
File tree
9 files changed
+675
-111
lines changed- .changeset
- content/providers/01-ai-sdk-providers
- examples/ai-functions/src/generate-image
- packages/google/src
9 files changed
+675
-111
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
Lines changed: 93 additions & 4 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
929 | 929 | | |
930 | 930 | | |
931 | 931 | | |
932 | | - | |
| 932 | + | |
933 | 933 | | |
934 | 934 | | |
935 | 935 | | |
| |||
948 | 948 | | |
949 | 949 | | |
950 | 950 | | |
| 951 | + | |
| 952 | + | |
| 953 | + | |
| 954 | + | |
| 955 | + | |
| 956 | + | |
951 | 957 | | |
952 | 958 | | |
953 | 959 | | |
| |||
1146 | 1152 | | |
1147 | 1153 | | |
1148 | 1154 | | |
1149 | | - | |
| 1155 | + | |
1150 | 1156 | | |
1151 | 1157 | | |
| 1158 | + | |
| 1159 | + | |
| 1160 | + | |
| 1161 | + | |
| 1162 | + | |
| 1163 | + | |
| 1164 | + | |
| 1165 | + | |
| 1166 | + | |
1152 | 1167 | | |
1153 | 1168 | | |
1154 | 1169 | | |
| |||
1178 | 1193 | | |
1179 | 1194 | | |
1180 | 1195 | | |
1181 | | - | |
| 1196 | + | |
1182 | 1197 | | |
1183 | 1198 | | |
1184 | 1199 | | |
| |||
1188 | 1203 | | |
1189 | 1204 | | |
1190 | 1205 | | |
1191 | | - | |
| 1206 | + | |
1192 | 1207 | | |
1193 | 1208 | | |
1194 | 1209 | | |
1195 | 1210 | | |
1196 | 1211 | | |
1197 | 1212 | | |
| 1213 | + | |
| 1214 | + | |
| 1215 | + | |
| 1216 | + | |
| 1217 | + | |
| 1218 | + | |
| 1219 | + | |
| 1220 | + | |
| 1221 | + | |
| 1222 | + | |
| 1223 | + | |
| 1224 | + | |
| 1225 | + | |
| 1226 | + | |
| 1227 | + | |
| 1228 | + | |
| 1229 | + | |
| 1230 | + | |
| 1231 | + | |
| 1232 | + | |
| 1233 | + | |
| 1234 | + | |
| 1235 | + | |
| 1236 | + | |
| 1237 | + | |
| 1238 | + | |
| 1239 | + | |
| 1240 | + | |
| 1241 | + | |
| 1242 | + | |
| 1243 | + | |
| 1244 | + | |
| 1245 | + | |
| 1246 | + | |
| 1247 | + | |
| 1248 | + | |
| 1249 | + | |
| 1250 | + | |
| 1251 | + | |
| 1252 | + | |
| 1253 | + | |
| 1254 | + | |
| 1255 | + | |
| 1256 | + | |
| 1257 | + | |
| 1258 | + | |
| 1259 | + | |
| 1260 | + | |
| 1261 | + | |
| 1262 | + | |
| 1263 | + | |
| 1264 | + | |
| 1265 | + | |
| 1266 | + | |
| 1267 | + | |
| 1268 | + | |
| 1269 | + | |
| 1270 | + | |
| 1271 | + | |
| 1272 | + | |
| 1273 | + | |
| 1274 | + | |
| 1275 | + | |
| 1276 | + | |
| 1277 | + | |
| 1278 | + | |
| 1279 | + | |
| 1280 | + | |
| 1281 | + | |
| 1282 | + | |
| 1283 | + | |
| 1284 | + | |
| 1285 | + | |
| 1286 | + | |
Lines changed: 11 additions & 34 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | | - | |
| 2 | + | |
3 | 3 | | |
4 | 4 | | |
| 5 | + | |
5 | 6 | | |
6 | 7 | | |
7 | | - | |
8 | | - | |
9 | | - | |
10 | | - | |
11 | | - | |
12 | | - | |
13 | | - | |
14 | | - | |
15 | | - | |
16 | | - | |
17 | | - | |
18 | | - | |
19 | | - | |
20 | | - | |
21 | | - | |
22 | | - | |
23 | | - | |
24 | | - | |
25 | | - | |
26 | | - | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
27 | 16 | | |
28 | 17 | | |
29 | | - | |
30 | | - | |
31 | | - | |
32 | | - | |
33 | | - | |
34 | | - | |
35 | | - | |
36 | | - | |
37 | | - | |
38 | | - | |
39 | | - | |
40 | | - | |
41 | | - | |
| 18 | + | |
42 | 19 | | |
Lines changed: 17 additions & 46 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | | - | |
| 2 | + | |
3 | 3 | | |
4 | 4 | | |
| 5 | + | |
5 | 6 | | |
6 | 7 | | |
7 | 8 | | |
8 | | - | |
9 | | - | |
| 9 | + | |
| 10 | + | |
10 | 11 | | |
11 | 12 | | |
12 | 13 | | |
13 | 14 | | |
14 | | - | |
15 | 15 | | |
16 | 16 | | |
17 | 17 | | |
18 | 18 | | |
19 | | - | |
20 | | - | |
21 | | - | |
22 | | - | |
23 | | - | |
24 | | - | |
25 | | - | |
26 | | - | |
27 | | - | |
28 | | - | |
29 | | - | |
30 | | - | |
31 | | - | |
32 | | - | |
33 | | - | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
34 | 25 | | |
35 | 26 | | |
36 | | - | |
37 | | - | |
38 | | - | |
39 | | - | |
40 | | - | |
41 | | - | |
42 | | - | |
43 | | - | |
44 | | - | |
45 | | - | |
46 | | - | |
47 | | - | |
48 | | - | |
49 | | - | |
50 | | - | |
51 | | - | |
52 | | - | |
53 | | - | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
54 | 33 | | |
55 | 34 | | |
56 | | - | |
57 | | - | |
58 | | - | |
59 | | - | |
60 | | - | |
61 | | - | |
62 | | - | |
63 | | - | |
64 | | - | |
| 35 | + | |
65 | 36 | | |
Lines changed: 5 additions & 15 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | | - | |
| 2 | + | |
3 | 3 | | |
4 | 4 | | |
| 5 | + | |
5 | 6 | | |
6 | 7 | | |
7 | | - | |
8 | | - | |
| 8 | + | |
| 9 | + | |
9 | 10 | | |
10 | 11 | | |
11 | 12 | | |
12 | 13 | | |
13 | | - | |
14 | | - | |
15 | | - | |
16 | | - | |
17 | | - | |
18 | | - | |
19 | | - | |
20 | | - | |
21 | | - | |
22 | | - | |
23 | | - | |
| 14 | + | |
24 | 15 | | |
25 | 16 | | |
26 | 17 | | |
27 | | - | |
28 | 18 | | |
Lines changed: 5 additions & 4 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | | - | |
| 2 | + | |
3 | 3 | | |
| 4 | + | |
4 | 5 | | |
5 | 6 | | |
6 | | - | |
7 | | - | |
| 7 | + | |
| 8 | + | |
8 | 9 | | |
9 | 10 | | |
10 | 11 | | |
11 | | - | |
| 12 | + | |
12 | 13 | | |
0 commit comments