Describe the Bug:
When attaching a code_executor (like AgentEngineSandboxCodeExecutor in my case) to an LlmAgent, the underlying model overly favors using the code execution tool. If a user explicitly asks to generate, write, or draft code, the model returns the code inside part.executable_code.code rather than as standard Markdown in part.text. Because the ADK loop automatically processes executable_code, the agent executes code that was only meant to be displayed to the user.
Steps to Reproduce:
Basic ADK steps to run agent.
Expected Behavior:
For generation-only prompts (e.g., "Generate...", "Write..."), the model should return the code formatted as Markdown inside part.text. The code_executor workflow should only be triggered if the user explicitly asks to compute a result, execute the code, or if the model needs to solve an analytical problem.
Ideally, the ADK's base agent could provide a mechanism to intercept or strictly prompt the model to differentiate between "code to write" and "code to run."
Observed Behavior:
The model places the generation script inside part.executable_code.code, triggering the underlying ADK agent loop to automatically run the script in the sandbox.
Environment Details:
- ADK Library Version (pip show google-adk): 1.33.0
- Desktop OS: Ubuntu 24.04.3 WSL
- Python Version (python -V): 3.13.7
Model Information:
- Are you using LiteLLM: Yes (but I tried with default gemini-2.5-flash same there)
- Which model is being used: azure/gpt-4.1
Logs:
Here is event.model_dump_json from my setup
{"content":{"parts":[{"executable_code":{"code":"import matplotlib.pyplot as plt\nimport numpy as np\n\n# Generate 100 random points\nx = np.random.rand(100)\ny = np.random.rand(100)\n\n# Create scatter plot\nplt.scatter(x, y)\nplt.title('Scatter Plot of 100 Random Points')\nplt.xlabel('X')\nplt.ylabel('Y')\nplt.show()","language":"PYTHON"}}],"role":"model"},"invocation_id":"--","author":"playground_agent","actions":{"state_delta":{},"artifact_delta":{},"requested_auth_configs":{},"requested_tool_confirmations":{}},"id":"--","timestamp":1779799981.6535492}
{"content":{"parts":[{"code_execution_result":{"outcome":"OUTCOME_OK","output":"Saved artifacts:\n`output_2026-05-26-12-53-14-408679.png`"}}],"role":"model"},"invocation_id":"---","author":"playground_agent","actions":{"state_delta":{"_code_execution_context":{}},"artifact_delta":{"output_2026-05-26-12-53-14-408679.png":0},"requested_auth_configs":{},"requested_tool_confirmations":{}},"id":"--","timestamp":1779799994.5919266}
{"model_version":"gpt-4.1-2025-04-14","content":{"parts":[{"text":"Great! You’ve successfully saved the scatter plot as an image file. Here’s a quick summary:\n\n- **Filename:** output_2026-05-26-12-53-14-408679.png\n- **Description:** Scatter plot of 100 random points using matplotlib in Python.\n\nIf you want to view, download, or make changes to the plot, just let me know how you'd like to proceed!"}],"role":"model"},"partial":false,"finish_reason":"STOP","usage_metadata":{"cached_content_token_count":0,"candidates_token_count":86,"prompt_token_count":1113,"total_token_count":1199},"invocation_id":"---","author":"playground_agent","actions":{"state_delta":{},"artifact_delta":{},"requested_auth_configs":{},"requested_tool_confirmations":{}},"id":"--","timestamp":1779799994.5964568}
Screenshots / Video:
N/A
Additional Context:
- I have used
AgentEngineSandboxCodeExecutor But I believe it would be same for other CodeExecutor also.
- I am currently trying it by heavily prompting the agent in the system
instruction to avoid the code execution tool for generation tasks but not seems to working for now.
Minimal Reproduction Code:
Exact setup can't shared as it is integrated in our system core. But here is sample
from google.adk.agents.llm_agent import LlmAgent
from google.adk.code_executors.agent_engine_sandbox_code_executor import AgentEngineSandboxCodeExecutor
# 1. Initialize the Code Executor
code_executor = AgentEngineSandboxCodeExecutor(
agent_engine_resource_name="projects/YOUR_PROJECT/locations/YOUR_LOCATION/reasoningEngines/YOUR_APP_ID"
)
# 2. Initialize the Agent
agent = LlmAgent(
name="coding_agent",
model="gemini-2.5-flash", # Note: Similar behavior observed with other models
code_executor=code_executor,
instruction="You are a helpful Python coding assistant."
)
# 3. Prompt the agent with a generation-only task
# The agent will generate AND automatically execute this code, despite not being asked to run it.
response = agent.run("Generate python code for a matplotlib scatter graph for 100 random points")
How often has this issue occurred?:
- Always (100%) (from my testing)
Describe the Bug:
When attaching a
code_executor(likeAgentEngineSandboxCodeExecutorin my case) to anLlmAgent, the underlying model overly favors using the code execution tool. If a user explicitly asks to generate, write, or draft code, the model returns the code insidepart.executable_code.coderather than as standard Markdown inpart.text. Because the ADK loop automatically processesexecutable_code, the agent executes code that was only meant to be displayed to the user.Steps to Reproduce:
Basic ADK steps to run agent.
Expected Behavior:
For generation-only prompts (e.g., "Generate...", "Write..."), the model should return the code formatted as Markdown inside
part.text. Thecode_executorworkflow should only be triggered if the user explicitly asks to compute a result, execute the code, or if the model needs to solve an analytical problem.Ideally, the ADK's base agent could provide a mechanism to intercept or strictly prompt the model to differentiate between "code to write" and "code to run."
Observed Behavior:
The model places the generation script inside
part.executable_code.code, triggering the underlying ADK agent loop to automatically run the script in the sandbox.Environment Details:
Model Information:
Logs:
Here is
event.model_dump_jsonfrom my setup{"content":{"parts":[{"executable_code":{"code":"import matplotlib.pyplot as plt\nimport numpy as np\n\n# Generate 100 random points\nx = np.random.rand(100)\ny = np.random.rand(100)\n\n# Create scatter plot\nplt.scatter(x, y)\nplt.title('Scatter Plot of 100 Random Points')\nplt.xlabel('X')\nplt.ylabel('Y')\nplt.show()","language":"PYTHON"}}],"role":"model"},"invocation_id":"--","author":"playground_agent","actions":{"state_delta":{},"artifact_delta":{},"requested_auth_configs":{},"requested_tool_confirmations":{}},"id":"--","timestamp":1779799981.6535492} {"content":{"parts":[{"code_execution_result":{"outcome":"OUTCOME_OK","output":"Saved artifacts:\n`output_2026-05-26-12-53-14-408679.png`"}}],"role":"model"},"invocation_id":"---","author":"playground_agent","actions":{"state_delta":{"_code_execution_context":{}},"artifact_delta":{"output_2026-05-26-12-53-14-408679.png":0},"requested_auth_configs":{},"requested_tool_confirmations":{}},"id":"--","timestamp":1779799994.5919266} {"model_version":"gpt-4.1-2025-04-14","content":{"parts":[{"text":"Great! You’ve successfully saved the scatter plot as an image file. Here’s a quick summary:\n\n- **Filename:** output_2026-05-26-12-53-14-408679.png\n- **Description:** Scatter plot of 100 random points using matplotlib in Python.\n\nIf you want to view, download, or make changes to the plot, just let me know how you'd like to proceed!"}],"role":"model"},"partial":false,"finish_reason":"STOP","usage_metadata":{"cached_content_token_count":0,"candidates_token_count":86,"prompt_token_count":1113,"total_token_count":1199},"invocation_id":"---","author":"playground_agent","actions":{"state_delta":{},"artifact_delta":{},"requested_auth_configs":{},"requested_tool_confirmations":{}},"id":"--","timestamp":1779799994.5964568}Screenshots / Video:
N/A
Additional Context:
AgentEngineSandboxCodeExecutorBut I believe it would be same for otherCodeExecutoralso.instructionto avoid the code execution tool for generation tasks but not seems to working for now.Minimal Reproduction Code:
Exact setup can't shared as it is integrated in our system core. But here is sample
How often has this issue occurred?: