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

fix(agent_tool): handle case when last_content.parts are unavailable

Open sumkincpp opened this issue 1 month ago • 3 comments

Problem:

This is small fix that handles the case when some reply content parts are not present in agent_tool replies (last_content.parts).

Stacktrace:

  File "/app/.venv/bin/apex-agent", line 10, in <module>
    sys.exit(main())
             ~~~~^^
  File "/app/src/apex_agent/main.py", line 904, in main
    asyncio.run(async_main())
    ~~~~~~~~~~~^^^^^^^^^^^^^^
  File "/home/ubuntu/.local/share/uv/python/cpython-3.13.9-linux-x86_64-gnu/lib/python3.13/asyncio/runners.py", line 195, in run
    return runner.run(main)
           ~~~~~~~~~~^^^^^^
  File "/home/ubuntu/.local/share/uv/python/cpython-3.13.9-linux-x86_64-gnu/lib/python3.13/asyncio/runners.py", line 118, in run
    return self._loop.run_until_complete(task)
           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^
  File "/home/ubuntu/.local/share/uv/python/cpython-3.13.9-linux-x86_64-gnu/lib/python3.13/asyncio/base_events.py", line 725, in run_until_complete
    return future.result()
           ~~~~~~~~~~~~~^^
  File "/app/src/apex_agent/main.py", line 898, in async_main
    await run_pipeline(args.config, args.pipeline, args.query, debug=args.debug, verbose=args.verbose)
  File "/app/src/apex_agent/main.py", line 823, in run_pipeline
    events = await runner.run_debug(query, quiet=True, verbose=verbose)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/app/.venv/lib/python3.13/site-packages/google/adk/runners.py", line 1054, in run_debug
    async for event in self.run_async(
    ...<8 lines>...
      collected_events.append(event)
  File "/app/.venv/lib/python3.13/site-packages/google/adk/runners.py", line 454, in run_async
    async for event in agen:
      yield event
  File "/app/.venv/lib/python3.13/site-packages/google/adk/runners.py", line 442, in _run_with_trace
    async for event in agen:
      yield event
  File "/app/.venv/lib/python3.13/site-packages/google/adk/runners.py", line 654, in _exec_with_plugin
    async for event in agen:
    ...<9 lines>...
      yield (modified_event if modified_event else event)
  File "/app/.venv/lib/python3.13/site-packages/google/adk/runners.py", line 431, in execute
    async for event in agen:
      yield event
  File "/app/.venv/lib/python3.13/site-packages/google/adk/agents/base_agent.py", line 291, in run_async
    async for event in agen:
      yield event
  File "/app/src/apex_agent/main.py", line 298, in _run_async_impl
    async for event in super()._run_async_impl(ctx):
    ...<50 lines>...
                logger.warning("FAIL Problem cannot be solved - detected in content")
  File "/app/.venv/lib/python3.13/site-packages/google/adk/agents/llm_agent.py", line 460, in _run_async_impl
    async for event in agen:
    ...<5 lines>...
        should_pause = True
  File "/app/.venv/lib/python3.13/site-packages/google/adk/flows/llm_flows/base_llm_flow.py", line 346, in run_async
    async for event in agen:
      last_event = event
      yield event
  File "/app/.venv/lib/python3.13/site-packages/google/adk/flows/llm_flows/base_llm_flow.py", line 433, in _run_one_step_async
    async for event in agen:
    ...<3 lines>...
      yield event
  File "/app/.venv/lib/python3.13/site-packages/google/adk/flows/llm_flows/base_llm_flow.py", line 545, in _postprocess_async
    async for event in agen:
      yield event
  File "/app/.venv/lib/python3.13/site-packages/google/adk/flows/llm_flows/base_llm_flow.py", line 666, in _postprocess_handle_function_calls_async
    if function_response_event := await functions.handle_function_calls_async(
                                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        invocation_context, function_call_event, llm_request.tools_dict
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    ):
    ^
  File "/app/.venv/lib/python3.13/site-packages/google/adk/flows/llm_flows/functions.py", line 198, in handle_function_calls_async
    return await handle_function_call_list_async(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    ...<5 lines>...
    )
    ^
  File "/app/.venv/lib/python3.13/site-packages/google/adk/flows/llm_flows/functions.py", line 244, in handle_function_call_list_async
    function_response_events = await asyncio.gather(*tasks)
                               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/app/.venv/lib/python3.13/site-packages/google/adk/flows/llm_flows/functions.py", line 430, in _execute_single_function_call_async
    function_response_event = await _run_with_trace()
                              ^^^^^^^^^^^^^^^^^^^^^^^
  File "/app/.venv/lib/python3.13/site-packages/google/adk/flows/llm_flows/functions.py", line 379, in _run_with_trace
    raise tool_error
  File "/app/.venv/lib/python3.13/site-packages/google/adk/flows/llm_flows/functions.py", line 366, in _run_with_trace
    function_response = await __call_tool_async(
                        ^^^^^^^^^^^^^^^^^^^^^^^^
        tool, args=function_args, tool_context=tool_context
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    )
    ^
  File "/app/.venv/lib/python3.13/site-packages/google/adk/flows/llm_flows/functions.py", line 788, in __call_tool_async
    return await tool.run_async(args=args, tool_context=tool_context)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/app/.venv/lib/python3.13/site-packages/google/adk/tools/agent_tool.py", line 173, in run_async
    merged_text = '\n'.join(p.text for p in last_content.parts if p.text)
                                            ^^^^^^^^^^^^^^^^^^
TypeError: 'NoneType' object is not iterable

Solution:

Return empty string as result

Testing Plan

Rerun agent that uses agent_tool.

Checklist

  • [x] I have read the CONTRIBUTING.md document.
  • [x] I have performed a self-review of my own code.
  • [x] I have commented my code, particularly in hard-to-understand areas.
  • [ ] I have added tests that prove my fix is effective or that my feature works.
  • [x] New and existing unit tests pass locally with my changes.
  • [x] I have manually tested my changes end-to-end.
  • [x] Any dependent changes have been merged and published in downstream modules.

sumkincpp avatar Nov 26 '25 18:11 sumkincpp

Summary of Changes

Hello @sumkincpp, I'm Gemini Code Assist[^1]! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request resolves a 'TypeError' that occurred in the 'agent_tool' when processing agent replies where 'last_content.parts' could be 'None'. The change introduces a more robust conditional check to ensure that 'last_content.parts' exists before attempting to iterate over it, thereby preventing runtime crashes and improving the stability of the agent's operation.

Highlights

  • Error Handling: Implemented a fix to prevent a 'TypeError: 'NoneType' object is not iterable' that occurred when 'last_content.parts' was 'None' in the 'agent_tool.py' module.
  • Robustness: Enhanced the 'agent_tool's 'run_async' method to gracefully handle cases where reply content parts are unavailable, returning an empty string instead of crashing.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with :thumbsup: and :thumbsdown: on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

[^1]: Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

gemini-code-assist[bot] avatar Nov 26 '25 18:11 gemini-code-assist[bot]

Hi @sumkincpp , Thank you for your contribution! We appreciate you taking the time to submit this pull request. Your PR has been received by the team and is currently under review. We will provide feedback as soon as we have an update to share.

ryanaiagent avatar Nov 30 '25 05:11 ryanaiagent

Hi @wyf7107 , can you please review this.

ryanaiagent avatar Nov 30 '25 05:11 ryanaiagent