Skip to content

Instantly share code, notes, and snippets.

@wilyJ80
Created January 11, 2026 22:26
Show Gist options
  • Select an option

  • Save wilyJ80/aa1d9e383d057851ed7449988475dfd7 to your computer and use it in GitHub Desktop.

Select an option

Save wilyJ80/aa1d9e383d057851ed7449988475dfd7 to your computer and use it in GitHub Desktop.
Gemini Image Generation with LangChain
from langchain_google_genai import ChatGoogleGenerativeAI, Modality
from base64 import b64decode
from datetime import datetime
from uuid import uuid4
model: str = "gemini-3-pro-image-preview"
# Force modality to discard verbal chat answer
llm = ChatGoogleGenerativeAI(model=model, response_modalities=[Modality.IMAGE])
prompt = "Generate an image of a Pinscher in a suit, on top of a table in a club. Shot with iPhone 6."
response = llm.invoke(prompt)
image_b64 = response.content[0]["image_url"]["url"]
# Convert b64 string to bytes
header, encoded = image_b64.split(",", 1)
image_bytes = b64decode(encoded)
filename = f"{str(uuid4())}.png"
with open(filename, "wb") as f:
f.write(image_bytes)
print(f"[INFO] File {filename} written to disk.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment