forked from google/adk-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_basic_processor.py
More file actions
296 lines (244 loc) · 9.83 KB
/
Copy pathtest_basic_processor.py
File metadata and controls
296 lines (244 loc) · 9.83 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
# 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.
"""Tests for basic LLM request processor."""
from unittest import mock
from google.adk.agents.invocation_context import InvocationContext
from google.adk.agents.llm_agent import LlmAgent
from google.adk.agents.run_config import RunConfig
from google.adk.flows.llm_flows.basic import _BasicLlmRequestProcessor
from google.adk.models.llm_request import LlmRequest
from google.adk.sessions.in_memory_session_service import InMemorySessionService
from google.adk.tools.function_tool import FunctionTool
from google.genai import types
from pydantic import BaseModel
from pydantic import Field
import pytest
class OutputSchema(BaseModel):
"""Test schema for output."""
name: str = Field(description='A name')
value: int = Field(description='A value')
def dummy_tool(query: str) -> str:
"""A dummy tool for testing."""
return f'Result: {query}'
async def _create_invocation_context(agent: LlmAgent) -> InvocationContext:
"""Helper to create InvocationContext for testing."""
session_service = InMemorySessionService()
session = await session_service.create_session(
app_name='test_app', user_id='test_user'
)
return InvocationContext(
invocation_id='test-id',
agent=agent,
session=session,
session_service=session_service,
run_config=RunConfig(),
)
class TestBasicLlmRequestProcessor:
"""Test class for _BasicLlmRequestProcessor."""
@pytest.mark.asyncio
async def test_sets_output_schema_when_no_tools(self):
"""Test that processor sets output_schema when agent has no tools."""
agent = LlmAgent(
name='test_agent',
model='gemini-2.5-flash',
output_schema=OutputSchema,
tools=[], # No tools
)
invocation_context = await _create_invocation_context(agent)
llm_request = LlmRequest()
processor = _BasicLlmRequestProcessor()
# Process the request
events = []
async for event in processor.run_async(invocation_context, llm_request):
events.append(event)
# Should have set response_schema since agent has no tools
assert llm_request.config.response_schema == OutputSchema
assert llm_request.config.response_mime_type == 'application/json'
@pytest.mark.asyncio
async def test_skips_output_schema_when_tools_present(self, mocker):
"""Test that processor skips output_schema when agent has tools."""
agent = LlmAgent(
name='test_agent',
model='gemini-2.5-flash',
output_schema=OutputSchema,
tools=[FunctionTool(func=dummy_tool)], # Has tools
)
invocation_context = await _create_invocation_context(agent)
llm_request = LlmRequest()
processor = _BasicLlmRequestProcessor()
can_use_output_schema_with_tools = mocker.patch(
'google.adk.flows.llm_flows.basic.can_use_output_schema_with_tools',
mock.MagicMock(return_value=False),
)
# Process the request
events = []
async for event in processor.run_async(invocation_context, llm_request):
events.append(event)
# Should NOT have set response_schema since agent has tools
assert llm_request.config.response_schema is None
assert llm_request.config.response_mime_type != 'application/json'
# Should have checked if output schema can be used with tools
can_use_output_schema_with_tools.assert_called_once_with(
agent.canonical_model
)
@pytest.mark.asyncio
async def test_sets_output_schema_when_tools_present(self, mocker):
"""Test that processor skips output_schema when agent has tools."""
agent = LlmAgent(
name='test_agent',
model='gemini-2.5-flash',
output_schema=OutputSchema,
tools=[FunctionTool(func=dummy_tool)], # Has tools
)
invocation_context = await _create_invocation_context(agent)
llm_request = LlmRequest()
processor = _BasicLlmRequestProcessor()
can_use_output_schema_with_tools = mocker.patch(
'google.adk.flows.llm_flows.basic.can_use_output_schema_with_tools',
mock.MagicMock(return_value=True),
)
# Process the request
events = []
async for event in processor.run_async(invocation_context, llm_request):
events.append(event)
# Should have set response_schema since output schema can be used with tools
assert llm_request.config.response_schema == OutputSchema
assert llm_request.config.response_mime_type == 'application/json'
# Should have checked if output schema can be used with tools
can_use_output_schema_with_tools.assert_called_once_with(
agent.canonical_model
)
@pytest.mark.asyncio
async def test_no_output_schema_no_tools(self):
"""Test that processor works normally when agent has no output_schema or tools."""
agent = LlmAgent(
name='test_agent',
model='gemini-2.5-flash',
# No output_schema, no tools
)
invocation_context = await _create_invocation_context(agent)
llm_request = LlmRequest()
processor = _BasicLlmRequestProcessor()
# Process the request
events = []
async for event in processor.run_async(invocation_context, llm_request):
events.append(event)
# Should not have set anything
assert llm_request.config.response_schema is None
assert llm_request.config.response_mime_type != 'application/json'
@pytest.mark.asyncio
async def test_sets_model_name(self):
"""Test that processor sets the model name correctly."""
agent = LlmAgent(
name='test_agent',
model='gemini-2.5-flash',
)
invocation_context = await _create_invocation_context(agent)
llm_request = LlmRequest()
processor = _BasicLlmRequestProcessor()
# Process the request
events = []
async for event in processor.run_async(invocation_context, llm_request):
events.append(event)
# Should have set the model name
assert llm_request.model == 'gemini-2.5-flash'
@pytest.mark.asyncio
async def test_skips_output_schema_for_task_mode(self):
"""Test that processor skips output_schema when agent is in task mode."""
agent = LlmAgent(
name='test_agent',
model='gemini-2.5-flash',
mode='task',
output_schema=OutputSchema,
)
invocation_context = await _create_invocation_context(agent)
llm_request = LlmRequest()
processor = _BasicLlmRequestProcessor()
async for _ in processor.run_async(invocation_context, llm_request):
pass
assert llm_request.config.response_schema is None
@pytest.mark.asyncio
async def test_disables_affective_dialog_and_proactivity_for_gemini_3_1_live(
self,
):
"""Gemini 3.1 Live does not support affective_dialog/proactivity."""
agent = LlmAgent(
name='test_agent',
model='gemini-3.1-flash-live-preview',
)
invocation_context = await _create_invocation_context(agent)
invocation_context.run_config = RunConfig(
enable_affective_dialog=True,
proactivity=types.ProactivityConfig(),
)
llm_request = LlmRequest()
processor = _BasicLlmRequestProcessor()
async for _ in processor.run_async(invocation_context, llm_request):
pass
assert llm_request.live_connect_config.enable_affective_dialog is None
assert llm_request.live_connect_config.proactivity is None
@pytest.mark.asyncio
async def test_keeps_affective_dialog_and_proactivity_for_non_gemini_3_1(
self,
):
"""Non-3.1 live models keep the configured affective_dialog/proactivity."""
agent = LlmAgent(
name='test_agent',
model='gemini-2.5-flash-live',
)
invocation_context = await _create_invocation_context(agent)
invocation_context.run_config = RunConfig(
enable_affective_dialog=True,
proactivity=types.ProactivityConfig(),
)
llm_request = LlmRequest()
processor = _BasicLlmRequestProcessor()
async for _ in processor.run_async(invocation_context, llm_request):
pass
assert llm_request.live_connect_config.enable_affective_dialog is True
assert llm_request.live_connect_config.proactivity is not None
@pytest.mark.asyncio
async def test_sets_translation_config(self):
"""Translation config is forwarded to the live connect config."""
agent = LlmAgent(
name='test_agent',
model='gemini-3.5-live-translate-preview',
)
invocation_context = await _create_invocation_context(agent)
invocation_context.run_config = RunConfig(
translation_config=types.TranslationConfig(
target_language_code='pl',
echo_target_language=True,
),
)
llm_request = LlmRequest()
processor = _BasicLlmRequestProcessor()
async for _ in processor.run_async(invocation_context, llm_request):
pass
translation_config = llm_request.live_connect_config.translation_config
assert translation_config.target_language_code == 'pl'
assert translation_config.echo_target_language is True
@pytest.mark.asyncio
async def test_translation_config_defaults_to_none(self):
"""Without a translation config the live connect field stays None."""
agent = LlmAgent(
name='test_agent',
model='gemini-2.5-flash-live',
)
invocation_context = await _create_invocation_context(agent)
llm_request = LlmRequest()
processor = _BasicLlmRequestProcessor()
async for _ in processor.run_async(invocation_context, llm_request):
pass
assert llm_request.live_connect_config.translation_config is None