Skip to content

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

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 3 commits into
base: develop
Choose a base branch
from

Conversation

andersonlemesc
Copy link

@andersonlemesc andersonlemesc commented Jun 3, 2025

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

Copy link

sourcery-ai bot commented Jun 3, 2025

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
Loading

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
Loading

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

@andersonlemesc andersonlemesc force-pushed the fix/websocket-format-error branch 2 times, most recently from d1b35cb to 242421a Compare June 5, 2025 01:56
- Fix original websocket format error
- Add robust model validation for all agent types
- Prevent empty model strings in LiteLLM calls
- Update Pydantic V2 compatibility (dict() -> model_dump())
- Improve error handling in workflow agents
- Add comprehensive logging and validation
@andersonlemesc andersonlemesc force-pushed the fix/websocket-format-error branch from 242421a to d4618fa Compare June 5, 2025 02:01
…ints

- Update config_type validation to accept ['studio', 'sse'] instead of ['studio', 'custom']
- Update type validation to accept ['official', 'community'] instead of ['official', 'custom']
- Resolves ResponseValidationError when listing MCP servers
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.

1 participant