Skip to content

Added iterator agent for ADK pipeline #1611

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

vivek100
Copy link

Add ArrayIteratorAgent for Array Iteration Functionality

Summary
Implements ArrayIteratorAgent to provide built-in array iteration functionality for the ADK. This agent allows developers to iterate over arrays in session state and pass each element to a sub-agent for processing, addressing a common workflow pattern that was previously missing from the ADK.

Changes Made

  • ✅ Added ArrayIteratorAgent class in src/google/adk/agents/array_iterator_agent.py
  • ✅ Implemented single sub-agent constraint with validation
  • ✅ Added support for nested array key access using dot notation (e.g., user_data.customers)
  • ✅ Implemented automatic result collection from sub-agent's output_key
  • ✅ Added comprehensive unit tests in tests/unittests/agents/test_array_iterator_agent.py
  • ✅ Updated agent exports and imports for proper integration
  • ✅ Added documentation and examples

Key Features

  1. Single Sub-Agent Pattern: Enforces exactly one sub-agent to keep iteration focused and simple
  2. Nested State Access: Supports dot notation for accessing nested arrays (app:config.items, user.data.list)
  3. Automatic Collection: Collects results from sub-agent's output_key into an array
  4. Configurable Context: Passes each array element via configurable item_key (default: current_item)
  5. Optional Limits: Supports max_items parameter for processing subsets
  6. Robust Error Handling: Validates array existence, sub-agent count, and handles edge cases

Usage Example

# Sub-agent processes individual items
processor = LlmAgent(
    name="item_processor", 
    instruction="Process: {current_item}",
    output_key="result"
)

# Array iterator
iterator = ArrayIteratorAgent(
    name="batch_processor",
    array_key="items",
    output_key="all_results", 
    sub_agents=[processor]
)

Testing

  • ✅ Unit tests cover all core functionality
  • ✅ Edge cases tested (empty arrays, missing keys, validation errors)
  • ✅ Integration with existing ADK patterns verified
  • ✅ Nested state access thoroughly tested
  • ✅ Error handling and validation confirmed

Breaking Changes
None - this is a new feature addition that doesn't modify existing functionality.

Documentation

  • Added comprehensive docstrings following Google style
  • Included usage examples in class documentation
  • Added type hints for all parameters and methods

Fixes #1610

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Option to iterate over an array and generated array response
1 participant