adk-python icon indicating copy to clipboard operation
adk-python copied to clipboard

Session can't continue after tool error or stopping stream

Open hung12ct opened this issue 1 year ago • 4 comments

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:

  1. Trigger a tool in the agent.
  2. Either: Raise an exception inside the tool, or Sstop the stream midway during the tool response.
  3. Send a new user message.
  4. 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

Image

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?

hung12ct avatar Jun 18 '25 03:06 hung12ct

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

jasveer-os avatar Jun 22 '25 10:06 jasveer-os

Thanks, my current workaround is deleting all events during the error/stop period

hung12ct avatar Jun 22 '25 13:06 hung12ct

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!

surajksharma07 avatar Oct 08 '25 11:10 surajksharma07

This is now supported in latest release, you can use runner.rewind_async() to rewind the failed invocation.

DeanChensj avatar Oct 24 '25 03:10 DeanChensj