Repository navigation
Expand file tree
/
Copy pathtest_agent_tool.py
More file actions
704 lines (565 loc) · 19.4 KB
/
Copy pathtest_agent_tool.py
File metadata and controls
704 lines (565 loc) · 19.4 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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
# Copyright 2025 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 typing import Any
from typing import Optional
from google.adk.agents.callback_context import CallbackContext
from google.adk.agents.invocation_context import InvocationContext
from google.adk.agents.llm_agent import Agent
from google.adk.agents.run_config import RunConfig
from google.adk.agents.sequential_agent import SequentialAgent
from google.adk.artifacts.in_memory_artifact_service import InMemoryArtifactService
from google.adk.memory.in_memory_memory_service import InMemoryMemoryService
from google.adk.models.llm_request import LlmRequest
from google.adk.models.llm_response import LlmResponse
from google.adk.plugins.base_plugin import BasePlugin
from google.adk.plugins.plugin_manager import PluginManager
from google.adk.sessions.in_memory_session_service import InMemorySessionService
from google.adk.tools.agent_tool import AgentTool
from google.adk.tools.tool_context import ToolContext
from google.adk.utils.variant_utils import GoogleLLMVariant
from google.genai import types
from google.genai.types import Part
from pydantic import BaseModel
from pytest import mark
from .. import testing_utils
function_call_custom = Part.from_function_call(
name='tool_agent', args={'custom_input': 'test1'}
)
function_call_no_schema = Part.from_function_call(
name='tool_agent', args={'request': 'test1'}
)
function_response_custom = Part.from_function_response(
name='tool_agent', response={'custom_output': 'response1'}
)
function_response_no_schema = Part.from_function_response(
name='tool_agent', response={'result': 'response1'}
)
def change_state_callback(callback_context: CallbackContext):
callback_context.state['state_1'] = 'changed_value'
print('change_state_callback: ', callback_context.state)
@mark.asyncio
async def test_agent_tool_inherits_parent_app_name(monkeypatch):
parent_app_name = 'parent_app'
captured: dict[str, str] = {}
class RecordingSessionService(InMemorySessionService):
async def create_session(
self,
*,
app_name: str,
user_id: str,
state: Optional[dict[str, Any]] = None,
session_id: Optional[str] = None,
):
captured['session_app_name'] = app_name
return await super().create_session(
app_name=app_name,
user_id=user_id,
state=state,
session_id=session_id,
)
monkeypatch.setattr(
'google.adk.sessions.in_memory_session_service.InMemorySessionService',
RecordingSessionService,
)
async def _empty_async_generator():
if False:
yield None
class StubRunner:
def __init__(
self,
*,
app_name: str,
agent: Agent,
artifact_service,
session_service,
memory_service,
credential_service,
plugins,
):
del artifact_service, memory_service, credential_service
captured['runner_app_name'] = app_name
self.agent = agent
self.session_service = session_service
self.plugin_manager = PluginManager(plugins=plugins)
self.app_name = app_name
def run_async(
self,
*,
user_id: str,
session_id: str,
invocation_id: Optional[str] = None,
new_message: Optional[types.Content] = None,
state_delta: Optional[dict[str, Any]] = None,
run_config: Optional[RunConfig] = None,
):
del (
user_id,
session_id,
invocation_id,
new_message,
state_delta,
run_config,
)
return _empty_async_generator()
async def close(self):
"""Mock close method."""
pass
monkeypatch.setattr('google.adk.runners.Runner', StubRunner)
tool_agent = Agent(
name='tool_agent',
model='test-model',
)
agent_tool = AgentTool(agent=tool_agent)
root_agent = Agent(
name='root_agent',
model='test-model',
tools=[agent_tool],
)
artifact_service = InMemoryArtifactService()
parent_session_service = InMemorySessionService()
parent_session = await parent_session_service.create_session(
app_name=parent_app_name,
user_id='user',
)
invocation_context = InvocationContext(
artifact_service=artifact_service,
session_service=parent_session_service,
memory_service=InMemoryMemoryService(),
plugin_manager=PluginManager(),
invocation_id='invocation-id',
agent=root_agent,
session=parent_session,
run_config=RunConfig(),
)
tool_context = ToolContext(invocation_context)
assert tool_context._invocation_context.app_name == parent_app_name
await agent_tool.run_async(
args={'request': 'hello'},
tool_context=tool_context,
)
assert captured['runner_app_name'] == parent_app_name
assert captured['session_app_name'] == parent_app_name
def test_no_schema():
mock_model = testing_utils.MockModel.create(
responses=[
function_call_no_schema,
'response1',
'response2',
]
)
tool_agent = Agent(
name='tool_agent',
model=mock_model,
)
root_agent = Agent(
name='root_agent',
model=mock_model,
tools=[AgentTool(agent=tool_agent)],
)
runner = testing_utils.InMemoryRunner(root_agent)
assert testing_utils.simplify_events(runner.run('test1')) == [
('root_agent', function_call_no_schema),
('root_agent', function_response_no_schema),
('root_agent', 'response2'),
]
def test_use_plugins():
"""The agent tool can use plugins from parent runner."""
class ModelResponseCapturePlugin(BasePlugin):
def __init__(self):
super().__init__('plugin')
self.model_responses = {}
async def after_model_callback(
self,
*,
callback_context: CallbackContext,
llm_response: LlmResponse,
) -> Optional[LlmResponse]:
response_text = []
for part in llm_response.content.parts:
if not part.text:
continue
response_text.append(part.text)
if response_text:
if callback_context.agent_name not in self.model_responses:
self.model_responses[callback_context.agent_name] = []
self.model_responses[callback_context.agent_name].append(
''.join(response_text)
)
mock_model = testing_utils.MockModel.create(
responses=[
function_call_no_schema,
'response1',
'response2',
]
)
tool_agent = Agent(
name='tool_agent',
model=mock_model,
)
root_agent = Agent(
name='root_agent',
model=mock_model,
tools=[AgentTool(agent=tool_agent)],
)
model_response_capture = ModelResponseCapturePlugin()
runner = testing_utils.InMemoryRunner(
root_agent, plugins=[model_response_capture]
)
assert testing_utils.simplify_events(runner.run('test1')) == [
('root_agent', function_call_no_schema),
('root_agent', function_response_no_schema),
('root_agent', 'response2'),
]
# should be able to capture response from both root and tool agent.
assert model_response_capture.model_responses == {
'tool_agent': ['response1'],
'root_agent': ['response2'],
}
def test_update_state():
"""The agent tool can read and change parent state."""
mock_model = testing_utils.MockModel.create(
responses=[
function_call_no_schema,
'{"custom_output": "response1"}',
'response2',
]
)
tool_agent = Agent(
name='tool_agent',
model=mock_model,
instruction='input: {state_1}',
before_agent_callback=change_state_callback,
)
root_agent = Agent(
name='root_agent',
model=mock_model,
tools=[AgentTool(agent=tool_agent)],
)
runner = testing_utils.InMemoryRunner(root_agent)
runner.session.state['state_1'] = 'state1_value'
runner.run('test1')
assert (
'input: changed_value' in mock_model.requests[1].config.system_instruction
)
assert runner.session.state['state_1'] == 'changed_value'
@mark.asyncio
async def test_update_artifacts():
"""The agent tool can read and write artifacts."""
async def before_tool_agent(callback_context: CallbackContext):
# Artifact 1 should be available in the tool agent.
artifact = await callback_context.load_artifact('artifact_1')
await callback_context.save_artifact(
'artifact_2', Part.from_text(text=artifact.text + ' 2')
)
tool_agent = SequentialAgent(
name='tool_agent',
before_agent_callback=before_tool_agent,
)
async def before_main_agent(callback_context: CallbackContext):
await callback_context.save_artifact(
'artifact_1', Part.from_text(text='test')
)
async def after_main_agent(callback_context: CallbackContext):
# Artifact 2 should be available after the tool agent.
artifact_2 = await callback_context.load_artifact('artifact_2')
await callback_context.save_artifact(
'artifact_3', Part.from_text(text=artifact_2.text + ' 3')
)
mock_model = testing_utils.MockModel.create(
responses=[function_call_no_schema, 'response2']
)
root_agent = Agent(
name='root_agent',
before_agent_callback=before_main_agent,
after_agent_callback=after_main_agent,
tools=[AgentTool(agent=tool_agent)],
model=mock_model,
)
runner = testing_utils.InMemoryRunner(root_agent)
runner.run('test1')
async def load_artifact(filename: str):
return await runner.runner.artifact_service.load_artifact(
app_name='test_app',
user_id='test_user',
session_id=runner.session_id,
filename=filename,
)
assert await runner.runner.artifact_service.list_artifact_keys(
app_name='test_app', user_id='test_user', session_id=runner.session_id
) == ['artifact_1', 'artifact_2', 'artifact_3']
assert await load_artifact('artifact_1') == Part.from_text(text='test')
assert await load_artifact('artifact_2') == Part.from_text(text='test 2')
assert await load_artifact('artifact_3') == Part.from_text(text='test 2 3')
@mark.parametrize(
'env_variables',
[
'GOOGLE_AI',
# TODO(wanyif): re-enable after fix.
# 'VERTEX',
],
indirect=True,
)
def test_custom_schema(env_variables):
class CustomInput(BaseModel):
custom_input: str
class CustomOutput(BaseModel):
custom_output: str
mock_model = testing_utils.MockModel.create(
responses=[
function_call_custom,
'{"custom_output": "response1"}',
'response2',
]
)
tool_agent = Agent(
name='tool_agent',
model=mock_model,
input_schema=CustomInput,
output_schema=CustomOutput,
output_key='tool_output',
)
root_agent = Agent(
name='root_agent',
model=mock_model,
tools=[AgentTool(agent=tool_agent)],
)
runner = testing_utils.InMemoryRunner(root_agent)
runner.session.state['state_1'] = 'state1_value'
assert testing_utils.simplify_events(runner.run('test1')) == [
('root_agent', function_call_custom),
('root_agent', function_response_custom),
('root_agent', 'response2'),
]
assert runner.session.state['tool_output'] == {'custom_output': 'response1'}
assert len(mock_model.requests) == 3
# The second request is the tool agent request.
assert mock_model.requests[1].config.response_schema == CustomOutput
assert mock_model.requests[1].config.response_mime_type == 'application/json'
@mark.parametrize(
'env_variables',
[
'VERTEX', # Test VERTEX_AI variant
],
indirect=True,
)
def test_agent_tool_response_schema_no_output_schema_vertex_ai(
env_variables,
):
"""Test AgentTool with no output schema has string response schema for VERTEX_AI."""
tool_agent = Agent(
name='tool_agent',
model=testing_utils.MockModel.create(responses=['test response']),
)
agent_tool = AgentTool(agent=tool_agent)
declaration = agent_tool._get_declaration()
assert declaration.name == 'tool_agent'
assert declaration.parameters.type == 'OBJECT'
assert declaration.parameters.properties['request'].type == 'STRING'
# Should have string response schema for VERTEX_AI
assert declaration.response is not None
assert declaration.response.type == types.Type.STRING
@mark.parametrize(
'env_variables',
[
'VERTEX', # Test VERTEX_AI variant
],
indirect=True,
)
def test_agent_tool_response_schema_with_output_schema_vertex_ai(
env_variables,
):
"""Test AgentTool with output schema has object response schema for VERTEX_AI."""
class CustomOutput(BaseModel):
custom_output: str
tool_agent = Agent(
name='tool_agent',
model=testing_utils.MockModel.create(responses=['test response']),
output_schema=CustomOutput,
)
agent_tool = AgentTool(agent=tool_agent)
declaration = agent_tool._get_declaration()
assert declaration.name == 'tool_agent'
# Should have object response schema for VERTEX_AI when output_schema exists
assert declaration.response is not None
assert declaration.response.type == types.Type.OBJECT
@mark.parametrize(
'env_variables',
[
'GOOGLE_AI', # Test GEMINI_API variant
],
indirect=True,
)
def test_agent_tool_response_schema_gemini_api(
env_variables,
):
"""Test AgentTool with GEMINI_API variant has no response schema."""
class CustomOutput(BaseModel):
custom_output: str
tool_agent = Agent(
name='tool_agent',
model=testing_utils.MockModel.create(responses=['test response']),
output_schema=CustomOutput,
)
agent_tool = AgentTool(agent=tool_agent)
declaration = agent_tool._get_declaration()
assert declaration.name == 'tool_agent'
# GEMINI_API should not have response schema
assert declaration.response is None
@mark.parametrize(
'env_variables',
[
'VERTEX', # Test VERTEX_AI variant
],
indirect=True,
)
def test_agent_tool_response_schema_with_input_schema_vertex_ai(
env_variables,
):
"""Test AgentTool with input and output schemas for VERTEX_AI."""
class CustomInput(BaseModel):
custom_input: str
class CustomOutput(BaseModel):
custom_output: str
tool_agent = Agent(
name='tool_agent',
model=testing_utils.MockModel.create(responses=['test response']),
input_schema=CustomInput,
output_schema=CustomOutput,
)
agent_tool = AgentTool(agent=tool_agent)
declaration = agent_tool._get_declaration()
assert declaration.name == 'tool_agent'
assert declaration.parameters.type == 'OBJECT'
assert declaration.parameters.properties['custom_input'].type == 'STRING'
# Should have object response schema for VERTEX_AI when output_schema exists
assert declaration.response is not None
assert declaration.response.type == types.Type.OBJECT
@mark.parametrize(
'env_variables',
[
'VERTEX', # Test VERTEX_AI variant
],
indirect=True,
)
def test_agent_tool_response_schema_with_input_schema_no_output_vertex_ai(
env_variables,
):
"""Test AgentTool with input schema but no output schema for VERTEX_AI."""
class CustomInput(BaseModel):
custom_input: str
tool_agent = Agent(
name='tool_agent',
model=testing_utils.MockModel.create(responses=['test response']),
input_schema=CustomInput,
)
agent_tool = AgentTool(agent=tool_agent)
declaration = agent_tool._get_declaration()
assert declaration.name == 'tool_agent'
assert declaration.parameters.type == 'OBJECT'
assert declaration.parameters.properties['custom_input'].type == 'STRING'
# Should have string response schema for VERTEX_AI when no output_schema
assert declaration.response is not None
assert declaration.response.type == types.Type.STRING
def test_include_plugins_default_true():
"""Test that plugins are propagated by default (include_plugins=True)."""
# Create a test plugin that tracks callbacks
class TrackingPlugin(BasePlugin):
def __init__(self, name: str):
super().__init__(name)
self.before_agent_calls = 0
async def before_agent_callback(self, **kwargs):
self.before_agent_calls += 1
tracking_plugin = TrackingPlugin(name='tracking')
mock_model = testing_utils.MockModel.create(
responses=[function_call_no_schema, 'response1', 'response2']
)
tool_agent = Agent(
name='tool_agent',
model=mock_model,
)
root_agent = Agent(
name='root_agent',
model=mock_model,
tools=[AgentTool(agent=tool_agent)], # Default include_plugins=True
)
runner = testing_utils.InMemoryRunner(root_agent, plugins=[tracking_plugin])
runner.run('test1')
# Plugin should be called for both root_agent and tool_agent
assert tracking_plugin.before_agent_calls == 2
def test_include_plugins_explicit_true():
"""Test that plugins are propagated when include_plugins=True."""
class TrackingPlugin(BasePlugin):
def __init__(self, name: str):
super().__init__(name)
self.before_agent_calls = 0
async def before_agent_callback(self, **kwargs):
self.before_agent_calls += 1
tracking_plugin = TrackingPlugin(name='tracking')
mock_model = testing_utils.MockModel.create(
responses=[function_call_no_schema, 'response1', 'response2']
)
tool_agent = Agent(
name='tool_agent',
model=mock_model,
)
root_agent = Agent(
name='root_agent',
model=mock_model,
tools=[AgentTool(agent=tool_agent, include_plugins=True)],
)
runner = testing_utils.InMemoryRunner(root_agent, plugins=[tracking_plugin])
runner.run('test1')
# Plugin should be called for both root_agent and tool_agent
assert tracking_plugin.before_agent_calls == 2
def test_include_plugins_false():
"""Test that plugins are NOT propagated when include_plugins=False."""
class TrackingPlugin(BasePlugin):
def __init__(self, name: str):
super().__init__(name)
self.before_agent_calls = 0
async def before_agent_callback(self, **kwargs):
self.before_agent_calls += 1
tracking_plugin = TrackingPlugin(name='tracking')
mock_model = testing_utils.MockModel.create(
responses=[function_call_no_schema, 'response1', 'response2']
)
tool_agent = Agent(
name='tool_agent',
model=mock_model,
)
root_agent = Agent(
name='root_agent',
model=mock_model,
tools=[AgentTool(agent=tool_agent, include_plugins=False)],
)
runner = testing_utils.InMemoryRunner(root_agent, plugins=[tracking_plugin])
runner.run('test1')
# Plugin should only be called for root_agent, not tool_agent
assert tracking_plugin.before_agent_calls == 1
def test_agent_tool_description_with_input_schema():
"""Test that agent description is propagated when using input_schema."""
class CustomInput(BaseModel):
"""This is the Pydantic model docstring."""
custom_input: str
agent_description = 'This is the agent description that should be used'
tool_agent = Agent(
name='tool_agent',
model=testing_utils.MockModel.create(responses=['test response']),
description=agent_description,
input_schema=CustomInput,
)
agent_tool = AgentTool(agent=tool_agent)
declaration = agent_tool._get_declaration()
# The description should come from the agent, not the Pydantic model
assert declaration.description == agent_description