Google’s Gemini 2.5 Flash Image (Stream) for image generation via Gemini API. Real-time SSE delivery of thinking and image chunks.
Gemini 2.5 Flash Image (Stream) generates images using Google’s Gemini 2.5 Flash Image model through the native Gemini API via AnyFast. Responses are delivered in real-time using Server-Sent Events (SSE), with thinking chunks arriving first followed by the image data chunk.
Streaming requires a Direct group token. Select the Direct group when creating your token in the AnyFast console.
In the streaming response, the image field is named inlineData (camelCase). In the non-streaming request body, the field is inline_data (snake_case). This is a native Gemini API behaviour.
import requests, base64, jsonurl = "https://www.anyfast.ai/v1beta/models/gemini-2.5-flash-image:streamGenerateContent"params = {"key": "YOUR_API_KEY"}data = { "contents": [ { "role": "user", "parts": [{"text": "Generate an image of a sunset over mountains"}] } ], "generationConfig": { "responseModalities": ["TEXT", "IMAGE"], "imageConfig": {"aspectRatio": "16:9", "imageSize": "1K"} }}response = requests.post(url, params=params, json=data, stream=True)for line in response.iter_lines(): if not line: continue decoded = line.decode("utf-8") if not decoded.startswith("data:"): continue chunk = json.loads(decoded[len("data:"):].strip()) candidates = chunk.get("candidates", []) if not candidates: continue parts = candidates[0].get("content", {}).get("parts", []) for part in parts: # Skip thinking chunks if part.get("thought"): continue # Save image chunk if "inlineData" in part: img_bytes = base64.b64decode(part["inlineData"]["data"]) with open("output.png", "wb") as f: f.write(img_bytes) print("Image saved to output.png")
Include both a text instruction and an inline_data reference image in the same parts array.
# First encode your image to base64:# BASE64=$(base64 -i your_photo.jpg)## Then send the request:curl "https://www.anyfast.ai/v1beta/models/gemini-2.5-flash-image:streamGenerateContent?key=YOUR_API_KEY" \ -H "Content-Type: application/json" \ -d '{ "contents": [ { "role": "user", "parts": [ { "text": "Hi, this is a picture of me. Can you add a llama next to me?" }, { "inline_data": { "mime_type": "image/jpeg", "data": "<YOUR_BASE64_ENCODED_IMAGE>" } } ] } ], "generationConfig": { "responseModalities": ["TEXT", "IMAGE"], "imageConfig": { "aspectRatio": "1:1", "imageSize": "1K" } } }'
import requests, base64, json# Read and encode your reference imagewith open("your_photo.jpg", "rb") as f: image_b64 = base64.b64encode(f.read()).decode("utf-8")url = "https://www.anyfast.ai/v1beta/models/gemini-2.5-flash-image:streamGenerateContent"params = {"key": "YOUR_API_KEY"}data = { "contents": [ { "role": "user", "parts": [ { "text": "Hi, this is a picture of me. Can you add a llama next to me?" }, { "inline_data": { "mime_type": "image/jpeg", "data": image_b64 } } ] } ], "generationConfig": { "responseModalities": ["TEXT", "IMAGE"], "imageConfig": {"aspectRatio": "1:1", "imageSize": "1K"} }}response = requests.post(url, params=params, json=data, stream=True)for line in response.iter_lines(): if not line: continue decoded = line.decode("utf-8") if not decoded.startswith("data:"): continue chunk = json.loads(decoded[len("data:"):].strip()) candidates = chunk.get("candidates", []) if not candidates: continue parts = candidates[0].get("content", {}).get("parts", []) for part in parts: if part.get("thought"): continue if "inlineData" in part: img_bytes = base64.b64decode(part["inlineData"]["data"]) with open("output.png", "wb") as f: f.write(img_bytes) print("Image saved to output.png")