Skip to content

Commit 45c1fcc

Browse files
google-genai-botcopybara-github
authored andcommitted
feat: disallow setting non-existent properties in BigQuery tools and credentials config
This will save the agent builder getting wrong impression if by mistake they set a property that does not exist. PiperOrigin-RevId: 803208559
1 parent ebf2c98 commit 45c1fcc

4 files changed

Lines changed: 25 additions & 4 deletions

File tree

src/google/adk/tools/_google_credentials.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
from google.auth.transport.requests import Request
2727
import google.oauth2.credentials
2828
from pydantic import BaseModel
29+
from pydantic import ConfigDict
2930
from pydantic import model_validator
3031

3132
from ..auth.auth_credential import AuthCredential
@@ -44,8 +45,7 @@ class BaseGoogleCredentialsConfig(BaseModel):
4445
"""
4546

4647
# Configure the model to allow arbitrary types like Credentials
47-
model_config = {"arbitrary_types_allowed": True}
48-
48+
model_config = ConfigDict(arbitrary_types_allowed=True, extra="forbid")
4949
credentials: Optional[google.auth.credentials.Credentials] = None
5050
"""The existing auth credentials to use. If set, this credential will be used
5151
for every end user, end users don't need to be involved in the oauthflow. This

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
from typing import Optional
1919

2020
from pydantic import BaseModel
21+
from pydantic import ConfigDict
2122
from pydantic import field_validator
2223

2324
from ...utils.feature_decorator import experimental
@@ -50,6 +51,9 @@ class WriteMode(Enum):
5051
class BigQueryToolConfig(BaseModel):
5152
"""Configuration for BigQuery tools."""
5253

54+
# Forbid any fields not defined in the model
55+
model_config = ConfigDict(extra='forbid')
56+
5357
write_mode: WriteMode = WriteMode.BLOCKED
5458
"""Write mode for BigQuery tools.
5559

tests/unittests/tools/bigquery/test_bigquery_credentials.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
from unittest import mock
1616

17-
from google.adk.tools.bigquery.bigquery_credentials import BigQueryCredentialsConfig
17+
from google.adk.tools.bigquery import BigQueryCredentialsConfig
1818
# Mock the Google OAuth and API dependencies
1919
import google.auth.credentials
2020
import google.oauth2.credentials
@@ -171,3 +171,12 @@ def test_empty_configuration_raises_error(self):
171171
),
172172
):
173173
BigQueryCredentialsConfig()
174+
175+
def test_invalid_property_raises_error(self):
176+
"""Test BigQueryCredentialsConfig raises exception when setting invalid property."""
177+
with pytest.raises(ValueError):
178+
BigQueryCredentialsConfig(
179+
client_id="test_client_id",
180+
client_secret="test_client_secret",
181+
non_existent_field="some value",
182+
)

tests/unittests/tools/bigquery/test_bigquery_tool_config.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,16 @@ def test_bigquery_tool_config_experimental_warning():
2727
BigQueryToolConfig()
2828

2929

30+
def test_bigquery_tool_config_invalid_property():
31+
"""Test BigQueryToolConfig raises exception when setting invalid property."""
32+
with pytest.raises(
33+
ValueError,
34+
):
35+
BigQueryToolConfig(non_existent_field="some value")
36+
37+
3038
def test_bigquery_tool_config_invalid_application_name():
31-
"""Test BigQueryToolConfig with invalid application name."""
39+
"""Test BigQueryToolConfig raises exception with invalid application name."""
3240
with pytest.raises(
3341
ValueError,
3442
match="Application name should not contain spaces.",

0 commit comments

Comments
 (0)