Skip to content

Commit 49112c6

Browse files
committed
feat(genai): Add additional Controlled Generation Samples
- CtrlGen with Enum Class - CtrlGen with Resp Schema Added requirements.txt
1 parent d99c471 commit 49112c6

File tree

4 files changed

+127
-0
lines changed

4 files changed

+127
-0
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# Copyright 2025 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
16+
def generate_content() -> str:
17+
# [START googlegenaisdk_ctrlgen_with_enum_class_schema]
18+
from google import genai
19+
import enum
20+
21+
class InstrumentClass(enum.Enum):
22+
PERCUSSION = "Percussion"
23+
STRING = "String"
24+
WOODWIND = "Woodwind"
25+
BRASS = "Brass"
26+
KEYBOARD = "Keyboard"
27+
28+
client = genai.Client()
29+
response = client.models.generate_content(
30+
model="gemini-2.0-flash-001",
31+
contents="What type of instrument is a guitar?",
32+
config={
33+
"response_mime_type": "text/x.enum",
34+
"response_schema": InstrumentClass,
35+
},
36+
)
37+
38+
print(response.text)
39+
# Example output:
40+
# String
41+
42+
# [END googlegenaisdk_ctrlgen_with_enum_class_schema]
43+
return response.text
44+
45+
46+
if __name__ == "__main__":
47+
generate_content()
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# Copyright 2025 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
16+
def generate_content() -> str:
17+
# [START googlegenaisdk_ctrlgen_with_resp_schema]
18+
from google import genai
19+
20+
response_schema = {
21+
"type": "ARRAY",
22+
"items": {
23+
"type": "OBJECT",
24+
"properties": {
25+
"recipe_name": {"type": "STRING"},
26+
"ingredients": {"type": "ARRAY", "items": {"type": "STRING"}},
27+
},
28+
"required": ["recipe_name", "ingredients"],
29+
},
30+
}
31+
32+
prompt = """
33+
List a few popular cookie recipes.
34+
"""
35+
36+
client = genai.Client()
37+
response = client.models.generate_content(
38+
model="gemini-2.0-flash-001",
39+
contents=prompt,
40+
config={
41+
"response_mime_type": "application/json",
42+
"response_schema": response_schema,
43+
},
44+
)
45+
46+
print(response.text)
47+
# Example output:
48+
# [
49+
# {
50+
# "ingredients": [
51+
# "2 1/4 cups all-purpose flour",
52+
# "1 teaspoon baking soda",
53+
# "1 teaspoon salt",
54+
# "1 cup (2 sticks) unsalted butter, softened",
55+
# "3/4 cup granulated sugar",
56+
# "3/4 cup packed brown sugar",
57+
# "1 teaspoon vanilla extract",
58+
# "2 large eggs",
59+
# "2 cups chocolate chips",
60+
# ],
61+
# "recipe_name": "Chocolate Chip Cookies",
62+
# }
63+
# ]
64+
# [END googlegenaisdk_ctrlgen_with_resp_schema]
65+
return response.text
66+
67+
68+
if __name__ == "__main__":
69+
generate_content()
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
google-genai==1.1.0

genai/controlled_generation/test_controlled_generation_samples.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@
2020

2121
import ctrlgen_with_class_schema
2222
import ctrlgen_with_enum_schema
23+
import ctrlgen_with_enum_class_schema
2324
import ctrlgen_with_nested_class_schema
2425
import ctrlgen_with_nullable_schema
26+
import ctrlgen_with_resp_schema
2527

2628
os.environ["GOOGLE_GENAI_USE_VERTEXAI"] = "True"
2729
os.environ["GOOGLE_CLOUD_LOCATION"] = "us-central1"
@@ -37,9 +39,17 @@ def test_ctrlgen_with_enum_schema() -> None:
3739
assert ctrlgen_with_enum_schema.generate_content()
3840

3941

42+
def test_ctrlgen_with_enum_class_schema() -> None:
43+
assert ctrlgen_with_enum_class_schema.generate_content()
44+
45+
4046
def test_ctrlgen_with_nested_class_schema() -> None:
4147
assert ctrlgen_with_nested_class_schema.generate_content()
4248

4349

4450
def test_ctrlgen_with_nullable_schema() -> None:
4551
assert ctrlgen_with_nullable_schema.generate_content()
52+
53+
54+
def test_ctrlgen_with_resp_schema() -> None:
55+
assert ctrlgen_with_resp_schema.generate_content()

0 commit comments

Comments
 (0)