Skip to content

Commit c28bc08

Browse files
MarkDaoustcopybara-github
authored andcommitted
Chore: Simplify the type-signature for contents.
This is a superset of what was here before, it shouldn't break anyone. - It removes the redundant list types (`list[x|y] | list[x|y|z]`). - It adds the missing `PartDict` and `FileDict` entries so `generate_content(contents={'text': "hello?"})` will pass mypy now. - Adds tests for `contents:File` and `contents:FileDict` - Reduces total formatted lines from 51 -> 42 - Reduces nesting "[" from 11 to 7. - Reorders to put simple types first. PiperOrigin-RevId: 789105998
1 parent bcef0a6 commit c28bc08

4 files changed

Lines changed: 84 additions & 19 deletions

File tree

google/genai/models.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5746,7 +5746,7 @@ def generate_content(
57465746
self,
57475747
*,
57485748
model: str,
5749-
contents: Union[types.ContentListUnion, types.ContentListUnionDict],
5749+
contents: types.ContentListUnionDict,
57505750
config: Optional[types.GenerateContentConfigOrDict] = None,
57515751
) -> types.GenerateContentResponse:
57525752
"""Makes an API request to generate content using a model.
@@ -5882,7 +5882,7 @@ def generate_content_stream(
58825882
self,
58835883
*,
58845884
model: str,
5885-
contents: Union[types.ContentListUnion, types.ContentListUnionDict],
5885+
contents: types.ContentListUnionDict,
58865886
config: Optional[types.GenerateContentConfigOrDict] = None,
58875887
) -> Iterator[types.GenerateContentResponse]:
58885888
"""Makes an API request to generate content using a model and yields the model's response in chunks.

google/genai/tests/data/story.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
The old lighthouse keeper, Silas, squinted at the churning sea. A storm, the likes of which he hadn't seen in fifty years, was brewing. Waves, the size of houses, were crashing against the jagged rocks below. The wind howled like a banshee, and the rain stung his face like a thousand tiny needles.
2+
3+
He checked the lamp, its beam a comforting, steady light against the growing darkness. It was his duty, his oath, to keep that light burning, no matter what. He'd seen too many ships lost to the treacherous coast.
4+
5+
Suddenly, a monstrous wave slammed into the lighthouse, shaking it to its foundations. The lamp flickered, and a cold dread washed over Silas. He scrambled to re-ignite the flame, his hands trembling. He had to, for the sake of the ships, and for the memory of all the sailors he had saved. He managed to get it back on just as the next huge wave hit.
6+
7+
The storm raged for what felt like an eternity. When the first rays of dawn finally broke through the clouds, the sea had calmed, and a small ship, battered but intact, was sailing towards the harbor. Silas, exhausted but relieved, watched the ship safely reach the shore. He knew that his light had guided them through the sh**storm. He smiled, the light reflecting in his kind eyes, and went to make his morning coffee.

google/genai/tests/models/test_generate_content_part.py

Lines changed: 54 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -486,18 +486,66 @@ def test_model_content_text(client):
486486
assert response.text
487487

488488

489+
def test_from_file_input(client):
490+
with pytest_helper.exception_if_vertex(client, ValueError):
491+
file = client.files.upload(file='tests/data/story.txt')
492+
client.models.generate_content(
493+
model='gemini-2.5-flash',
494+
contents=file,
495+
)
496+
client.models.generate_content(
497+
model='gemini-2.5-flash',
498+
contents=[
499+
'Summarize this file',
500+
file,
501+
],
502+
)
503+
client.models.generate_content(
504+
model='gemini-2.5-flash',
505+
contents=[['Summarize this file', file]],
506+
)
507+
508+
509+
def test_from_file_dict_input(client):
510+
with pytest_helper.exception_if_vertex(client, ValueError):
511+
file = client.files.upload(file='tests/data/story.txt')
512+
file_dict = file.model_dump()
513+
client.models.generate_content(
514+
model='gemini-2.5-flash',
515+
contents=file_dict,
516+
)
517+
client.models.generate_content(
518+
model='gemini-2.5-flash',
519+
contents=[
520+
'Summarize this file',
521+
file_dict,
522+
],
523+
)
524+
client.models.generate_content(
525+
model='gemini-2.5-flash',
526+
contents=[['Summarize this file', file_dict]],
527+
)
528+
529+
489530
def test_from_uploaded_file_uri(client):
490-
with pytest_helper.exception_if_vertex(client, errors.ClientError):
531+
with pytest_helper.exception_if_vertex(client, ValueError):
532+
file = client.files.upload(file='tests/data/story.txt')
533+
file_part = types.Part.from_uri(file_uri=file.uri, mime_type='text/plain')
491534
client.models.generate_content(
492-
model='gemini-1.5-flash',
535+
model='gemini-2.5-flash',
536+
contents=file_part,
537+
)
538+
client.models.generate_content(
539+
model='gemini-2.5-flash',
493540
contents=[
494541
'Summarize this file',
495-
types.Part.from_uri(
496-
file_uri='https://generativelanguage.googleapis.com/v1beta/files/w1l20sq33nwn',
497-
mime_type='text/plain',
498-
),
542+
file_part,
499543
],
500544
)
545+
client.models.generate_content(
546+
model='gemini-2.5-flash',
547+
contents=[['Summarize this file', file_part]],
548+
)
501549

502550

503551
def test_from_uri_inferred_mime_type(client):

google/genai/types.py

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3676,19 +3676,25 @@ class FileDict(TypedDict, total=False):
36763676

36773677
FileOrDict = Union[File, FileDict]
36783678

3679+
36793680
if _is_pillow_image_imported:
3680-
PartUnion = Union[File, Part, PIL_Image, str]
3681+
PartUnion = Union[str, PIL_Image, File, Part]
36813682
else:
3682-
PartUnion = Union[File, Part, str] # type: ignore[misc]
3683+
PartUnion = Union[str, File, Part] # type: ignore[misc]
36833684

36843685

3685-
PartUnionDict = Union[PartUnion, PartDict]
3686+
if _is_pillow_image_imported:
3687+
PartUnionDict = Union[str, PIL_Image, File, FileDict, Part, PartDict]
3688+
else:
3689+
PartUnionDict = Union[str, File, FileDict, Part, PartDict] # type: ignore[misc]
36863690

36873691

3688-
ContentUnion = Union[Content, list[PartUnion], PartUnion]
3692+
ContentUnion = Union[Content, PartUnion, list[PartUnion]]
36893693

36903694

3691-
ContentUnionDict = Union[ContentUnion, ContentDict]
3695+
ContentUnionDict = Union[
3696+
Content, ContentDict, PartUnionDict, list[PartUnionDict]
3697+
]
36923698

36933699

36943700
class GenerationConfigRoutingConfigAutoRoutingMode(_common.BaseModel):
@@ -3764,10 +3770,10 @@ class GenerationConfigRoutingConfigDict(TypedDict, total=False):
37643770
]
37653771

37663772

3767-
SpeechConfigUnion = Union[SpeechConfig, str]
3773+
SpeechConfigUnion = Union[str, SpeechConfig]
37683774

37693775

3770-
SpeechConfigUnionDict = Union[SpeechConfigUnion, SpeechConfigDict]
3776+
SpeechConfigUnionDict = Union[str, SpeechConfig, SpeechConfigDict]
37713777

37723778

37733779
class GenerateContentConfig(_common.BaseModel):
@@ -4160,10 +4166,10 @@ class GenerateContentConfigDict(TypedDict, total=False):
41604166
]
41614167

41624168

4163-
ContentListUnion = Union[list[ContentUnion], ContentUnion]
4169+
ContentListUnion = Union[ContentUnion, list[ContentUnion]]
41644170

41654171

4166-
ContentListUnionDict = Union[list[ContentUnionDict], ContentUnionDict]
4172+
ContentListUnionDict = Union[ContentUnionDict, list[ContentUnionDict]]
41674173

41684174

41694175
class _GenerateContentParameters(_common.BaseModel):
@@ -12594,13 +12600,17 @@ class LiveClientRealtimeInputDict(TypedDict, total=False):
1259412600
LiveClientRealtimeInput, LiveClientRealtimeInputDict
1259512601
]
1259612602

12603+
1259712604
if _is_pillow_image_imported:
12598-
BlobImageUnion = Union[Blob, PIL_Image]
12605+
BlobImageUnion = Union[PIL_Image, Blob]
1259912606
else:
1260012607
BlobImageUnion = Blob # type: ignore[misc]
1260112608

1260212609

12603-
BlobImageUnionDict = Union[BlobImageUnion, BlobDict]
12610+
if _is_pillow_image_imported:
12611+
BlobImageUnionDict = Union[PIL_Image, Blob, BlobDict]
12612+
else:
12613+
BlobImageUnionDict = Union[Blob, BlobDict] # type: ignore[misc]
1260412614

1260512615

1260612616
class LiveSendRealtimeInputParameters(_common.BaseModel):

0 commit comments

Comments
 (0)