Plan: Gemini image models on Vertex, and a generate_image tool
Closes: #481. Branch: feat/vertex-images. Status: Done (2026-10-26) — implemented as planned, with the erratum below
Design
Why not the chat path
pi’s assistant message carries text, thinking and tool calls; an image part a chat model returned has nowhere to go, so pi treats image generation as a separate model operation (ImageModel, generateImages: node_modules/@earendil-works/pi-ai/dist/types.d.ts, ImageModel and ProviderImages). Google’s chat models (gemini-3.8-flash and kin) produce only text on the API anyway: image generation is the "Nano Banana" family, separate models. pi ships one image API, openrouter-images (KnownImageApi), so Vertex needs an implementation of its own.
What was measured (2026-10-26, project gadhs-claude-enterprise-dev, global)
One real generation per model, generateContent with responseModalities: ["TEXT","IMAGE"], images inspected: all five produced the asked-for picture.
| Vertex model id | Time | MIME | Notes |
|---|---|---|---|
|
11.5 s |
PNG |
thinks (476 tokens); the image part arrived twice |
|
8.3 s |
PNG |
"Nano Banana 2" |
|
2.8 s |
JPEG |
fastest, cheapest |
|
16.5 s |
PNG |
thinks (100 tokens); the image part arrived twice; "Nano Banana Pro" |
|
5.2 s |
PNG |
also serves in |
Usage came back with candidatesTokensDetails split by modality (IMAGE 1120 tokens for a 1K image; TEXT separately) and thoughtsTokenCount. The -preview ids in Google’s docs return not-found here; the GA ids above are what the catalogue lists.
Part 1: pi-vertex registers the image models
The catalogue. Five entries in packages/pi-vertex/models.json, each "publisher": "google", "region": "global", and a new field "type": "image" on ModelEntry (config.ts; absent means chat, as today). Each carries output: ["image"], or ["text","image"] for gemini-2.5-flash-image, and its costs from Google’s pricing page (USD per 1M tokens, read 2026-10-26):
| Model | Input | Text out | Image out |
|---|---|---|---|
|
2.00 |
12.00 |
120.00 |
|
0.50 |
3.00 |
60.00 |
|
0.25 |
1.50 |
30.00 |
|
1.50 |
7.50 |
30.00 |
|
0.30 |
2.50 |
30.00 |
pi’s cost has one output rate, and image tokens are priced apart from text tokens. So an image entry carries pi’s cost with output = the image rate (the common case: an image answer is almost all image tokens), plus a pi-vertex-only imageCost: { textOutput } the implementation uses to price the text part at its own rate. The implementation computes usage.cost itself from the response’s per-modality counts, so the figure pi records is the true one, not the single-rate estimate. Thinking tokens are text output.
Registration. Image entries share the vertex-gemini provider id with the chat entries, so the google kind’s one registerProvider call carries one mixed models array (chat entries as today; image entries with type: "image", api: "vertex-images", output) and an images map { "vertex-images": { generateImages } }. That is pi’s shape: ProviderConfigInput has one models?: ProviderModelConfig[] and images?: Partial<Record<ImageApi, ProviderImages>> (node_modules/@earendil-works/pi-coding-agent/dist/core/provider-composer.d.ts:55,60). provider.ts’s structural `ProviderConfig and ProviderModelConfig grow those optional fields (type, output, images); toProviderModel emits an image entry without reasoning, contextWindow and maxTokens, which pi’s ImageModel does not carry (pi-ai/dist/types.d.ts, "Image-generation model"). The installed pi’s documentation (1.4, custom-provider.md, "Register mixed-operation models") shows the same registration; the copy under node_modules predates that section.
The implementation (packages/pi-vertex/images.ts, ≤ 200 lines): a fetch to https://aiplatform.googleapis.com/v1/projects/<project>/locations/global/publishers/google/models/<id>:generateContent (the region from the entry, as other kinds do; global for all five), bearer token from createTokenSource() (adc-token.ts), one re-fetch on a 401 as maas.ts does. Request: contents: [{ role: "user", parts }] where text input becomes { text } parts and image input { inlineData: { mimeType, data } }; generationConfig: { responseModalities: ["TEXT","IMAGE"] }. Response: parts with thought: true are skipped; inlineData parts become image blocks with the response’s MIME type; text parts become text blocks. A duplicated image part (identical data twice, as two models returned) is kept once. stopReason is "stop" on a 200 with at least one image, "error" with errorMessage otherwise, Google’s safety block (finishReason other than STOP, or promptFeedback.blockReason) included, never a throw for a provider error, matching openrouter-images.js. signal aborts the fetch. Usage: promptTokenCount → input; candidatesTokensDetails by modality and thoughtsTokenCount → text and image output priced at their rates; totalTokens from the response.
Nothing else changes for chat. The streams, the probe (image ids answer its one-token generateContent with 200, as they did) and the ADC retry are untouched. catalogueDefaults is not consulted for image entries.
Part 2: generate_image in pi-modes
A tool, registered as count and ask are (command-wiring.ts, from registerToolsAndCommands in packages/pi-modes/index.ts), with a "": "allow" rule under its own surface in every mode that has rules: auto, plan and manual. The monitor’s tools carry one in plan too (modes.json) though plan’s tools list omits them, because a tool with no rule falls to the judge, whose rubric cannot know what it does. It is *not in plan mode’s tools list: plan changes nothing, and an image is a file. Helpers get it only when their agent file names it; no packaged agent does, and the profile drift test says so.
| Parameter | Type | Meaning |
|---|---|---|
|
string |
What to generate or how to edit the references. |
|
string |
Where to save, relative to the workspace or absolute inside it. The extension is from the response’s MIME type when the path has none; several images are numbered |
|
string[]? |
Reference images to edit or follow, workspace paths; read as |
|
string? |
|
Containment is the tool’s own. The workspace-write rule applies to the write and edit tools (authorize.ts, isWriteAsk), and a custom tool’s ask carries no path; so the tool itself refuses a path or reference outside the session’s workspace root, a credential-shaped name (credentialShaped, authorize.ts:115, pi-modes' own), and a path under .git. The output file does not exist yet, so its containment is decided as authorize.ts decides a write’s: reallyInside(path, cwd) (authorize.ts:220), which walks the nearest existing ancestor’s real location. Not pi-workflow’s resolveWorkspacePath, whose realpathSync of the target fails on a new file. References exist, so reallyInside serves them too. It never overwrites: an existing file is a refusal naming it.
The call goes through ctx.modelRegistry.generateImages(model, { input }) (model-registry.d.ts:49), the model found by findOfType("image", provider, id); an unknown or unavailable model is a tool error naming models.getAvailableOfType("image")’s list. The result’s usage is returned on the tool result (`usage), so pi counts it in the session total and the spend cap sees it, as a helper’s spend is counted (#124).
The result the model reads: the saved path(s), any text the model returned, the cost, and the image(s) themselves as image content, so the model can look at what it made and iterate (passing the saved file back as a reference). A stopReason of error is a thrown tool error with Google’s message, so a safety refusal is visible.
Risks
| Risk | Mode | Mitigation |
|---|---|---|
A write outside the workspace through the tool |
Fail-closed |
The tool’s own containment, tested: |
Cost mis-stated (one |
Silent |
|
A thinking model’s thought parts shown as text, or a duplicated image shown twice |
Silent |
The filter and the dedupe, tested on the captured shapes (the two-image responses above). |
Credentials in a log or error |
Silent |
The token never appears in an error; errors carry Google’s |
The request shape drifts from Vertex’s |
Fail-closed (a changed API returns an error result, never an image) |
A contract test pins the exact request body for text-only and text-plus-reference input; one live generation per release is the operator’s call, not a test. |
Provider registration breaks chat models |
Fail-closed (pi’s |
The existing provider tests run; a new one asserts the google kind registers the mixed |
Scope
In
-
packages/pi-vertex/config.ts:ModelEntry.type?: "chat" | "image",output?,imageCost?; validation. -
packages/pi-vertex/images.ts: the implementation;index.tsandprovider.ts: registration. -
packages/pi-vertex/models.json: the five entries. -
packages/pi-modes/image-tool.ts: the tool;command-wiring.tsandindex.ts: registration;modes.json: the allow rules in auto, plan and manual;config.ts:ModesConfig.imageModel?: string, validated asprovider/id. -
Tests. pi-vertex: contract tests on the request body and on captured response shapes (usage priced to the cent, the thought filter, the dedupe, a safety block, a 401 whose error carries no token), and unit tests on registration and config validation. pi-modes: unit tests on containment (the six refusals), the result shape, the default and configured model, and the error paths, against a fake registry; the existing tool-surface test (
test/tool-surfaces.test.ts) and the profile drift test gain the new tool. -
Docs: the vertex page (the image models, costs, the chat-path explanation), the cookbook ("Generate an image", and codemode’s
models.generateImagesas the other route), the modes page (the tool), CHANGELOG; this plan underdocs/modules/ROOT/pages/plans/vertex-images.adoc, linked fromdocs/modules/ROOT/nav.adoc.
Out
-
Turning codemode on by default: a separate decision (#481 notes it).
-
Resolution and aspect-ratio controls (
imageConfig): a follow-up once the default is in use; the defaults measured above are 1K. -
Video or other Google media models.
-
Non-global regions for image models:
globalserves all five today.
Delivery
One branch, one MR, Closes #481. The plan is committed first. Tests with the code; validate green before push; docs and CHANGELOG on the same branch. One live generation to confirm the wired path end to end before the MR, at a few cents, is approved by the operator (2026-10-26, "you can spend money on it").
Erratum (2026-10-26): the tool-surface test the plan names (test/tool-surfaces.test.ts) is about count; the new tool is pinned instead by the registration test in test/image-tool.test.ts and the drift test in test/subagent-gate.test.ts (no packaged agent declares it). reallyInside is exported from authorize.ts for the tool. The probe’s candidate list gains the five ids, since the catalog must stay a subset of what the probe asks about; they answer its one-token call. Live check before the MR: pi’s own registry listed the five image models, and the tool’s body generated and saved a PNG through it for $0.0376.