@@ -91,6 +91,10 @@ def to_mapping_value(value) -> struct_pb2.Struct:
91
91
# We got a dict (or something dict-like); convert it.
92
92
return struct_pb2 .Struct (fields = {k : to_value (v ) for k , v in value .items ()})
93
93
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" ]
94
98
95
99
class Image :
96
100
"""Image."""
@@ -222,15 +226,15 @@ def _generate_images(
222
226
number_of_images : int = 1 ,
223
227
width : Optional [int ] = None ,
224
228
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 ,
226
230
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 ,
228
232
compression_quality : Optional [float ] = None ,
229
233
language : Optional [str ] = None ,
230
234
safety_filter_level : Optional [
231
- Literal ["block_most" , "block_some" , "block_few" , "block_fewest" ]
235
+ Literal [* SAFETY_FILTER_LEVELS ]
232
236
] = None ,
233
- person_generation : Optional [Literal ["dont_allow" , "allow_adult" , "allow_all" ]] = None ,
237
+ person_generation : Optional [Literal [* PERSON_GENERATIONS ]] = None ,
234
238
) -> "ImageGenerationResponse" :
235
239
"""Generates images from text prompt.
236
240
@@ -287,6 +291,8 @@ def _generate_images(
287
291
parameters = {}
288
292
max_size = max (width or 0 , height or 0 ) or None
289
293
if aspect_ratio is not None :
294
+ if aspect_ratio not in ASPECT_RATIOS :
295
+ raise ValueError (f'aspect_ratio not in { ASPECT_RATIOS } ' )
290
296
parameters ["aspectRatio" ] = aspect_ratio
291
297
elif max_size :
292
298
# Note: The size needs to be a string
@@ -309,6 +315,8 @@ def _generate_images(
309
315
310
316
parameters ["outputOptions" ] = {}
311
317
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 } ' )
312
320
parameters ["outputOptions" ]["mimeType" ] = output_mime_type
313
321
shared_generation_parameters ["mime_type" ] = output_mime_type
314
322
@@ -317,6 +325,8 @@ def _generate_images(
317
325
shared_generation_parameters ["compression_quality" ] = compression_quality
318
326
319
327
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 } ' )
320
330
parameters ["safetySetting" ] = safety_filter_level
321
331
shared_generation_parameters ["safety_filter_level" ] = safety_filter_level
322
332
0 commit comments