Skip to content

Commit b9d3be1

Browse files
matthew29tangcopybara-github
authored andcommitted
feat: Automatically determine mime_type for Part.from_uri
PiperOrigin-RevId: 749089175
1 parent 473bf4b commit b9d3be1

3 files changed

Lines changed: 47 additions & 4 deletions

File tree

google/genai/tests/models/test_generate_content_part.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -467,9 +467,10 @@ def test_from_uploaded_file_uri(client):
467467
],
468468
)
469469

470-
def test_from_uri_error(client):
471-
# missing mime_type
472-
with pytest.raises(TypeError):
470+
471+
def test_from_uri_inferred_mime_type(client):
472+
# gs://generativeai-downloads/images/scones.jpg isn't supported in MLDev
473+
with pytest_helper.exception_if_mldev(client, errors.ClientError):
473474
client.models.generate_content(
474475
model='gemini-1.5-flash',
475476
contents=[
@@ -481,6 +482,20 @@ def test_from_uri_error(client):
481482
)
482483

483484

485+
def test_from_uri_invalid_inferred_mime_type(client):
486+
# Throws ValueError if mime_type cannot be inferred.
487+
with pytest.raises(ValueError):
488+
client.models.generate_content(
489+
model='gemini-1.5-flash',
490+
contents=[
491+
'What is this image about?',
492+
types.Part.from_uri(
493+
file_uri='uri/without/mime/type'
494+
),
495+
],
496+
)
497+
498+
484499
def test_audio_uri(client):
485500
with pytest_helper.exception_if_mldev(client, errors.ClientError):
486501
client.models.generate_content(

google/genai/tests/types/test_types.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,19 @@ def test_factory_method_from_uri_part():
4141
assert isinstance(my_part, SubPart)
4242

4343

44+
def test_factory_method_from_uri_inferred_mime_type_part():
45+
46+
my_part = SubPart.from_uri(
47+
file_uri='gs://generativeai-downloads/images/scones.jpg',
48+
)
49+
assert (
50+
my_part.file_data.file_uri
51+
== 'gs://generativeai-downloads/images/scones.jpg'
52+
)
53+
assert my_part.file_data.mime_type == 'image/jpeg'
54+
assert isinstance(my_part, SubPart)
55+
56+
4457
def test_factory_method_from_text_part():
4558
my_part = SubPart.from_text(text='What is your name?')
4659
assert my_part.text == 'What is your name?'

google/genai/types.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -642,7 +642,22 @@ class Part(_common.BaseModel):
642642
)
643643

644644
@classmethod
645-
def from_uri(cls, *, file_uri: str, mime_type: str) -> 'Part':
645+
def from_uri(
646+
cls, *, file_uri: str, mime_type: Optional[str] = None
647+
) -> 'Part':
648+
"""Creates a Part from a file uri.
649+
650+
Args:
651+
file_uri (str): The uri of the file
652+
mime_type (str): mime_type: The MIME type of the image. If not provided,
653+
the MIME type will be automatically determined.
654+
"""
655+
if mime_type is None:
656+
import mimetypes
657+
658+
mime_type, _ = mimetypes.guess_type(file_uri)
659+
if not mime_type:
660+
raise ValueError(f'Failed to determine mime type for file: {file_uri}')
646661
file_data = FileData(file_uri=file_uri, mime_type=mime_type)
647662
return cls(file_data=file_data)
648663

0 commit comments

Comments
 (0)