Skip to content

Commit 7916dea

Browse files
Merge branch 'main' into feat-Add-early-exit-mechanism-to-SequentialAgent-using-escalate-action
2 parents 8cb05a5 + 0bdba30 commit 7916dea

File tree

4 files changed

+92
-33
lines changed

4 files changed

+92
-33
lines changed

contributing/samples/oauth_calendar_agent/README.md

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,37 +4,45 @@
44

55
This sample tests and demos the OAuth support in ADK via two tools:
66

7-
* 1. list_calendar_events
7+
* 1. list_calendar_events
88

9-
This is a customized tool that calls Google Calendar API to list calendar events.
10-
It pass in the client id and client secrete to ADK and then get back the access token from ADK.
11-
And then it uses the access token to call calendar api.
9+
This is a customized tool that calls Google Calendar API to list calendar
10+
events. It pass in the client id and client secrete to ADK and then get back
11+
the access token from ADK. And then it uses the access token to call
12+
calendar api.
1213

13-
* 2. get_calendar_events
14+
* 2. get_calendar_events
1415

15-
This is an google calendar tool that calls Google Calendar API to get the details of a specific calendar.
16-
This tool is from the ADK built-in Google Calendar ToolSet.
17-
Everything is wrapped and the tool user just needs to pass in the client id and client secret.
16+
This is an google calendar tool that calls Google Calendar API to get the
17+
details of a specific calendar. This tool is from the ADK built-in Google
18+
Calendar ToolSet. Everything is wrapped and the tool user just needs to pass
19+
in the client id and client secret.
1820

1921
## How to use
2022

21-
* 1. Follow https://developers.google.com/identity/protocols/oauth2#1.-obtain-oauth-2.0-credentials-from-the-dynamic_data.setvar.console_name. to get your client id and client secret.
22-
Be sure to choose "web" as your client type.
23+
* 1. Follow
24+
https://developers.google.com/identity/protocols/oauth2#1.-obtain-oauth-2.0-credentials-from-the-dynamic_data.setvar.console_name.
25+
to get your client id and client secret. Be sure to choose "web" as your
26+
client type.
2327

24-
* 2. Configure your `.env` file to add two variables:
28+
* 2. Configure your `.env` file to add two variables:
2529

26-
* OAUTH_CLIENT_ID={your client id}
27-
* OAUTH_CLIENT_SECRET={your client secret}
30+
* OAUTH_CLIENT_ID={your client id}
31+
* OAUTH_CLIENT_SECRET={your client secret}
2832

29-
Note: don't create a separate `.env` file , instead put it to the same `.env` file that stores your Vertex AI or Dev ML credentials
33+
Note: don't create a separate `.env` file , instead put it to the same
34+
`.env` file that stores your Vertex AI or Dev ML credentials
3035

31-
* 3. Follow https://developers.google.com/identity/protocols/oauth2/web-server#creatingcred to add http://localhost/dev-ui/ to "Authorized redirect URIs".
36+
* 3. Follow
37+
https://developers.google.com/identity/protocols/oauth2/web-server#creatingcred
38+
to add http://localhost/dev-ui/ to "Authorized redirect URIs".
3239

33-
Note: localhost here is just a hostname that you use to access the dev ui, replace it with the actual hostname you use to access the dev ui.
40+
Note: localhost here is just a hostname that you use to access the dev ui,
41+
replace it with the actual hostname you use to access the dev ui.
3442

35-
* 4. For 1st run, allow popup for localhost in Chrome.
43+
* 4. For 1st run, allow popup for localhost in Chrome.
3644

3745
## Sample prompt
3846

39-
* `List all my today's meeting from 7am to 7pm.`
40-
* `Get the details of the first event.`
47+
* `List all my today's meeting from 7am to 7pm.`
48+
* `Get the details of the first event.`
Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,32 @@
11
# Output Schema with Tools Sample Agent
22

3-
This sample demonstrates how to use structured output (`output_schema`) alongside other tools in an ADK agent. Previously, this combination was not allowed, but now it's supported through a special processor that handles the interaction.
3+
This sample demonstrates how to use structured output (`output_schema`)
4+
alongside other tools in an ADK agent. Previously, this combination was not
5+
allowed, but now it's supported through a special processor that handles the
6+
interaction.
47

58
## How it Works
69

710
The agent combines:
8-
- **Tools**: `search_wikipedia` and `get_current_year` for gathering information
9-
- **Structured Output**: `PersonInfo` schema to ensure consistent response format
11+
12+
- **Tools**: `search_wikipedia` and `get_current_year` for gathering
13+
information
14+
- **Structured Output**: `PersonInfo` schema to ensure consistent response
15+
format
1016

1117
When both `output_schema` and `tools` are specified:
12-
1. ADK automatically adds a special `set_model_response` tool
13-
2. The model can use the regular tools for information gathering
14-
3. For the final response, the model uses `set_model_response` with structured data
15-
4. ADK extracts and validates the structured response
18+
19+
1. ADK automatically adds a special `set_model_response` tool
20+
2. The model can use the regular tools for information gathering
21+
3. For the final response, the model uses `set_model_response` with structured
22+
data
23+
4. ADK extracts and validates the structured response
1624

1725
## Expected Response Format
1826

19-
The agent will return information in this structured format for user query "Tell me about Albert Einstein":
27+
The agent will return information in this structured format for user query
28+
29+
> Tell me about Albert Einstein.
2030
2131
```json
2232
{
@@ -30,7 +40,7 @@ The agent will return information in this structured format for user query "Tell
3040

3141
## Key Features Demonstrated
3242

33-
1. **Tool Usage**: Agent can search Wikipedia and get current year
34-
2. **Structured Output**: Response follows strict PersonInfo schema
35-
3. **Validation**: ADK validates the response matches the schema
36-
4. **Flexibility**: Works with any combination of tools and output schemas
43+
1. **Tool Usage**: Agent can search Wikipedia and get current year
44+
2. **Structured Output**: Response follows strict PersonInfo schema
45+
3. **Validation**: ADK validates the response matches the schema
46+
4. **Flexibility**: Works with any combination of tools and output schemas

src/google/adk/sessions/vertex_ai_session_service.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,23 @@ async def create_session(
7373
user_id: str,
7474
state: Optional[dict[str, Any]] = None,
7575
session_id: Optional[str] = None,
76+
**kwargs: Any,
7677
) -> Session:
78+
"""Creates a new session.
79+
80+
Args:
81+
app_name: The name of the application.
82+
user_id: The ID of the user.
83+
state: The initial state of the session.
84+
session_id: The ID of the session.
85+
**kwargs: Additional arguments to pass to the session creation. E.g. set
86+
expire_time='2025-10-01T00:00:00Z' to set the session expiration time.
87+
See https://cloud.google.com/vertex-ai/generative-ai/docs/reference/rest/v1beta1/projects.locations.reasoningEngines.sessions
88+
for more details.
89+
Returns:
90+
The created session.
91+
"""
92+
7793
if session_id:
7894
raise ValueError(
7995
'User-provided Session id is not supported for'
@@ -84,6 +100,7 @@ async def create_session(
84100
api_client = self._get_api_client()
85101

86102
config = {'session_state': state} if state else {}
103+
config.update(kwargs)
87104

88105
if _is_vertex_express_mode(self._project, self._location):
89106
config['wait_for_completion'] = False

tests/unittests/sessions/test_vertex_ai_session_service.py

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,7 @@ def __init__(self) -> None:
242242
self.agent_engines.sessions.create.side_effect = self._create_session
243243
self.agent_engines.sessions.events.list.side_effect = self._list_events
244244
self.agent_engines.sessions.events.append.side_effect = self._append_event
245+
self.last_create_session_config: dict[str, Any] = {}
245246

246247
def _get_session(self, name: str):
247248
session_id = name.split('/')[-1]
@@ -275,6 +276,7 @@ def _delete_session(self, name: str):
275276
self.session_dict.pop(session_id)
276277

277278
def _create_session(self, name: str, user_id: str, config: dict[str, Any]):
279+
self.last_create_session_config = config
278280
new_session_id = '4'
279281
self.session_dict[new_session_id] = {
280282
'name': (
@@ -360,7 +362,8 @@ def mock_vertex_ai_session_service(agent_engine_id: Optional[str] = None):
360362

361363

362364
@pytest.fixture
363-
def mock_get_api_client():
365+
def mock_api_client_instance():
366+
"""Creates a mock API client instance for testing."""
364367
api_client = MockApiClient()
365368
api_client.session_dict = {
366369
'1': MOCK_SESSION_JSON_1,
@@ -373,9 +376,15 @@ def mock_get_api_client():
373376
'1': (copy.deepcopy(MOCK_EVENT_JSON), None),
374377
'2': (copy.deepcopy(MOCK_EVENT_JSON_2), 'my_token'),
375378
}
379+
return api_client
380+
381+
382+
@pytest.fixture
383+
def mock_get_api_client(mock_api_client_instance):
384+
"""Mocks the _get_api_client method to return a mock API client."""
376385
with mock.patch(
377386
'google.adk.sessions.vertex_ai_session_service.VertexAiSessionService._get_api_client',
378-
return_value=api_client,
387+
return_value=mock_api_client_instance,
379388
):
380389
yield
381390

@@ -521,6 +530,21 @@ async def test_create_session_with_custom_session_id():
521530
)
522531

523532

533+
@pytest.mark.asyncio
534+
@pytest.mark.usefixtures('mock_get_api_client')
535+
async def test_create_session_with_custom_config(mock_api_client_instance):
536+
session_service = mock_vertex_ai_session_service()
537+
538+
expire_time = '2025-12-12T12:12:12.123456Z'
539+
await session_service.create_session(
540+
app_name='123', user_id='user', expire_time=expire_time
541+
)
542+
assert (
543+
mock_api_client_instance.last_create_session_config['expire_time']
544+
== expire_time
545+
)
546+
547+
524548
@pytest.mark.asyncio
525549
@pytest.mark.usefixtures('mock_get_api_client')
526550
async def test_append_event():

0 commit comments

Comments
 (0)