Skip to content

Commit 8c4ff74

Browse files
google-genai-botcopybara-github
authored andcommitted
feat: limit number of user-provided BigQuery job labels and reserve internal prefixes
PiperOrigin-RevId: 879973232
1 parent 327b3af commit 8c4ff74

2 files changed

Lines changed: 29 additions & 3 deletions

File tree

src/google/adk/tools/bigquery/config.py

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,12 @@ class BigQueryToolConfig(BaseModel):
8282
By default, no particular application name will be set in the BigQuery
8383
interaction. But if the tool user (agent builder) wants to differentiate
8484
their application/agent for tracking or support purpose, they can set this
85-
field. If set, this value will be added to the user_agent in BigQuery API calls, and also to the BigQuery job labels with the key
85+
field. If set, this value will be added to the user_agent in BigQuery API
86+
calls, and also to the BigQuery job labels with the key
8687
"adk-bigquery-application-name".
88+
89+
Note: This field is for usage discovery and tracking purposes only and should
90+
not be used for security-sensitive decisions.
8791
"""
8892

8993
compute_project_id: Optional[str] = None
@@ -108,8 +112,13 @@ class BigQueryToolConfig(BaseModel):
108112
These labels will be added to all BigQuery jobs executed by the tools.
109113
Labels must be key-value pairs where both keys and values are strings.
110114
Labels can be used for billing, monitoring, and resource organization.
111-
For more information about labels, see
115+
For more information about labels, see
112116
https://cloud.google.com/bigquery/docs/labels-intro.
117+
118+
Note: These labels are for usage discovery and tracking purposes only and
119+
should not be used for security-sensitive decisions. The number of
120+
user-provided labels is restricted to 20, and keys starting with
121+
"adk-bigquery-" are reserved for internal usage.
113122
"""
114123

115124
@field_validator('maximum_bytes_billed')
@@ -136,9 +145,16 @@ def validate_application_name(cls, v):
136145
@field_validator('job_labels')
137146
@classmethod
138147
def validate_job_labels(cls, v):
139-
"""Validate that job_labels keys are not empty."""
148+
"""Validate the job labels."""
140149
if v is not None:
150+
if len(v) > 20:
151+
raise ValueError('Only up to 20 job labels can be provided')
141152
for key in v.keys():
142153
if not key:
143154
raise ValueError('Label keys cannot be empty.')
155+
if key.startswith('adk-bigquery-'):
156+
raise ValueError(
157+
'Label key cannot start with "adk-bigquery-" as it is'
158+
f' reserved for internal usage, found "{key}".'
159+
)
144160
return v

tests/unittests/tools/bigquery/test_bigquery_tool_config.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,16 @@ def test_bigquery_tool_config_valid_labels(labels):
130130
"Label keys cannot be empty",
131131
id="empty-label-key",
132132
),
133+
pytest.param(
134+
{"adk-bigquery-test": "value"},
135+
'Label key cannot start with "adk-bigquery-"',
136+
id="internal-label-key",
137+
),
138+
pytest.param(
139+
{f"key_{i}": "value" for i in range(21)},
140+
"Only up to 20 job labels can be provided",
141+
id="too-many-labels",
142+
),
133143
],
134144
)
135145
def test_bigquery_tool_config_invalid_labels(labels, message):

0 commit comments

Comments
 (0)