Skip to content

Commit b1f632d

Browse files
yyyu-googlecopybara-github
authored andcommitted
feat: add enable_prompt_injection_detection for Computer Use feature for the Gemini API.
PiperOrigin-RevId: 919308344
1 parent 2910346 commit b1f632d

4 files changed

Lines changed: 133 additions & 3 deletions

File tree

google/genai/_live_converters.py

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,14 @@
2323
from ._common import set_value_by_path as setv
2424

2525

26+
def _Environment_to_vertex_enum_validate(enum_value: Any) -> None:
27+
if enum_value in set(['ENVIRONMENT_MOBILE', 'ENVIRONMENT_DESKTOP']):
28+
raise ValueError(
29+
f'{enum_value} enum value is only supported in Gemini Developer API'
30+
' mode, not in Gemini Enterprise Agent Platform mode.'
31+
)
32+
33+
2634
def _AudioTranscriptionConfig_to_mldev(
2735
from_object: Union[dict[str, Any], object],
2836
parent_object: Optional[dict[str, Any]] = None,
@@ -124,6 +132,32 @@ def _CodeExecutionResult_to_vertex(
124132
return to_object
125133

126134

135+
def _ComputerUse_to_vertex(
136+
from_object: Union[dict[str, Any], object],
137+
parent_object: Optional[dict[str, Any]] = None,
138+
) -> dict[str, Any]:
139+
to_object: dict[str, Any] = {}
140+
if getv(from_object, ['environment']) is not None:
141+
_Environment_to_vertex_enum_validate(getv(from_object, ['environment']))
142+
setv(to_object, ['environment'], getv(from_object, ['environment']))
143+
144+
if getv(from_object, ['excluded_predefined_functions']) is not None:
145+
setv(
146+
to_object,
147+
['excludedPredefinedFunctions'],
148+
getv(from_object, ['excluded_predefined_functions']),
149+
)
150+
151+
if getv(from_object, ['enable_prompt_injection_detection']) is not None:
152+
raise ValueError(
153+
'enable_prompt_injection_detection parameter is only supported in'
154+
' Gemini Developer API mode, not in Gemini Enterprise Agent Platform'
155+
' mode.'
156+
)
157+
158+
return to_object
159+
160+
127161
def _Content_to_mldev(
128162
from_object: Union[dict[str, Any], object],
129163
parent_object: Optional[dict[str, Any]] = None,
@@ -1892,7 +1926,11 @@ def _Tool_to_vertex(
18921926
setv(to_object, ['retrieval'], getv(from_object, ['retrieval']))
18931927

18941928
if getv(from_object, ['computer_use']) is not None:
1895-
setv(to_object, ['computerUse'], getv(from_object, ['computer_use']))
1929+
setv(
1930+
to_object,
1931+
['computerUse'],
1932+
_ComputerUse_to_vertex(getv(from_object, ['computer_use']), to_object),
1933+
)
18961934

18971935
if getv(from_object, ['file_search']) is not None:
18981936
raise ValueError(

google/genai/caches.py

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,14 @@
3232
logger = logging.getLogger('google_genai.caches')
3333

3434

35+
def _Environment_to_vertex_enum_validate(enum_value: Any) -> None:
36+
if enum_value in set(['ENVIRONMENT_MOBILE', 'ENVIRONMENT_DESKTOP']):
37+
raise ValueError(
38+
f'{enum_value} enum value is only supported in Gemini Developer API'
39+
' mode, not in Gemini Enterprise Agent Platform mode.'
40+
)
41+
42+
3543
def _AuthConfig_to_mldev(
3644
from_object: Union[dict[str, Any], object],
3745
parent_object: Optional[dict[str, Any]] = None,
@@ -119,6 +127,32 @@ def _CodeExecutionResult_to_vertex(
119127
return to_object
120128

121129

130+
def _ComputerUse_to_vertex(
131+
from_object: Union[dict[str, Any], object],
132+
parent_object: Optional[dict[str, Any]] = None,
133+
) -> dict[str, Any]:
134+
to_object: dict[str, Any] = {}
135+
if getv(from_object, ['environment']) is not None:
136+
_Environment_to_vertex_enum_validate(getv(from_object, ['environment']))
137+
setv(to_object, ['environment'], getv(from_object, ['environment']))
138+
139+
if getv(from_object, ['excluded_predefined_functions']) is not None:
140+
setv(
141+
to_object,
142+
['excludedPredefinedFunctions'],
143+
getv(from_object, ['excluded_predefined_functions']),
144+
)
145+
146+
if getv(from_object, ['enable_prompt_injection_detection']) is not None:
147+
raise ValueError(
148+
'enable_prompt_injection_detection parameter is only supported in'
149+
' Gemini Developer API mode, not in Gemini Enterprise Agent Platform'
150+
' mode.'
151+
)
152+
153+
return to_object
154+
155+
122156
def _Content_to_mldev(
123157
from_object: Union[dict[str, Any], object],
124158
parent_object: Optional[dict[str, Any]] = None,
@@ -960,7 +994,11 @@ def _Tool_to_vertex(
960994
setv(to_object, ['retrieval'], getv(from_object, ['retrieval']))
961995

962996
if getv(from_object, ['computer_use']) is not None:
963-
setv(to_object, ['computerUse'], getv(from_object, ['computer_use']))
997+
setv(
998+
to_object,
999+
['computerUse'],
1000+
_ComputerUse_to_vertex(getv(from_object, ['computer_use']), to_object),
1001+
)
9641002

9651003
if getv(from_object, ['file_search']) is not None:
9661004
raise ValueError(

google/genai/models.py

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,14 @@
3636
logger = logging.getLogger('google_genai.models')
3737

3838

39+
def _Environment_to_vertex_enum_validate(enum_value: Any) -> None:
40+
if enum_value in set(['ENVIRONMENT_MOBILE', 'ENVIRONMENT_DESKTOP']):
41+
raise ValueError(
42+
f'{enum_value} enum value is only supported in Gemini Developer API'
43+
' mode, not in Gemini Enterprise Agent Platform mode.'
44+
)
45+
46+
3947
def _PersonGeneration_to_mldev_enum_validate(enum_value: Any) -> None:
4048
if enum_value in set(['ALLOW_ALL']):
4149
raise ValueError(
@@ -273,6 +281,33 @@ def _ComputeTokensResponse_from_vertex(
273281
return to_object
274282

275283

284+
def _ComputerUse_to_vertex(
285+
from_object: Union[dict[str, Any], object],
286+
parent_object: Optional[dict[str, Any]] = None,
287+
root_object: Optional[Union[dict[str, Any], object]] = None,
288+
) -> dict[str, Any]:
289+
to_object: dict[str, Any] = {}
290+
if getv(from_object, ['environment']) is not None:
291+
_Environment_to_vertex_enum_validate(getv(from_object, ['environment']))
292+
setv(to_object, ['environment'], getv(from_object, ['environment']))
293+
294+
if getv(from_object, ['excluded_predefined_functions']) is not None:
295+
setv(
296+
to_object,
297+
['excludedPredefinedFunctions'],
298+
getv(from_object, ['excluded_predefined_functions']),
299+
)
300+
301+
if getv(from_object, ['enable_prompt_injection_detection']) is not None:
302+
raise ValueError(
303+
'enable_prompt_injection_detection parameter is only supported in'
304+
' Gemini Developer API mode, not in Gemini Enterprise Agent Platform'
305+
' mode.'
306+
)
307+
308+
return to_object
309+
310+
276311
def _ContentEmbeddingStatistics_from_vertex(
277312
from_object: Union[dict[str, Any], object],
278313
parent_object: Optional[dict[str, Any]] = None,
@@ -4350,7 +4385,13 @@ def _Tool_to_vertex(
43504385
setv(to_object, ['retrieval'], getv(from_object, ['retrieval']))
43514386

43524387
if getv(from_object, ['computer_use']) is not None:
4353-
setv(to_object, ['computerUse'], getv(from_object, ['computer_use']))
4388+
setv(
4389+
to_object,
4390+
['computerUse'],
4391+
_ComputerUse_to_vertex(
4392+
getv(from_object, ['computer_use']), to_object, root_object
4393+
),
4394+
)
43544395

43554396
if getv(from_object, ['file_search']) is not None:
43564397
raise ValueError(

google/genai/types.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,10 @@ class Environment(_common.CaseInSensitiveEnum):
202202
"""Defaults to browser."""
203203
ENVIRONMENT_BROWSER = 'ENVIRONMENT_BROWSER'
204204
"""Operates in a web browser."""
205+
ENVIRONMENT_MOBILE = 'ENVIRONMENT_MOBILE'
206+
"""Operates in a mobile environment."""
207+
ENVIRONMENT_DESKTOP = 'ENVIRONMENT_DESKTOP'
208+
"""Operates in a desktop environment."""
205209

206210

207211
class AuthType(_common.CaseInSensitiveEnum):
@@ -3295,6 +3299,11 @@ class ComputerUse(_common.BaseModel):
32953299
1. Using a more restricted / different action space.
32963300
2. Improving the definitions / instructions of predefined functions.""",
32973301
)
3302+
enable_prompt_injection_detection: Optional[bool] = Field(
3303+
default=None,
3304+
description="""Optional. Whether enable the prompt injection detection check on computer-use request.
3305+
""",
3306+
)
32983307

32993308

33003309
class ComputerUseDict(TypedDict, total=False):
@@ -3310,6 +3319,10 @@ class ComputerUseDict(TypedDict, total=False):
33103319
1. Using a more restricted / different action space.
33113320
2. Improving the definitions / instructions of predefined functions."""
33123321

3322+
enable_prompt_injection_detection: Optional[bool]
3323+
"""Optional. Whether enable the prompt injection detection check on computer-use request.
3324+
"""
3325+
33133326

33143327
ComputerUseOrDict = Union[ComputerUse, ComputerUseDict]
33153328

0 commit comments

Comments
 (0)