Skip to content

Commit 8d1091a

Browse files
Abhi-13copybara-github
authored andcommitted
feat: Add ModelArmorConfig support for prompt and response sanitization via the Model Armor service
PiperOrigin-RevId: 857115428
1 parent 351e490 commit 8d1091a

File tree

4 files changed

+76
-0
lines changed

4 files changed

+76
-0
lines changed

google/genai/batches.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1013,6 +1013,11 @@ def _GenerateContentConfig_to_mldev(
10131013
getv(from_object, ['enable_enhanced_civic_answers']),
10141014
)
10151015

1016+
if getv(from_object, ['model_armor_config']) is not None:
1017+
raise ValueError(
1018+
'model_armor_config parameter is not supported in Gemini API.'
1019+
)
1020+
10161021
return to_object
10171022

10181023

google/genai/models.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1117,6 +1117,11 @@ def _GenerateContentConfig_to_mldev(
11171117
getv(from_object, ['enable_enhanced_civic_answers']),
11181118
)
11191119

1120+
if getv(from_object, ['model_armor_config']) is not None:
1121+
raise ValueError(
1122+
'model_armor_config parameter is not supported in Gemini API.'
1123+
)
1124+
11201125
return to_object
11211126

11221127

@@ -1279,6 +1284,13 @@ def _GenerateContentConfig_to_vertex(
12791284
'enable_enhanced_civic_answers parameter is not supported in Vertex AI.'
12801285
)
12811286

1287+
if getv(from_object, ['model_armor_config']) is not None:
1288+
setv(
1289+
parent_object,
1290+
['modelArmorConfig'],
1291+
getv(from_object, ['model_armor_config']),
1292+
)
1293+
12821294
return to_object
12831295

12841296

google/genai/tests/models/test_generate_content.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,22 @@ class InstrumentEnum(Enum):
546546
),
547547
exception_if_vertex='not supported',
548548
),
549+
pytest_helper.TestTableItem(
550+
name='test_model_armor_config',
551+
parameters=types._GenerateContentParameters(
552+
model=GEMINI_FLASH_LATEST,
553+
contents=t.t_contents('What is your name?'),
554+
config={
555+
'model_armor_config': {
556+
'prompt_template_name': '',
557+
'response_template_name': '',
558+
# Intentionally left blank just to test that the SDK doesn't
559+
# throw an exception.
560+
},
561+
},
562+
),
563+
exception_if_mldev='not supported',
564+
),
549565
]
550566

551567
pytestmark = pytest_helper.setup(

google/genai/types.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5003,6 +5003,38 @@ class SafetySettingDict(TypedDict, total=False):
50035003
SpeechConfigUnionDict = Union[str, SpeechConfig, SpeechConfigDict]
50045004

50055005

5006+
class ModelArmorConfig(_common.BaseModel):
5007+
"""Configuration for Model Armor integrations of prompt and responses.
5008+
5009+
This data type is not supported in Gemini API.
5010+
"""
5011+
5012+
prompt_template_name: Optional[str] = Field(
5013+
default=None,
5014+
description="""Optional. The name of the Model Armor template to use for prompt sanitization.""",
5015+
)
5016+
response_template_name: Optional[str] = Field(
5017+
default=None,
5018+
description="""Optional. The name of the Model Armor template to use for response sanitization.""",
5019+
)
5020+
5021+
5022+
class ModelArmorConfigDict(TypedDict, total=False):
5023+
"""Configuration for Model Armor integrations of prompt and responses.
5024+
5025+
This data type is not supported in Gemini API.
5026+
"""
5027+
5028+
prompt_template_name: Optional[str]
5029+
"""Optional. The name of the Model Armor template to use for prompt sanitization."""
5030+
5031+
response_template_name: Optional[str]
5032+
"""Optional. The name of the Model Armor template to use for response sanitization."""
5033+
5034+
5035+
ModelArmorConfigOrDict = Union[ModelArmorConfig, ModelArmorConfigDict]
5036+
5037+
50065038
class GenerateContentConfig(_common.BaseModel):
50075039
"""Optional model configuration parameters.
50085040

@@ -5219,6 +5251,12 @@ class GenerateContentConfig(_common.BaseModel):
52195251
models. This field is not supported in Vertex AI.
52205252
""",
52215253
)
5254+
model_armor_config: Optional[ModelArmorConfig] = Field(
5255+
default=None,
5256+
description="""Settings for prompt and response sanitization using the Model Armor
5257+
service. If supplied, safety_settings must not be supplied.
5258+
""",
5259+
)
52225260

52235261
@pydantic.field_validator('response_schema', mode='before')
52245262
@classmethod
@@ -5429,6 +5467,11 @@ class GenerateContentConfigDict(TypedDict, total=False):
54295467
models. This field is not supported in Vertex AI.
54305468
"""
54315469

5470+
model_armor_config: Optional[ModelArmorConfigDict]
5471+
"""Settings for prompt and response sanitization using the Model Armor
5472+
service. If supplied, safety_settings must not be supplied.
5473+
"""
5474+
54325475

54335476
GenerateContentConfigOrDict = Union[
54345477
GenerateContentConfig, GenerateContentConfigDict

0 commit comments

Comments
 (0)