Skip to content

Commit 9ccc6ce

Browse files
matthew29tangcopybara-github
authored andcommitted
fix: Remove bytes for Gemini Developer API before sending a video for Veo Video Extension async
PiperOrigin-RevId: 822280664
1 parent f3f5a5c commit 9ccc6ce

File tree

2 files changed

+75
-0
lines changed

2 files changed

+75
-0
lines changed

google/genai/models.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7277,6 +7277,36 @@ async def generate_videos(
72777277
'Source and prompt/image/video are mutually exclusive.'
72787278
+ ' Please only use source.'
72797279
)
7280+
# Gemini Developer API does not support video bytes.
7281+
video_dct: dict[str, Any] = {}
7282+
if not self._api_client.vertexai and video:
7283+
if isinstance(video, types.Video):
7284+
video_dct = video.model_dump()
7285+
else:
7286+
video_dct = dict(video)
7287+
7288+
if video_dct.get('uri') and video_dct.get('video_bytes'):
7289+
video = types.Video(
7290+
uri=video_dct.get('uri'), mime_type=video_dct.get('mime_type')
7291+
)
7292+
elif not self._api_client.vertexai and source:
7293+
if isinstance(source, types.GenerateVideosSource):
7294+
source_dct = source.model_dump()
7295+
video_dct = source_dct.get('video', {})
7296+
else:
7297+
source_dct = dict(source)
7298+
if isinstance(source_dct.get('video'), types.Video):
7299+
video_obj: types.Video = source_dct.get('video', types.Video())
7300+
video_dct = video_obj.model_dump()
7301+
if video_dct and video_dct.get('uri') and video_dct.get('video_bytes'):
7302+
source = types.GenerateVideosSource(
7303+
prompt=source_dct.get('prompt'),
7304+
image=source_dct.get('image'),
7305+
video=types.Video(
7306+
uri=video_dct.get('uri'),
7307+
mime_type=video_dct.get('mime_type'),
7308+
),
7309+
)
72807310
return await self._generate_videos(
72817311
model=model,
72827312
prompt=prompt,

google/genai/tests/models/test_generate_videos.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -700,3 +700,48 @@ async def test_text_to_video_poll_async(client):
700700
operation = await client.aio.operations.get(operation=operation)
701701

702702
assert operation.result.generated_videos[0].video.uri
703+
704+
705+
@pytest.mark.asyncio
706+
async def test_generated_video_extension_from_source_poll_async(client):
707+
# Gemini API only supports video extension on generated videos.
708+
if client.vertexai:
709+
return
710+
711+
operation1 = await client.aio.models.generate_videos(
712+
model="veo-3.1-generate-preview",
713+
prompt="Rain",
714+
config=types.GenerateVideosConfig(
715+
number_of_videos=1,
716+
),
717+
)
718+
while not operation1.done:
719+
# Skip the sleep when in replay mode.
720+
if client._api_client._mode not in ("replay", "auto"):
721+
time.sleep(20)
722+
operation1 = await client.aio.operations.get(operation=operation1)
723+
724+
video1 = operation1.result.generated_videos[0].video
725+
assert video1.uri
726+
assert await client.aio.files.download(file=video1)
727+
728+
operation2 = await client.aio.models.generate_videos(
729+
model="veo-3.1-generate-preview",
730+
source=types.GenerateVideosSource(
731+
prompt="Sun",
732+
video=video1
733+
),
734+
config=types.GenerateVideosConfig(
735+
number_of_videos=1,
736+
),
737+
)
738+
while not operation2.done:
739+
# Skip the sleep when in replay mode.
740+
if client._api_client._mode not in ("replay", "auto"):
741+
time.sleep(20)
742+
operation2 = await client.aio.operations.get(operation=operation2)
743+
744+
video2 = operation2.result.generated_videos[0].video
745+
assert video2.uri
746+
assert await client.aio.files.download(file=video2)
747+

0 commit comments

Comments
 (0)