evo-ai icon indicating copy to clipboard operation
evo-ai copied to clipboard

fix(agent_builder): correct sub-agent tuple return format

Open andersonlemesc opened this issue 7 months ago • 1 comments

Summary

This pull request addresses multiple critical issues in agent creation and handling that were causing runtime errors and WebSocket connection problems.

Original Issue (2 days ago)

Fixed an issue in agent_builder.py where the function _get_sub_agents was returning sub-agents without the expected tuple format. This caused a ValueError: too many values to unpack (expected 2) error during WebSocket connection handling, leading to connection closures.

Additional Critical Fixes (Recent)

Extended the PR to address comprehensive LLM model validation and agent type handling issues:

Agent Builder Enhancements

  • ✅ Fix sub-agent tuple return format (original issue)
  • ✅ Add robust model validation for LLM agents to prevent empty model strings
  • ✅ Enhanced error handling and logging throughout agent creation

Workflow Agent Improvements

  • ✅ Remove model requirements for orchestrator agents (workflow, task, sequential, parallel, loop)
  • ✅ Prevent model configuration errors in non-LLM agent types

Schema & Compatibility Updates

  • ✅ Update Pydantic V2 compatibility (dict()model_dump())
  • ✅ Enhanced validation rules for all agent types
  • ✅ Maintain backward compatibility

Problems Solved

  1. WebSocket Connection Issues: Sub-agents now returned in correct tuple format ✅
  2. LLM Model Validation: Prevents LiteLLM "provider not provided" errors ✅
  3. Agent Type Handling: Workflow agents no longer incorrectly receive model parameters ✅
  4. Pydantic Compatibility: Fixed deprecation warnings for V2 ✅

Changes Made

  • Updated _get_sub_agents to return a list of tuples, each containing a LlmAgent and an Optional[AsyncExitStack]
  • Added comprehensive model validation for all agent types
  • Enhanced error handling and logging
  • Fixed Pydantic V2 compatibility issues

Testing

  • ✅ Verified that sub-agents are correctly created and returned in expected format
  • ✅ Tested WebSocket interactions to confirm original error is resolved
  • ✅ All agent types (LLM, Workflow, Task, Sequential, Parallel, Loop) tested and working
  • ✅ No breaking changes to existing functionality
  • ✅ Enhanced error handling verified

Impact

Resolves critical runtime errors and improves overall system stability by ensuring proper agent creation, validation, and execution across all agent types.

Summary by Sourcery

Bug Fixes:

  • Return sub-agents as (agent, exit_stack) tuples in _get_sub_agents to resolve the too-many-values-to-unpack error and avoid WebSocket connection closures
  • Add comprehensive LLM model validation to prevent empty model configuration errors
  • Update Pydantic V2 compatibility and enhance agent type validation

andersonlemesc avatar Jun 03 '25 02:06 andersonlemesc

Reviewer's Guide

This PR fixes the tuple return format in _get_sub_agents by appending sub-agents as (LlmAgent, AsyncExitStack) pairs, resolving unpacking errors during WebSocket handling.

Sequence Diagram: Fix for Sub-Agent Unpacking Error

sequenceDiagram
    participant C as Caller
    participant ASG as agent_builder._get_sub_agents

    C->>ASG: Call _get_sub_agents(...)
    activate ASG
    %% Previously, ASG would prepare a list of LlmAgent objects.
    %% Now, ASG prepares a list of (LlmAgent, Optional[AsyncExitStack]) tuples.
    ASG-->>C: Returns sub_agents_list
    deactivate ASG

    alt Old behavior (sub_agents_list elements were LlmAgent objects)
        C->>C: Attempt to unpack item: agent, stack = item_from_list
        C-->>C: Raises ValueError: too many values to unpack (expected 2)
    else New behavior (sub_agents_list elements are (LlmAgent, Optional[AsyncExitStack]) tuples)
        C->>C: Unpack item: agent, stack = item_from_list
        C-->>C: Successfully unpacks tuple
    end

Class Diagram: Update to _get_sub_agents Return Type in agent_builder

classDiagram
    class agent_builder {
        + _get_sub_agents(...) : List~(LlmAgent, Optional~AsyncExitStack~)~
    }
    class LlmAgent {
      %% Represents an LLM Agent
    }
    class AsyncExitStack {
      %% Represents an asynchronous exit stack, potentially optional
    }
    agent_builder ..> LlmAgent : uses/returns
    agent_builder ..> AsyncExitStack : uses/returns

File-Level Changes

Change Details Files
Return sub-agents as tuples with exit stacks
  • Replaced sub_agents.append(sub_agent) with sub_agents.append((sub_agent, exit_stack))
  • Adjusted the function to include exit_stack in each returned tuple
  • Ensured logging remains accurate after tuple change
src/services/adk/agent_builder.py

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an issue from a review comment by replying to it. You can also reply to a review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull request title to generate a title at any time. You can also comment @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in the pull request body to generate a PR summary at any time exactly where you want it. You can also comment @sourcery-ai summary on the pull request to (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the pull request to resolve all Sourcery comments. Useful if you've already addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull request to dismiss all existing Sourcery reviews. Especially useful if you want to start fresh with a new review - don't forget to comment @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

  • Contact our support team for questions or feedback.
  • Visit our documentation for detailed guides and information.
  • Keep in touch with the Sourcery team by following us on X/Twitter, LinkedIn or GitHub.

sourcery-ai[bot] avatar Jun 03 '25 02:06 sourcery-ai[bot]