Skip to content

Commit 817b264

Browse files
test(code_execution): add tests for pre-processor instruction injection
1 parent 894c24f commit 817b264

1 file changed

Lines changed: 52 additions & 0 deletions

File tree

tests/unittests/flows/llm_flows/test_code_execution.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@
2525
from google.adk.code_executors.code_execution_utils import CodeExecutionResult
2626
from google.adk.flows.llm_flows._code_execution import _extract_code_from_error_message
2727
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
2830
from google.adk.flows.llm_flows._code_execution import response_processor
31+
from google.adk.models.llm_request import LlmRequest
2932
from google.adk.models.llm_response import LlmResponse
3033
from google.genai import types
3134
import pytest
@@ -227,3 +230,52 @@ def test_maybe_recover_unparseable_message():
227230
error_message='some completely different message',
228231
)
229232
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

Comments
 (0)