Skip to content

IndexError in ParallelAgent with LlmAgent #3523

@prolsen

Description

@prolsen

Summary

IndexError: list index out of range occurs in llm_agent.py line 449 when using LlmAgent instances within a ParallelAgent. The code attempts to access events[-2] without checking if the events list has at least 2 items.

Environment

  • ADK Version: google-adk - 1.18.0
    Summary: Agent Development Kit
  • Python Version: 3.12
  • OS: macOS

Bug Location

File: google/adk/agents/llm_agent.py
Line: 449

Current Code (Buggy)

if ctx.is_resumable:
  events = ctx._get_events(current_invocation=True, current_branch=True)
  if events and (
      ctx.should_pause_invocation(events[-1])
      or ctx.should_pause_invocation(events[-2])  # ← BUG: No length check
  ):
    return

The Problem

The code checks if events (ensuring the list is not empty), but then attempts to access events[-2] without verifying the list has at least 2 elements. This causes an IndexError when an agent generates only 1 event.

Proposed Fix

if ctx.is_resumable:
  events = ctx._get_events(current_invocation=True, current_branch=True)
  if events and (
      ctx.should_pause_invocation(events[-1])
      or (len(events) >= 2 and ctx.should_pause_invocation(events[-2]))  # ← Fixed
  ):
    return

How to Reproduce

Minimal Example

from google.adk.agents import Agent, SequentialAgent, ParallelAgent
from google.adk.models.google_llm import Gemini
from google.adk.runners import InMemoryRunner
from google.adk.tools.tool_context import ToolContext
from typing import Dict, Any
import asyncio

# Create a simple tool
def example_tool(tool_context: ToolContext, data: str) -> Dict[str, Any]:
    """A simple tool that saves to session state."""
    tool_context.state["result"] = data
    return {"status": "success"}

# Create agents with tools
agent1 = Agent(
    name="Agent1",
    model=Gemini(model="gemini-2.5-flash"),
    instruction="Analyze the input and call example_tool with your analysis.",
    tools=[example_tool],
    output_key="analysis1"
)

agent2 = Agent(
    name="Agent2",
    model=Gemini(model="gemini-2.5-flash"),
    instruction="Analyze the input and call example_tool with your analysis.",
    tools=[example_tool],
    output_key="analysis2"
)

# Create parallel agent
parallel = ParallelAgent(
    name="ParallelAnalysis",
    sub_agents=[agent1, agent2]
)

# Wrap in sequential agent
pipeline = SequentialAgent(
    name="Pipeline",
    sub_agents=[parallel]
)

# Run
async def test():
    runner = InMemoryRunner(agent=pipeline)
    response = await runner.run_debug("Analyze this data")
    print(response)

asyncio.run(test())

Expected Behavior

The parallel agents should execute successfully and return their results.

Actual Behavior

IndexError: list index out of range
  File ".../google/adk/agents/llm_agent.py", line 449, in _run_async_impl
    or ctx.should_pause_invocation(events[-2])
       ~~~~~~^^^^

Stack Trace

ExceptionGroup: unhandled errors in a TaskGroup (1 sub-exception)
+-+---------------- 1 ----------------
  | Traceback (most recent call last):
  |   File ".../google/adk/agents/parallel_agent.py", line 136, in process_an_agent
  |     async for event in events_for_one_agent:
  |   File ".../google/adk/agents/base_agent.py", line 294, in run_async
  |     async for event in agen:
  |   File ".../google/adk/agents/llm_agent.py", line 449, in _run_async_impl
  |     or ctx.should_pause_invocation(events[-2])
  |                                    ~~~~~~^^^^
  | IndexError: list index out of range

Impact

  • Severity: High
  • Affected: Any use of LlmAgent with tools inside ParallelAgent (at least in my testing)
  • Workaround: Use SequentialAgent instead of ParallelAgent (loses parallel execution benefits)

Tested Fix

Tested the proposed fix.

Related

This may be related to the resumability feature (ctx.is_resumable) and how events are tracked during parallel execution?

Metadata

Metadata

Labels

core[Component] This issue is related to the core interface and implementationtools[Component] This issue is related to tools

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions