ADK filters events that contain only function responses
Describe the bug As part of the preparation for an LLM call, the ADK will look through the session’s event list to create the LLM message history. As part of this, it filters out all events that don’t have any content or roles. Additionally, it will filter out any events that don’t have any text. Unfortunately, there are cases where there are events that just contain the function_response. These events are filtered mistakenly as well.
To Reproduce Steps to reproduce the behavior:
- Download the attached script
- Convert the .txt format to .py
- Run the script by 'python reproduce.py'
It creates a user, llm and tool event. The tool is a simple function call that returns only the function_response. This event is filtered via ADK.
Expected behavior The script sends What is the weather in London? user query and calls get_weather tool; however, the output only includes the user query because get_weather returns only function_response.
The output is:
parts=[Part(
text='What is the weather in London?'
)] role='user'
parts=[Part(
function_call=FunctionCall(
args={
'city': 'London'
},
id='tool_call_123',
name='get_weather'
)
)] role='model'
The output should be:
parts=[Part(
text='What is the weather in London?'
)] role='user'
parts=[Part(
function_call=FunctionCall(
args={
'city': 'London'
},
id='tool_call_123',
name='get_weather'
)
)] role='model'
parts=[Part(
text='For context:'
), Part(
text="[tool] `get_weather` tool returned result: {'result': 'The weather in London is sunny.'}"
)] role='user'
Desktop (please complete the following information):
- OS: MacOS
- Python version(python -V): 3.12.0
- ADK version(pip show google-adk): 1.8.0
Model Information: Sent a corresponding PR. https://github.com/google/adk-python/pull/2152
@alimosaed Thanks for reporting the issue! I have reproduced successfully,To fix the bug, update the _get_contents() function in google/adk/flows/llm_flows/contents.py. The old code skipped tool events, so tool responses were missing. The new fix adds logic to handle role="tool" and include tool outputs in the processed content. After updating and redeploying, the tool responses will appear correctly along with user and model messages.
def _get_contents(current_branch, events, agent_name):
"""
Fixed version of _get_contents to include tool responses.
Converts tool events into readable context parts.
"""
contents = []
for event in events:
if event.content.role in ["user", "model"]:
contents.append(event.content)
elif event.content.role == "tool":
for part in event.content.parts:
if part.function_response:
tool_name = part.function_response.name
tool_result = part.function_response.response
context_text = f"[tool] {tool_name} tool returned result: {tool_result}"
contents.append(types.Content(
role="user",
parts=[
types.Part.from_text(text="For context:"),
types.Part.from_text(text=context_text)
]
))
return contents
Thanks!