Skip to content

Instantly share code, notes, and snippets.

@up1
Last active March 12, 2026 09:12
Show Gist options
  • Select an option

  • Save up1/5909618febd4aea845e14c9822aedc8a to your computer and use it in GitHub Desktop.

Select an option

Save up1/5909618febd4aea845e14c9822aedc8a to your computer and use it in GitHub Desktop.
Demo with Gemini Embedding 2 preview
from google import genai
from google.genai import types
MODEL_ID = "gemini-embedding-2-preview"
client = genai.Client() # Assumes that the environment variable GOOGLE_API_KEY is set
# This example shows how to embed both text and an image using the Gemini embedding model.
with open("gemini-embedding-2.jpg", "rb") as f:
image_bytes = f.read()
result = client.models.embed_content(
model=MODEL_ID,
contents=[
types.Content(
parts=[
types.Part(text="Hello Gemini embedding 2 preview"),
types.Part.from_bytes(
data=image_bytes,
mime_type="image/jpeg",
),
]
)
],
)
# This produces one embedding
for embedding in result.embeddings:
print("Embedding size:", len(embedding.values))
print("Embedding for content:")
print(embedding.values)
Embedding size: 3072
Embedding for content:
[-0.0066875685, 0.02640603, -0.017316187, 0.017908914, -0.0020837318,
-0.017711164, -0.035931446, 0.0010204086, 0.0034726176, -0.059705723, -0.0051356093,
0.019519681, -0.0030912803, -0.025513291, 0.027540058, 0.0015974143, 0.046740968,
...
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment