fix(agent_builder): correct sub-agent tuple return format
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
- WebSocket Connection Issues: Sub-agents now returned in correct tuple format ✅
-
LLM Model Validation: Prevents
LiteLLM "provider not provided"errors ✅ - Agent Type Handling: Workflow agents no longer incorrectly receive model parameters ✅
- Pydantic Compatibility: Fixed deprecation warnings for V2 ✅
Changes Made
- Updated
_get_sub_agentsto return a list of tuples, each containing aLlmAgentand anOptional[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_agentsto 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
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 |
|
src/services/adk/agent_builder.py |
Tips and commands
Interacting with Sourcery
-
Trigger a new review: Comment
@sourcery-ai reviewon 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 issueto create an issue from it. -
Generate a pull request title: Write
@sourcery-aianywhere in the pull request title to generate a title at any time. You can also comment@sourcery-ai titleon the pull request to (re-)generate the title at any time. -
Generate a pull request summary: Write
@sourcery-ai summaryanywhere in the pull request body to generate a PR summary at any time exactly where you want it. You can also comment@sourcery-ai summaryon the pull request to (re-)generate the summary at any time. -
Generate reviewer's guide: Comment
@sourcery-ai guideon the pull request to (re-)generate the reviewer's guide at any time. -
Resolve all Sourcery comments: Comment
@sourcery-ai resolveon 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 dismisson 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 reviewto 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.