Skip to content

Commit e951337

Browse files
qiaodevMarkDaoust
authored andcommitted
feat: support customer-managed encryption key in cached content
PiperOrigin-RevId: 759685739
1 parent f4dd629 commit e951337

File tree

3 files changed

+46
-0
lines changed

3 files changed

+46
-0
lines changed

google/genai/caches.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -518,6 +518,9 @@ def _CreateCachedContentConfig_to_mldev(
518518
),
519519
)
520520

521+
if getv(from_object, ['kms_key_name']) is not None:
522+
raise ValueError('kms_key_name parameter is not supported in Gemini API.')
523+
521524
return to_object
522525

523526

@@ -1171,6 +1174,13 @@ def _CreateCachedContentConfig_to_vertex(
11711174
),
11721175
)
11731176

1177+
if getv(from_object, ['kms_key_name']) is not None:
1178+
setv(
1179+
parent_object,
1180+
['encryption_spec', 'kmsKeyName'],
1181+
getv(from_object, ['kms_key_name']),
1182+
)
1183+
11741184
return to_object
11751185

11761186

google/genai/tests/caches/test_create.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,13 @@
9393
'models/gemini-1.5-pro-001'
9494
)
9595

96+
_CREATE_CACHED_CONTENT_PARAMETERS_GCS_URI_CMEK = deepcopy(
97+
_CREATE_CACHED_CONTENT_PARAMETERS_GCS_URI
98+
)
99+
_CREATE_CACHED_CONTENT_PARAMETERS_GCS_URI_CMEK.config.kms_key_name = (
100+
'projects/test-project/locations/us-central1/keyRings/test-keyring/cryptoKeys/test-key'
101+
)
102+
96103
if sys.version_info >= (3, 11):
97104
_EXPIRE_TIME = datetime.datetime.fromisoformat('2024-12-20T00:00:00Z')
98105
else:
@@ -137,6 +144,12 @@
137144
exception_if_mldev='INVALID_ARGUMENT',
138145
parameters=_CREATE_CACHED_CONTENT_PARAMETERS_GCS_URI,
139146
),
147+
pytest_helper.TestTableItem(
148+
name='test_caches_create_with_gcs_uri_cmek',
149+
exception_if_mldev='not supported',
150+
exception_if_vertex='INVALID_ARGUMENT', # The key is invalid.
151+
parameters=_CREATE_CACHED_CONTENT_PARAMETERS_GCS_URI_CMEK,
152+
),
140153
pytest_helper.TestTableItem(
141154
name='test_caches_create_with_gcs_uri_expire_time',
142155
exception_if_mldev='INVALID_ARGUMENT',

google/genai/types.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8237,6 +8237,18 @@ class CreateCachedContentConfig(_common.BaseModel):
82378237
description="""Configuration for the tools to use. This config is shared for all tools.
82388238
""",
82398239
)
8240+
kms_key_name: Optional[str] = Field(
8241+
default=None,
8242+
description="""The Cloud KMS resource identifier of the customer managed
8243+
encryption key used to protect a resource.
8244+
The key needs to be in the same region as where the compute resource is
8245+
created. See
8246+
https://cloud.google.com/vertex-ai/docs/general/cmek for more
8247+
details. If this is set, then all created CachedContent objects
8248+
will be encrypted with the provided encryption key.
8249+
Allowed formats: projects/{project}/locations/{location}/keyRings/{key_ring}/cryptoKeys/{crypto_key}
8250+
""",
8251+
)
82408252

82418253

82428254
class CreateCachedContentConfigDict(TypedDict, total=False):
@@ -8271,6 +8283,17 @@ class CreateCachedContentConfigDict(TypedDict, total=False):
82718283
"""Configuration for the tools to use. This config is shared for all tools.
82728284
"""
82738285

8286+
kms_key_name: Optional[str]
8287+
"""The Cloud KMS resource identifier of the customer managed
8288+
encryption key used to protect a resource.
8289+
The key needs to be in the same region as where the compute resource is
8290+
created. See
8291+
https://cloud.google.com/vertex-ai/docs/general/cmek for more
8292+
details. If this is set, then all created CachedContent objects
8293+
will be encrypted with the provided encryption key.
8294+
Allowed formats: projects/{project}/locations/{location}/keyRings/{key_ring}/cryptoKeys/{crypto_key}
8295+
"""
8296+
82748297

82758298
CreateCachedContentConfigOrDict = Union[
82768299
CreateCachedContentConfig, CreateCachedContentConfigDict

0 commit comments

Comments
 (0)