Skip to content

Commit 82fa10b

Browse files
google-genai-botcopybara-github
authored andcommitted
feat: add new conversational analytics api tool set
PiperOrigin-RevId: 859449435
1 parent bf2b56d commit 82fa10b

File tree

11 files changed

+1174
-0
lines changed

11 files changed

+1174
-0
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# Data Agent Sample
2+
3+
This sample agent demonstrates ADK's first-party tools for interacting with
4+
Data Agents powered by [Conversational Analytics API](https://docs.cloud.google.com/gemini/docs/conversational-analytics-api/overview).
5+
These tools are distributed via
6+
the `google.adk.tools.data_agent` module and allow you to list,
7+
inspect, and
8+
chat with Data Agents using natural language.
9+
10+
These tools leverage stateful conversations, meaning you can ask follow-up
11+
questions in the same session, and the agent will maintain context.
12+
13+
## Prerequisites
14+
15+
1. An active Google Cloud project with BigQuery and Gemini APIs enabled.
16+
2. Google Cloud authentication configured for Application Default Credentials:
17+
```bash
18+
gcloud auth application-default login
19+
```
20+
3. At least one Data Agent created. You could create data agents via
21+
[Conversational API](https://docs.cloud.google.com/gemini/docs/conversational-analytics-api/overview),
22+
its
23+
[Python SDK](https://docs.cloud.google.com/gemini/docs/conversational-analytics-api/build-agent-sdk),
24+
or for BigQuery data
25+
[BigQuery Studio](https://docs.cloud.google.com/bigquery/docs/create-data-agents#create_a_data_agent).
26+
These agents are created and configured in the Google Cloud console and
27+
point to your BigQuery tables or other data sources.
28+
4. Follow the official
29+
[Setup and prerequisites](https://docs.cloud.google.com/gemini/docs/conversational-analytics-api/overview#setup)
30+
guide to enable the API and configure IAM permissions and authentication for
31+
your data sources.
32+
33+
## Tools Used
34+
35+
* `list_accessible_data_agents`: Lists Data Agents you have permission to
36+
access in the configured GCP project.
37+
* `get_data_agent_info`: Retrieves details about a specific Data Agent given
38+
its full resource name.
39+
* `ask_data_agent`: Chats with a specific Data Agent using natural language.
40+
This tool maintains conversation state: if you ask multiple
41+
questions to the same agent in one session, it will use the same
42+
conversation, allowing for follow-ups. If you switch agents, a new
43+
conversation will be started for the new agent.
44+
45+
## How to Run
46+
47+
1. Navigate to the root of the ADK repository.
48+
2. Run the agent using the ADK CLI:
49+
```bash
50+
adk run --agent-path contributing/samples/data_agent
51+
```
52+
3. The CLI will prompt you for input. You can ask questions like the examples
53+
below.
54+
55+
## Sample prompts
56+
57+
* "List accessible data agents."
58+
* "Using agent
59+
`projects/my-project/locations/global/dataAgents/sales-agent-123`, who were
60+
my top 3 customers last quarter?"
61+
* "How does that compare to the quarter before?"
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Copyright 2026 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from . import agent
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
# Copyright 2026 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import os
16+
17+
from google.adk.agents import Agent
18+
from google.adk.auth.auth_credential import AuthCredentialTypes
19+
from google.adk.tools.data_agent.config import DataAgentToolConfig
20+
from google.adk.tools.data_agent.credentials import DataAgentCredentialsConfig
21+
from google.adk.tools.data_agent.data_agent_toolset import DataAgentToolset
22+
import google.auth
23+
import google.auth.transport.requests
24+
25+
# Define the desired credential type.
26+
# By default use Application Default Credentials (ADC) from the local
27+
# environment, which can be set up by following
28+
# https://cloud.google.com/docs/authentication/provide-credentials-adc.
29+
CREDENTIALS_TYPE = None
30+
31+
if CREDENTIALS_TYPE == AuthCredentialTypes.OAUTH2:
32+
# Initiaze the tools to do interactive OAuth
33+
# The environment variables OAUTH_CLIENT_ID and OAUTH_CLIENT_SECRET
34+
# must be set
35+
credentials_config = DataAgentCredentialsConfig(
36+
client_id=os.getenv("OAUTH_CLIENT_ID"),
37+
client_secret=os.getenv("OAUTH_CLIENT_SECRET"),
38+
)
39+
elif CREDENTIALS_TYPE == AuthCredentialTypes.SERVICE_ACCOUNT:
40+
# Initialize the tools to use the credentials in the service account key.
41+
# If this flow is enabled, make sure to replace the file path with your own
42+
# service account key file
43+
# https://cloud.google.com/iam/docs/service-account-creds#user-managed-keys
44+
creds, _ = google.auth.load_credentials_from_file(
45+
"service_account_key.json",
46+
scopes=["https://www.googleapis.com/auth/cloud-platform"],
47+
)
48+
creds.refresh(google.auth.transport.requests.Request())
49+
credentials_config = DataAgentCredentialsConfig(credentials=creds)
50+
else:
51+
# Initialize the tools to use the application default credentials.
52+
# https://cloud.google.com/docs/authentication/provide-credentials-adc
53+
application_default_credentials, _ = google.auth.default()
54+
credentials_config = DataAgentCredentialsConfig(
55+
credentials=application_default_credentials
56+
)
57+
58+
tool_config = DataAgentToolConfig(
59+
max_query_result_rows=100,
60+
)
61+
da_toolset = DataAgentToolset(
62+
credentials_config=credentials_config,
63+
data_agent_tool_config=tool_config,
64+
tool_filter=[
65+
"list_accessible_data_agents",
66+
"get_data_agent_info",
67+
"ask_data_agent",
68+
],
69+
)
70+
71+
root_agent = Agent(
72+
name="data_agent",
73+
model="gemini-2.0-flash",
74+
description="Agent to answer user questions using Data Agents.",
75+
instruction=(
76+
"## Persona\nYou are a helpful assistant that uses Data Agents"
77+
" to answer user questions about their data.\n\n## Tools\n- You can"
78+
" list available data agents using `list_accessible_data_agents`.\n-"
79+
" You can get information about a specific data agent using"
80+
" `get_data_agent_info`.\n- You can chat with a specific data"
81+
" agent using `ask_data_agent`.\n"
82+
),
83+
tools=[da_toolset],
84+
)

src/google/adk/features/_feature_registry.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ class FeatureName(str, Enum):
3333
BIGTABLE_TOOL_SETTINGS = "BIGTABLE_TOOL_SETTINGS"
3434
BIGTABLE_TOOLSET = "BIGTABLE_TOOLSET"
3535
COMPUTER_USE = "COMPUTER_USE"
36+
DATA_AGENT_TOOL_CONFIG = "DATA_AGENT_TOOL_CONFIG"
37+
DATA_AGENT_TOOLSET = "DATA_AGENT_TOOLSET"
3638
GOOGLE_CREDENTIALS_CONFIG = "GOOGLE_CREDENTIALS_CONFIG"
3739
GOOGLE_TOOL = "GOOGLE_TOOL"
3840
JSON_SCHEMA_FOR_FUNC_DECL = "JSON_SCHEMA_FOR_FUNC_DECL"
@@ -97,6 +99,12 @@ class FeatureConfig:
9799
FeatureName.COMPUTER_USE: FeatureConfig(
98100
FeatureStage.EXPERIMENTAL, default_on=True
99101
),
102+
FeatureName.DATA_AGENT_TOOL_CONFIG: FeatureConfig(
103+
FeatureStage.EXPERIMENTAL, default_on=True
104+
),
105+
FeatureName.DATA_AGENT_TOOLSET: FeatureConfig(
106+
FeatureStage.EXPERIMENTAL, default_on=True
107+
),
100108
FeatureName.GOOGLE_CREDENTIALS_CONFIG: FeatureConfig(
101109
FeatureStage.EXPERIMENTAL, default_on=True
102110
),
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Copyright 2026 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
"""Data Agent Tools."""
16+
17+
from __future__ import annotations
18+
19+
from .credentials import DataAgentCredentialsConfig
20+
from .data_agent_toolset import DataAgentToolset
21+
22+
__all__ = [
23+
"DataAgentCredentialsConfig",
24+
"DataAgentToolset",
25+
]
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Copyright 2026 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from __future__ import annotations
16+
17+
from pydantic import BaseModel
18+
from pydantic import ConfigDict
19+
20+
from ...features import experimental
21+
from ...features import FeatureName
22+
23+
24+
@experimental(FeatureName.DATA_AGENT_TOOL_CONFIG)
25+
class DataAgentToolConfig(BaseModel):
26+
"""Configuration for Data Agent tools."""
27+
28+
# Forbid any fields not defined in the model
29+
model_config = ConfigDict(extra='forbid')
30+
31+
max_query_result_rows: int = 50
32+
"""Maximum number of rows to return from a query.
33+
34+
By default, the query result will be limited to 50 rows.
35+
"""
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Copyright 2026 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from __future__ import annotations
16+
17+
from .._google_credentials import BaseGoogleCredentialsConfig
18+
19+
DATA_AGENT_TOKEN_CACHE_KEY = "data_agent_token_cache"
20+
DATA_AGENT_DEFAULT_SCOPE = ["https://www.googleapis.com/auth/bigquery"]
21+
22+
23+
class DataAgentCredentialsConfig(BaseGoogleCredentialsConfig):
24+
"""Data Agent Credentials Configuration for Google API tools."""
25+
26+
def __post_init__(self) -> DataAgentCredentialsConfig:
27+
"""Populate default scope if scopes is None."""
28+
super().__post_init__()
29+
30+
if not self.scopes:
31+
self.scopes = DATA_AGENT_DEFAULT_SCOPE
32+
33+
# Set the token cache key
34+
self._token_cache_key = DATA_AGENT_TOKEN_CACHE_KEY
35+
36+
return self

0 commit comments

Comments
 (0)