@@ -66,9 +66,9 @@ func JsonNoSchema() (*genai.GenerateContentResponse, error) {
66
66
log .Fatal (err )
67
67
}
68
68
prompt := "List a few popular cookie recipes in JSON format.\n \n " +
69
- "Use this JSON schema:\n \n " +
70
- "Recipe = {'recipe_name': str, 'ingredients': list[str]}\n " +
71
- "Return: list[Recipe]"
69
+ "Use this JSON schema:\n \n " +
70
+ "Recipe = {'recipe_name': str, 'ingredients': list[str]}\n " +
71
+ "Return: list[Recipe]"
72
72
response , err := client .Models .GenerateContent (ctx , "gemini-2.0-flash" , genai .Text (prompt ), nil )
73
73
if err != nil {
74
74
log .Fatal (err )
@@ -222,7 +222,7 @@ func JsonEnumRaw() (*genai.GenerateContentResponse, error) {
222
222
}
223
223
224
224
parts := []* genai.Part {
225
- {Text : "Please summarize this transcript " },
225
+ {Text : "What kind of instrument is this: " },
226
226
{InlineData : & genai.Blob {Data : data , MIMEType : "image/jpeg" }},
227
227
}
228
228
contents := []* genai.Content {
@@ -252,12 +252,32 @@ func XEnum() (*genai.GenerateContentResponse, error) {
252
252
log .Fatal (err )
253
253
}
254
254
255
+ // Choice is a custom type representing a musical instrument category.
256
+ type Choice string
257
+
258
+ const (
259
+ Percussion Choice = "Percussion"
260
+ String Choice = "String"
261
+ Woodwind Choice = "Woodwind"
262
+ Brass Choice = "Brass"
263
+ Keyboard Choice = "Keyboard"
264
+ )
265
+
266
+ // Define a schema restricting the response to the allowed Choice enum values.
267
+ schema := & genai.Schema {
268
+ Type : genai .TypeString ,
269
+ Enum : []string {
270
+ string (Percussion ),
271
+ string (String ),
272
+ string (Woodwind ),
273
+ string (Brass ),
274
+ string (Keyboard ),
275
+ },
276
+ }
277
+
255
278
config := & genai.GenerateContentConfig {
256
279
ResponseMIMEType : "text/x.enum" ,
257
- ResponseSchema : & genai.Schema {
258
- Type : genai .TypeString ,
259
- Enum : []string {"Percussion" , "String" , "Woodwind" , "Brass" , "Keyboard" },
260
- },
280
+ ResponseSchema : schema ,
261
281
}
262
282
263
283
// Open the file.
@@ -274,7 +294,7 @@ func XEnum() (*genai.GenerateContentResponse, error) {
274
294
}
275
295
276
296
parts := []* genai.Part {
277
- {Text : "Please summarize this transcript " },
297
+ {Text : "What kind of instrument is this: " },
278
298
{InlineData : & genai.Blob {Data : data , MIMEType : "image/jpeg" }},
279
299
}
280
300
contents := []* genai.Content {
@@ -326,7 +346,7 @@ func XEnumRaw() (*genai.GenerateContentResponse, error) {
326
346
}
327
347
328
348
parts := []* genai.Part {
329
- {Text : "Please summarize this transcript " },
349
+ {Text : "What kind of instrument is this: " },
330
350
{InlineData : & genai.Blob {Data : data , MIMEType : "image/jpeg" }},
331
351
}
332
352
contents := []* genai.Content {
0 commit comments