mirror of
https://github.com/encounter/adk-python.git
synced 2026-07-09 18:19:28 -07:00
Fixes #423 Related to #1670 - This avoids the `GeneratorExit` error thrown, which would crash OTel metric collection and cause `Failed to detach context` error. - This also allows all function calls are processed when exit_loop is called together with other tools in the same LLmResponse. A sample agent for testing: ``` from google.adk import Agent from google.adk.agents.loop_agent import LoopAgent from google.adk.tools.exit_loop_tool import exit_loop worker_1 = Agent( name='worker_1', description='Worker 1', instruction="""\ Just say job #1 is done. If job #1 is said to be done. Call exit_loop tool.""", tools=[exit_loop], ) worker_2 = Agent( name='worker_2', description='Worker 2', instruction="""\ Just say job #2 is done. If job #2 is said to be done. Call exit_loop tool.""", tools=[exit_loop], ) work_agent = LoopAgent( name='work_agent', description='Do all work.', sub_agents=[worker_1, worker_2], max_iterations=5, ) root_agent = Agent( model='gemini-2.0-flash', name='hello_world_agent', description='hello world agent that can roll a check prime', instruction="""Hand off works to sub agents.""", sub_agents=[work_agent], ) ``` PiperOrigin-RevId: 785538101
150 lines
4.6 KiB
Python
150 lines
4.6 KiB
Python
# Copyright 2025 Google LLC
|
|
#
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
# you may not use this file except in compliance with the License.
|
|
# You may obtain a copy of the License at
|
|
#
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
#
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
# See the License for the specific language governing permissions and
|
|
# limitations under the License.
|
|
|
|
"""Testings for the SequentialAgent."""
|
|
|
|
from typing import AsyncGenerator
|
|
|
|
from google.adk.agents.base_agent import BaseAgent
|
|
from google.adk.agents.invocation_context import InvocationContext
|
|
from google.adk.agents.loop_agent import LoopAgent
|
|
from google.adk.events import Event
|
|
from google.adk.events import EventActions
|
|
from google.adk.sessions.in_memory_session_service import InMemorySessionService
|
|
from google.genai import types
|
|
import pytest
|
|
from typing_extensions import override
|
|
|
|
|
|
class _TestingAgent(BaseAgent):
|
|
|
|
@override
|
|
async def _run_async_impl(
|
|
self, ctx: InvocationContext
|
|
) -> AsyncGenerator[Event, None]:
|
|
yield Event(
|
|
author=self.name,
|
|
invocation_id=ctx.invocation_id,
|
|
content=types.Content(
|
|
parts=[types.Part(text=f'Hello, async {self.name}!')]
|
|
),
|
|
)
|
|
|
|
@override
|
|
async def _run_live_impl(
|
|
self, ctx: InvocationContext
|
|
) -> AsyncGenerator[Event, None]:
|
|
yield Event(
|
|
author=self.name,
|
|
invocation_id=ctx.invocation_id,
|
|
content=types.Content(
|
|
parts=[types.Part(text=f'Hello, live {self.name}!')]
|
|
),
|
|
)
|
|
|
|
|
|
class _TestingAgentWithEscalateAction(BaseAgent):
|
|
|
|
@override
|
|
async def _run_async_impl(
|
|
self, ctx: InvocationContext
|
|
) -> AsyncGenerator[Event, None]:
|
|
yield Event(
|
|
author=self.name,
|
|
invocation_id=ctx.invocation_id,
|
|
content=types.Content(
|
|
parts=[types.Part(text=f'Hello, async {self.name}!')]
|
|
),
|
|
actions=EventActions(escalate=True),
|
|
)
|
|
yield Event(
|
|
author=self.name,
|
|
invocation_id=ctx.invocation_id,
|
|
content=types.Content(
|
|
parts=[types.Part(text=f'I have done my job after escalation!!')]
|
|
),
|
|
)
|
|
|
|
|
|
async def _create_parent_invocation_context(
|
|
test_name: str, agent: BaseAgent
|
|
) -> InvocationContext:
|
|
session_service = InMemorySessionService()
|
|
session = await session_service.create_session(
|
|
app_name='test_app', user_id='test_user'
|
|
)
|
|
return InvocationContext(
|
|
invocation_id=f'{test_name}_invocation_id',
|
|
agent=agent,
|
|
session=session,
|
|
session_service=session_service,
|
|
)
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_run_async(request: pytest.FixtureRequest):
|
|
agent = _TestingAgent(name=f'{request.function.__name__}_test_agent')
|
|
loop_agent = LoopAgent(
|
|
name=f'{request.function.__name__}_test_loop_agent',
|
|
max_iterations=2,
|
|
sub_agents=[
|
|
agent,
|
|
],
|
|
)
|
|
parent_ctx = await _create_parent_invocation_context(
|
|
request.function.__name__, loop_agent
|
|
)
|
|
events = [e async for e in loop_agent.run_async(parent_ctx)]
|
|
|
|
assert len(events) == 2
|
|
assert events[0].author == agent.name
|
|
assert events[1].author == agent.name
|
|
assert events[0].content.parts[0].text == f'Hello, async {agent.name}!'
|
|
assert events[1].content.parts[0].text == f'Hello, async {agent.name}!'
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_run_async_with_escalate_action(request: pytest.FixtureRequest):
|
|
non_escalating_agent = _TestingAgent(
|
|
name=f'{request.function.__name__}_test_non_escalating_agent'
|
|
)
|
|
escalating_agent = _TestingAgentWithEscalateAction(
|
|
name=f'{request.function.__name__}_test_escalating_agent'
|
|
)
|
|
ignored_agent = _TestingAgent(
|
|
name=f'{request.function.__name__}_test_ignored_agent'
|
|
)
|
|
loop_agent = LoopAgent(
|
|
name=f'{request.function.__name__}_test_loop_agent',
|
|
sub_agents=[non_escalating_agent, escalating_agent, ignored_agent],
|
|
)
|
|
parent_ctx = await _create_parent_invocation_context(
|
|
request.function.__name__, loop_agent
|
|
)
|
|
events = [e async for e in loop_agent.run_async(parent_ctx)]
|
|
|
|
# Only two events are generated because the sub escalating_agent escalates.
|
|
assert len(events) == 3
|
|
assert events[0].author == non_escalating_agent.name
|
|
assert events[1].author == escalating_agent.name
|
|
assert events[0].content.parts[0].text == (
|
|
f'Hello, async {non_escalating_agent.name}!'
|
|
)
|
|
assert events[1].content.parts[0].text == (
|
|
f'Hello, async {escalating_agent.name}!'
|
|
)
|
|
assert (
|
|
events[2].content.parts[0].text == 'I have done my job after escalation!!'
|
|
)
|