-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Expand file tree
/
Copy pathtest_functions_parallel.py
More file actions
190 lines (162 loc) · 5.79 KB
/
test_functions_parallel.py
File metadata and controls
190 lines (162 loc) · 5.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# Copyright 2026 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from google.adk.agents.llm_agent import Agent
from google.adk.events.event import Event
from google.adk.events.event_actions import EventActions
from google.adk.flows.llm_flows.functions import deep_merge_dicts
from google.adk.flows.llm_flows.functions import merge_parallel_function_response_events
from google.adk.tools.tool_context import ToolContext
from google.genai import types
import pytest
from ... import testing_utils
@pytest.mark.asyncio
async def test_parallel_function_calls_with_state_change():
function_calls = [
types.Part.from_function_call(
name='update_session_state',
args={'key': 'test_key1', 'value': 'test_value1'},
),
types.Part.from_function_call(
name='update_session_state',
args={'key': 'test_key2', 'value': 'test_value2'},
),
types.Part.from_function_call(
name='transfer_to_agent', args={'agent_name': 'test_sub_agent'}
),
]
function_responses = [
types.Part.from_function_response(
name='update_session_state', response={'result': None}
),
types.Part.from_function_response(
name='update_session_state', response={'result': None}
),
types.Part.from_function_response(
name='transfer_to_agent', response={'result': None}
),
]
responses: list[types.Content] = [
function_calls,
'response1',
]
function_called = 0
mock_model = testing_utils.MockModel.create(responses=responses)
async def update_session_state(
key: str, value: str, tool_context: ToolContext
) -> None:
nonlocal function_called
function_called += 1
tool_context.state.update({key: value})
return
async def transfer_to_agent(
agent_name: str, tool_context: ToolContext
) -> None:
nonlocal function_called
function_called += 1
tool_context.actions.transfer_to_agent = agent_name
return
test_sub_agent = Agent(
name='test_sub_agent',
)
agent = Agent(
name='root_agent',
model=mock_model,
tools=[update_session_state, transfer_to_agent],
sub_agents=[test_sub_agent],
)
runner = testing_utils.TestInMemoryRunner(agent)
events = await runner.run_async_with_new_session('test')
# Notice that the following assertion only checks the "contents" part of the events.
# The "actions" part will be checked later.
assert testing_utils.simplify_events(events) == [
('root_agent', function_calls),
('root_agent', function_responses),
('test_sub_agent', 'response1'),
]
# Asserts the function calls.
assert function_called == 3
# Asserts the actions in response event.
response_event = events[1]
assert response_event.actions == EventActions(
state_delta={
'test_key1': 'test_value1',
'test_key2': 'test_value2',
},
transfer_to_agent='test_sub_agent',
)
def test_deep_merge_dicts_concatenates_lists():
"""Test that deep_merge_dicts concatenates list values instead of overwriting."""
d1 = {'state_delta': {'items': ['a']}}
d2 = {'state_delta': {'items': ['b']}}
result = deep_merge_dicts(d1, d2)
assert result['state_delta']['items'] == ['a', 'b']
def test_deep_merge_dicts_overwrites_non_list_non_dict():
"""Test that deep_merge_dicts still overwrites scalar values."""
d1 = {'key': 'old'}
d2 = {'key': 'new'}
result = deep_merge_dicts(d1, d2)
assert result['key'] == 'new'
def test_deep_merge_dicts_merges_nested_dicts():
"""Test that deep_merge_dicts recursively merges nested dicts."""
d1 = {'a': {'b': 1, 'c': 2}}
d2 = {'a': {'b': 3, 'd': 4}}
result = deep_merge_dicts(d1, d2)
assert result == {'a': {'b': 3, 'c': 2, 'd': 4}}
def test_deep_merge_dicts_handles_mixed_list_and_non_list():
"""Test that deep_merge_dicts overwrites when types differ (list vs non-list)."""
d1 = {'key': 'not_a_list'}
d2 = {'key': ['a', 'b']}
result = deep_merge_dicts(d1, d2)
assert result['key'] == ['a', 'b']
d1 = {'key': ['a', 'b']}
d2 = {'key': 'not_a_list'}
result = deep_merge_dicts(d1, d2)
assert result['key'] == 'not_a_list'
def test_merge_parallel_function_response_events_merges_state_delta_lists():
"""Test that parallel events with list state_delta values are concatenated, not overwritten."""
invocation_id = 'base_invocation_123'
event1 = Event(
invocation_id=invocation_id,
author='tool',
content=types.Content(
role='user',
parts=[
types.Part(
function_response=types.FunctionResponse(
name='func_1',
response={'result': 'ok'},
)
)
],
),
actions=EventActions(state_delta={'items': ['a']}),
)
event2 = Event(
invocation_id=invocation_id,
author='tool',
content=types.Content(
role='user',
parts=[
types.Part(
function_response=types.FunctionResponse(
name='func_2',
response={'result': 'ok'},
)
)
],
),
actions=EventActions(state_delta={'items': ['b']}),
)
merged_event = merge_parallel_function_response_events([event1, event2])
assert merged_event.actions.state_delta == {'items': ['a', 'b']}