|
25 | 25 | from google.adk.code_executors.code_execution_utils import CodeExecutionResult |
26 | 26 | from google.adk.flows.llm_flows._code_execution import _extract_code_from_error_message |
27 | 27 | from google.adk.flows.llm_flows._code_execution import _maybe_recover_from_api_rejection |
| 28 | +from google.adk.flows.llm_flows._code_execution import _NON_BUILTIN_EXECUTOR_INSTRUCTION |
| 29 | +from google.adk.flows.llm_flows._code_execution import request_processor |
28 | 30 | from google.adk.flows.llm_flows._code_execution import response_processor |
| 31 | +from google.adk.models.llm_request import LlmRequest |
29 | 32 | from google.adk.models.llm_response import LlmResponse |
30 | 33 | from google.genai import types |
31 | 34 | import pytest |
@@ -227,3 +230,52 @@ def test_maybe_recover_unparseable_message(): |
227 | 230 | error_message='some completely different message', |
228 | 231 | ) |
229 | 232 | assert _maybe_recover_from_api_rejection(llm_response) is False |
| 233 | + |
| 234 | + |
| 235 | +# --------------------------------------------------------------------------- |
| 236 | +# Pre-processor: instruction injection |
| 237 | +# --------------------------------------------------------------------------- |
| 238 | + |
| 239 | + |
| 240 | +@pytest.mark.asyncio |
| 241 | +async def test_pre_processor_injects_instruction_for_non_builtin_executor(): |
| 242 | + mock_executor = MagicMock(spec=BaseCodeExecutor) |
| 243 | + mock_executor.optimize_data_file = False |
| 244 | + |
| 245 | + agent = Agent(name='test_agent', code_executor=mock_executor) |
| 246 | + invocation_context = await testing_utils.create_invocation_context( |
| 247 | + agent=agent, user_content='run some code' |
| 248 | + ) |
| 249 | + llm_request = LlmRequest() |
| 250 | + |
| 251 | + _ = [ |
| 252 | + event |
| 253 | + async for event in request_processor.run_async( |
| 254 | + invocation_context, llm_request |
| 255 | + ) |
| 256 | + ] |
| 257 | + |
| 258 | + assert llm_request.config.system_instruction is not None |
| 259 | + assert _NON_BUILTIN_EXECUTOR_INSTRUCTION in str( |
| 260 | + llm_request.config.system_instruction |
| 261 | + ) |
| 262 | + |
| 263 | + |
| 264 | +@pytest.mark.asyncio |
| 265 | +async def test_pre_processor_does_not_inject_instruction_for_builtin_executor(): |
| 266 | + code_executor = BuiltInCodeExecutor() |
| 267 | + agent = Agent(name='test_agent', code_executor=code_executor) |
| 268 | + invocation_context = await testing_utils.create_invocation_context( |
| 269 | + agent=agent, user_content='run some code' |
| 270 | + ) |
| 271 | + llm_request = LlmRequest(model='gemini-2.0-flash') |
| 272 | + |
| 273 | + _ = [ |
| 274 | + event |
| 275 | + async for event in request_processor.run_async( |
| 276 | + invocation_context, llm_request |
| 277 | + ) |
| 278 | + ] |
| 279 | + |
| 280 | + system_instruction = str(llm_request.config.system_instruction or '') |
| 281 | + assert _NON_BUILTIN_EXECUTOR_INSTRUCTION not in system_instruction |
0 commit comments