base_url. See Gemini-compatible API for the concept.
You’ll need an active API key with permission for the model you call. See Authentication.
1. Install and configure
2. Text-to-image
Request theIMAGE modality and read the base64 image from the first candidate.
candidates[0].content.parts[0].inlineData.data.
Resolution
For NanoBanana 2 and Pro, select the output size withimageSize — 1K, 2K, 4K, plus 512px on NanoBanana 2:
imageSize is validated by the model — supported values are 1K, 2K, 4K (plus 512px on NanoBanana 2); anything else returns 400 INVALID_ARGUMENT. NanoBanana (gemini-2.5-flash-image) outputs 1K. Sizes below 1K (e.g. 512px) are billed at the 1K rate.3. Image-to-image (edit)
Pass one or more input images plus an instruction incontents.
4. Streaming
Usegenerate_content_stream to receive the response as it’s produced.
:streamGenerateContent?alt=sse, which returns Server-Sent Events (data: {json} frames) — exactly what the SDK consumes.
5. Count tokens
countTokens is proxied straight to Google, so you get a real token count for your request — useful for budgeting before you generate.
Preview model ids are supported here too — see Models.
6. Handle errors
BananaHub returns errors in the Gemini envelope, so the SDK raiseserrors.APIError with the canonical status: