Session can't continue after tool error or stopping stream
Describe the bug
Currently if I stop the stream mid-response, or if a tool throws an error before finishing, the session becomes unusable. Any new message returns:
litellm.BadRequestError: OpenAIException - An assistant message with 'tool_calls' must be followed by tool messages responding to each 'tool_call_id'.
It seems the session expects unfinished tool calls to be completed, even after failure or interruption. Would be great if the session could recover or reset tool_call state in such cases.
To Reproduce Steps to reproduce the behavior:
- Trigger a tool in the agent.
- Either: Raise an exception inside the tool, or Sstop the stream midway during the tool response.
- Send a new user message.
- Error occurs: tool_call_id not responded. Expected behavior I expect to be able to stop the stream at any point without breaking the session. Also, if a tool throws an exception, the session should remain usable => it shouldn’t get stuck or throw tool_call_id errors on the next user message.
Screenshots
Desktop (please complete the following information):
- OS: Linux Ubuntu
- Python version(python -V): 3.12
- ADK version(pip show google-adk): 1.3.0 How can i fix this?
I've also faced this issue. The Runner appends it based on interaction, and does not clean up on failures. For now I bypassed this by cleaning LLMRequest in before_model_callback for LLM.
Code:
def sanitize_llm_requests(llm_req_with_contents: LlmRequest) -> LlmRequest:
cleaned_contents = []
i = 0
llm_req = llm_req_with_contents.contents
while i < len(llm_req):
current_content = llm_req[i]
parts = current_content.parts if hasattr(current_content, 'parts') else []
if parts and hasattr(parts[0], 'function_call') and parts[0].function_call is not None:
# this is a function_call — check next element exists and is a function_response
if i + 1 < len(llm_req):
# it's not the last function call
next_parts = llm_req[i + 1].parts if hasattr(llm_req[i + 1], 'parts') else []
if next_parts and hasattr(next_parts[0], 'function_response') and next_parts[0].function_response is not None:
# valid pair — keep both
cleaned_contents.append(current_content)
cleaned_contents.append(llm_req[i + 1])
i += 2
else:
# invalid — skip function_call
i += 1
else:
# no next content - most recent, ignore skipping
cleaned_contents.append(current_content)
i += 1
else:
# not a function_call — keep as is
cleaned_contents.append(current_content)
i += 1
llm_req_with_contents.contents = cleaned_contents
You can also use tool_call_id to keep/reject array elements
Thanks, my current workaround is deleting all events during the error/stop period
Hi @hung12ct and thanks to everyone who participated in this thread.
We're currently doing some housekeeping on our issue tracker. Given the rapid pace of development and the number of updates since this was last active, there's a good chance this issue has been resolved in a newer version. To help us keep our backlog focused on current issues, we are closing this as stale.
If you're still facing this problem, could you please update to the very latest version of the library and confirm the issue still exists? If it does, please comment here, and we will be happy to reopen this issue and investigate further.
Thanks for your contribution and understanding!
This is now supported in latest release, you can use runner.rewind_async() to rewind the failed invocation.