Skip to content

Commit dfecd12

Browse files
committed
check enums
Change-Id: I8e7b92fc15d3941b7fa74a97b95d2577be9d6c1f
1 parent 8cbe814 commit dfecd12

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

google/generativeai/generative_models.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
from collections.abc import Iterable
66
import textwrap
7-
from typing import Any, Union, overload
7+
from typing import Any, Literal, Union, overload
88
import reprlib
99

1010
# pylint: disable=bad-continuation, line-too-long

google/generativeai/vision_models/_vision_models.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,10 @@ def to_mapping_value(value) -> struct_pb2.Struct:
9191
# We got a dict (or something dict-like); convert it.
9292
return struct_pb2.Struct(fields={k: to_value(v) for k, v in value.items()})
9393

94+
ASPECT_RATIOS = ["1:1", "9:16", "16:9", "4:3", "3:4"]
95+
OUTPUT_MIME_TYPES = ["image/png", "image/jpeg"]
96+
SAFETY_FILTER_LEVELS = ["block_most", "block_some", "block_few", "block_fewest"]
97+
PERSON_GENERATIONS = ["dont_allow", "allow_adult", "allow_all"]
9498

9599
class Image:
96100
"""Image."""
@@ -222,15 +226,15 @@ def _generate_images(
222226
number_of_images: int = 1,
223227
width: Optional[int] = None,
224228
height: Optional[int] = None,
225-
aspect_ratio: Optional[Literal["1:1", "9:16", "16:9", "4:3", "3:4"]] = None,
229+
aspect_ratio: Optional[Literal[*ASPECT_RATIOS]] = None,
226230
guidance_scale: Optional[float] = None,
227-
output_mime_type: Optional[Literal["image/png", "image/jpeg"]] = None,
231+
output_mime_type: Optional[Literal[*OUTPUT_MIME_TYPES]] = None,
228232
compression_quality: Optional[float] = None,
229233
language: Optional[str] = None,
230234
safety_filter_level: Optional[
231-
Literal["block_most", "block_some", "block_few", "block_fewest"]
235+
Literal[*SAFETY_FILTER_LEVELS]
232236
] = None,
233-
person_generation: Optional[Literal["dont_allow", "allow_adult", "allow_all"]] = None,
237+
person_generation: Optional[Literal[*PERSON_GENERATIONS]] = None,
234238
) -> "ImageGenerationResponse":
235239
"""Generates images from text prompt.
236240
@@ -287,6 +291,8 @@ def _generate_images(
287291
parameters = {}
288292
max_size = max(width or 0, height or 0) or None
289293
if aspect_ratio is not None:
294+
if aspect_ratio not in ASPECT_RATIOS:
295+
raise ValueError(f'aspect_ratio not in {ASPECT_RATIOS}')
290296
parameters["aspectRatio"] = aspect_ratio
291297
elif max_size:
292298
# Note: The size needs to be a string
@@ -309,6 +315,8 @@ def _generate_images(
309315

310316
parameters["outputOptions"] = {}
311317
if output_mime_type is not None:
318+
if output_mime_type not in OUTPUT_MIME_TYPES:
319+
raise ValueError(f'output_mime_type not in {OUTPUT_MIME_TYPES}')
312320
parameters["outputOptions"]["mimeType"] = output_mime_type
313321
shared_generation_parameters["mime_type"] = output_mime_type
314322

@@ -317,6 +325,8 @@ def _generate_images(
317325
shared_generation_parameters["compression_quality"] = compression_quality
318326

319327
if safety_filter_level is not None:
328+
if safety_filter_level not in SAFETY_FILTER_LEVELS:
329+
raise ValueError(f'safety_filter_level not in {SAFETY_FILTER_LEVELS}')
320330
parameters["safetySetting"] = safety_filter_level
321331
shared_generation_parameters["safety_filter_level"] = safety_filter_level
322332

0 commit comments

Comments
 (0)