Repository navigation
Closed
Labels
need attention[Status] This issue is important but the maintainers haven't started working on ittools[Component] This issue is related to tools
Description
the raw response of the code execution returns the following block:
Raw response:
{"candidates":[{"content":{"parts":[{"executable_code":{"code":"import math\nprint(math.factorial(5))\n","language":"PYTHON"}},{"code_execution_result":{"outcome":"OUTCOME_OK","output":"120\n"}},{"text":"120\n"}],"role":"model"},"finish_reason":"STOP"}],"model_version":"gemini-2.0-flash","usage_metadata":{"candidates_token_count":20,"candidates_tokens_details":[{"modality":"TEXT","token_count":20}],"prompt_token_count":91,"prompt_tokens_details":[{"modality":"TEXT","token_count":91}],"tool_use_prompt_token_count":107,"tool_use_prompt_tokens_details":[{"modality":"TEXT","token_count":107}],"total_token_count":218},"automatic_function_calling_history":[]}
We can see that the model successfully executed the code and is returning the output of the execution in "output" key.
However, the model cannot access the "outcome" or the "output" key from the function response above. But it gets access to this:
Generated event in agent run streaming: {"content":{"parts":[{"functionResponse":{"id":"adk-78a12908-7fc8-47ee-8c57-b685989bc5f5","name":"CodingAgent","response":{"result":""}}}],"role":"user"},"invocation_id":"e-d2de849d-3df7-4603-9ca6-f02ea539af54","author":"jarvis","actions":{"state_delta":{},"artifact_delta":{},"requested_auth_configs":{}},"id":"iJURFFJN","timestamp":1744628648.768249}
In this snippet, we can see that the response of the coding agent is a dictionary containing result which is an empty string.
The agent looks into this response dictionary to provide final output to the user, however it seems to be a bug or something that the agent is not correctly accessing the output of the code execution. This causes the agent to get into a loop and, eventually, fails due to exhaustive depth limit.
Metadata
Metadata
Assignees
Labels
need attention[Status] This issue is important but the maintainers haven't started working on ittools[Component] This issue is related to tools
Activity
hangfei commentedon Apr 14, 2025
@adars124 could you provide a minimal example to reproduce your issue?
adars124 commentedon Apr 15, 2025
Run the web interface:
uv run adk webAsk question like: 'what's the result of 4*8-2+6/4?' or 'calcualte the fibonacci series upto the 10th term using python'.
It gives the following response as of now, yesterday there was the problem of infinite loop.
I have also noticed that, even though we carefully craft the prompt as we need it, the model fails to properly understand the user intent and execute the corresponding tool. I have to explicitly tell it to either google search, or execute the code, and things alike.
take a look at this prompt: 'what's the nepali date today?' -> response: 'Could you please specify which Gregorian date you would like me to convert to the Nepali date?'
i have to explicitly tell it to do this: 'search google and tell me today's nepali date'
and only then i get the intended output using google search: 'Today's Nepali date is Baishakh 2, 2082.'
hangfei commentedon Apr 15, 2025
@adars124 the new issue you observed is usually related model's capability and knowledge base.
The model has less knowledge on the problem you try to tackled. I suggest you add more background information on what's Nepali date, and what's the task etc. You can use another model to generate the prompt. It can be like"I am building an agent. help me build a better system instruction(prompt) based on my current setup and failure patterns i have observed."
cc: @polong-lin to add more documentation to help with this kind of problem.
adars124 commentedon Apr 16, 2025
@hangfei sure will try creating better prompts. But what is the issue related to code execution? The model cannot access the output of the code execution for some reason. Need help on this.
dcdj99 commentedon Apr 25, 2025
I am also facing the same issue but it works after when I used the exact same prompt form the docs https://google.github.io/adk-docs/tools/built-in-tools/
Chand-d commentedon May 3, 2025
I am also facing the same issue, my search agent is working fine but when i try running the code agent ( even simple queries ). it performs the calculations and even gets answers right but it returns empty result array. here is my agent.py and logs.
#agent.py
from google.adk.tools import agent_tool
from google.adk.agents import Agent
from google.adk.tools import google_search, built_in_code_execution
import logging
import json
search_agent = Agent(
model='gemini-2.0-flash',
name='SearchAgent',
instruction="""
Your Role: You are a specialized information retrieval assistant.
Your ONLY Task: Use the google_search tool to find answers.
Input: Receive query via 'request'.
Action: Call google_search(query=...).
Output: Return ONLY the tool result.
""",
description="Performs web searches.",
tools=[google_search],
)
coding_agent = Agent(
model='gemini-2.0-flash',
name='CodeAgent',
instruction="""
Your Role: You are a specialized code execution assistant.
Your Task: Execute Python code and return its output.
CRITICAL INSTRUCTIONS:
EXACT RESPONSE FORMAT:
[exact output from code execution]
EXAMPLES:
CRITICAL:
""",
description="Executes Python code and returns raw output",
tools=[built_in_code_execution],
)
root_agent = Agent(
name="RootAgent",
model="gemini-2.0-flash",
instruction="""
Your Role: You are the primary conversational assistant.
Available Specialist Tools:
Process for CodeAgent:
CodeAgent(request=code).CRITICAL INSTRUCTIONS:
EXAMPLES:
""",
description="Main conversational agent that delegates tasks",
tools=[
agent_tool.AgentTool(agent=search_agent),
agent_tool.AgentTool(agent=coding_agent)
],
)
#logs
here you can see,
Raw response:
{"code_execution_result":{"outcome":"OUTCOME_OK","output":"The sum of all such integers is: 20097264105\n"}},{"text":"The sum of all such integers is: 20097264105\n"}],"role":"model"},"finish_reason":"STOP"}], and {"parts":[{"function_response":
but,
{"name":"CodeAgent","response":{"result":""}}}],"role":"user"}.
nathan-nguyen-webjet commentedon May 27, 2025
I am having the same problem, using code_executor=VertexAiCodeExecutor(optimize_data_file=True,stateful=True) works but the built_in_code_executors will always return {}
Chand-d commentedon May 27, 2025
I am using a way around like this and it works.
agent.py
from google.adk.tools import agent_tool, FunctionTool
from google.adk.agents import Agent
from google.adk.tools import google_search, built_in_code_execution
from typing import Dict, Any
import io
import sys
import logging
import traceback
--- Specialist Agent Definitions ---
Search Agent - This is working fine, so don't modify it
search_agent = Agent(
model='gemini-2.0-flash',
name='SearchAgent',
instruction="""
Your Role: You are a specialized information retrieval assistant.
Your ONLY Task: Use the google_search tool to find answers for the given query.
Input: Receive query via 'request'.
Action: Call google_search(query=...).
Output: Return ONLY the search results provided by the tool.
""",
description="Performs web searches to answer questions.",
tools=[google_search],
)
--- Direct Code Execution Function ---
async def execute_code_via_agent(code_string: str) -> Dict[str, Any]:
"""
Executes the given Python code string directly using Python's exec() function
and returns the standard output in a dictionary.
Wrap the function into a FunctionTool
code_execution_tool = FunctionTool(
func=execute_code_via_agent
)
--- Root Agent Definition ---
Root Agent that uses both tools
root_agent = Agent(
name="RootAgent",
model="gemini-2.0-flash",
instruction="""
You are the primary conversational assistant that can perform two types of tasks:
For code execution:
For search:
General guidelines:
""",
description="Main conversational agent that delegates search or code execution tasks.",
tools=[
agent_tool.AgentTool(agent=search_agent),
code_execution_tool
],
)
hangfei commentedon Jun 1, 2025
@seanzhou1023 Could you helpl take a look?
KSonPham commentedon Jul 2, 2025
Hey @adars124, have you tried:
seanzhou1023 commentedon Jul 12, 2025
@adars124 thank you for reporting the issue!
Given the code executor response is correct, I would assume it was a bug in agent tool.
@Chand-d thank you for providing the codes to reproduce the issue and providing a workaround.
However as @KSonPham mentioned, recently we've migrate build in code execution tool to code executor, I've migrated the exmaple provided by @Chand-d below :
it works fine now.
feel free to reopen the issue if it still doesn't work.