Skip to content

Commit 72d9f4f

Browse files
speedstorm1copybara-github
authored andcommitted
chore: add unified content wrapping for multimodal gemini embeddings (gemini-embedding-2) for Gemini API
PiperOrigin-RevId: 889923492
1 parent 5c820f2 commit 72d9f4f

File tree

2 files changed

+35
-19
lines changed

2 files changed

+35
-19
lines changed

google/genai/models.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5864,9 +5864,8 @@ def embed_content(
58645864
},
58655865
)
58665866
5867-
# Multimodal embeddings are only supported for the Vertex AI API.
58685867
multimodal_embeddings = client.models.embed_content(
5869-
model='gemini-embedding-2-exp-11-2025',
5868+
model='gemini-embedding-2-preview',
58705869
contents=[
58715870
types.Part.from_uri(
58725871
file_uri='gs://generativeai-downloads/images/scones.jpg',
@@ -5879,6 +5878,8 @@ def embed_content(
58795878
)
58805879
"""
58815880
if not self._api_client.vertexai:
5881+
if 'gemini-embedding-2' in model:
5882+
contents = t.t_contents(contents) # type: ignore[assignment]
58825883
return self._embed_content(model=model, contents=contents, config=config)
58835884

58845885
if t.t_is_vertex_embed_content_model(model):
@@ -8508,9 +8509,8 @@ async def embed_content(
85088509
},
85098510
)
85108511
8511-
# Multimodal embeddings are only supported for the Vertex AI API.
85128512
multimodal_embeddings = await client.aio.models.embed_content(
8513-
model='gemini-embedding-2-exp-11-2025',
8513+
model='gemini-embedding-2-preview',
85148514
contents=[
85158515
types.Part.from_uri(
85168516
file_uri='gs://generativeai-downloads/images/scones.jpg',
@@ -8523,6 +8523,8 @@ async def embed_content(
85238523
)
85248524
"""
85258525
if not self._api_client.vertexai:
8526+
if 'gemini-embedding-2' in model:
8527+
contents = t.t_contents(contents) # type: ignore[assignment]
85268528
return await self._embed_content(
85278529
model=model, contents=contents, config=config
85288530
)

google/genai/tests/models/test_embed_content.py

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -94,16 +94,14 @@ def _get_bytes_from_file(relative_path: str) -> bytes:
9494
exception_if_mldev='parameter is not supported',
9595
),
9696
pytest_helper.TestTableItem(
97-
name='test_vertex_new_api_text_only',
97+
name='test_new_api_text_only',
9898
parameters=types.EmbedContentParameters(
99-
model='gemini-embedding-2-exp-11-2025',
99+
model='gemini-embedding-2-preview',
100100
contents=t.t_contents('What is your name?'),
101101
config={
102102
'output_dimensionality': 100,
103103
},
104104
),
105-
# Model not exposed on MLDev.
106-
exception_if_mldev='404',
107105
),
108106
pytest_helper.TestTableItem(
109107
name='test_vertex_new_api_maas',
@@ -120,9 +118,9 @@ def _get_bytes_from_file(relative_path: str) -> bytes:
120118
exception_if_mldev='404',
121119
),
122120
pytest_helper.TestTableItem(
123-
name='test_vertex_new_api_gcs_image_and_config',
121+
name='test_new_api_gcs_image_and_config',
124122
parameters=types.EmbedContentParameters(
125-
model='gemini-embedding-2-exp-11-2025',
123+
model='gemini-embedding-2-preview',
126124
contents=[
127125
types.Content(
128126
parts=[
@@ -145,13 +143,11 @@ def _get_bytes_from_file(relative_path: str) -> bytes:
145143
},
146144
},
147145
),
148-
# Model not exposed on MLDev.
149-
exception_if_mldev='404',
150146
),
151147
pytest_helper.TestTableItem(
152-
name='test_vertex_new_api_inline_pdf',
148+
name='test_new_api_inline_pdf',
153149
parameters=types.EmbedContentParameters(
154-
model='gemini-embedding-2-exp-11-2025',
150+
model='gemini-embedding-2-preview',
155151
contents=[
156152
types.Content(
157153
parts=[
@@ -166,8 +162,6 @@ def _get_bytes_from_file(relative_path: str) -> bytes:
166162
'output_dimensionality': 100,
167163
},
168164
),
169-
# Model not exposed on MLDev.
170-
exception_if_mldev='404',
171165
),
172166
pytest_helper.TestTableItem(
173167
name='test_vertex_new_api_list_of_contents_error',
@@ -191,6 +185,28 @@ def _get_bytes_from_file(relative_path: str) -> bytes:
191185
)
192186

193187

188+
def test_gemini_embedding_2_content_combination(client):
189+
response = client.models.embed_content(
190+
model='gemini-embedding-2-preview',
191+
contents=[
192+
'The jetpack is cool',
193+
types.Part.from_bytes(
194+
data=_get_bytes_from_file('../data/checkerboard.png'),
195+
mime_type='image/png',
196+
),
197+
types.Part.from_uri(
198+
file_uri='gs://generativeai-downloads/images/scones.jpg',
199+
mime_type='image/jpeg',
200+
),
201+
],
202+
config={'output_dimensionality': 100},
203+
)
204+
# assert has one total embedding
205+
assert response.embeddings is not None
206+
assert len(response.embeddings) == 1
207+
assert len(response.embeddings[0].values) == 100
208+
209+
194210
@pytest.mark.asyncio
195211
async def test_async(client):
196212
response = await client.aio.models.embed_content(
@@ -203,10 +219,8 @@ async def test_async(client):
203219

204220
@pytest.mark.asyncio
205221
async def test_async_new_api(client):
206-
if not client.vertexai:
207-
return
208222
response = await client.aio.models.embed_content(
209-
model='gemini-embedding-2-exp-11-2025',
223+
model='gemini-embedding-2-preview',
210224
contents=t.t_contents('What is your name?'),
211225
config={'output_dimensionality': 10},
212226
)

0 commit comments

Comments
 (0)