Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions src/google/adk/flows/llm_flows/contents.py
Original file line number Diff line number Diff line change
Expand Up @@ -557,8 +557,10 @@ def _get_current_turn_contents(
# Find the latest event that starts the current turn and process from there
for i in range(len(events) - 1, -1, -1):
event = events[i]
if _should_include_event_in_context(current_branch, event) and (
event.author == 'user' or _is_other_agent_reply(agent_name, event)
if (
_should_include_event_in_context(current_branch, event)
and (event.author == 'user' or _is_other_agent_reply(agent_name, event))
and not _is_direct_transfer(event)
):
return _get_contents(
current_branch,
Expand All @@ -570,6 +572,22 @@ def _get_current_turn_contents(
return []


def _is_direct_transfer(event: Event) -> bool:
'Check whether the event is a direct transfer event.'

return bool(
event.actions.transfer_to_agent
or (
event.content
and event.content.parts
and any(
p.function_call and p.function_call.name == 'transfer_to_agent'
for p in event.content.parts
)
)
)


def _is_other_agent_reply(current_agent_name: str, event: Event) -> bool:
"""Whether the event is a reply from another agent."""
return bool(
Expand Down
87 changes: 87 additions & 0 deletions tests/unittests/flows/llm_flows/test_contents.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,93 @@ async def test_include_contents_none_multi_branch_current_turn():
]


@pytest.mark.asyncio
async def test_events_with_transfer_to_agent_are_include():
"""Test that events with transfer_to_agent are included when include_contents='none'"""
agent = Agent(
model="gemini-2.5-flash", name="test_agent", include_contents="none"
)
llm_request = LlmRequest(model="gemini-2.5-flash")
invocation_context = await testing_utils.create_invocation_context(
agent=agent
)

events = [
Event(
invocation_id="inv1",
author="user",
content=types.UserContent("First user message"),
),
Event(
invocation_id="inv1",
author="parent",
content=types.Content(
parts=[
types.Part(
function_call=types.FunctionCall(
args={"agent_name": "test_agent"},
id="call_inv1",
name="transfer_to_agent",
)
)
],
role="model",
),
),
Event(
invocation_id="inv1",
author="parent",
content=types.Content(
parts=[
types.Part(
function_response=types.FunctionResponse(
id="call_inv1",
name="transfer_to_agent",
response={"result": None},
),
),
],
role="user",
),
actions=EventActions(transfer_to_agent="test_agent"),
),
]

invocation_context.session.events = events
async for _ in contents.request_processor.run_async(
invocation_context, llm_request
):
pass

assert llm_request.contents == [
types.UserContent("First user message"),
types.Content(
parts=[
types.Part(text="For context:"),
types.Part(
text=(
"[parent] called tool `transfer_to_agent` with"
" parameters: {'agent_name': 'test_agent'}"
)
),
],
role="user",
),
types.Content(
parts=[
types.Part(text="For context:"),
types.Part(
text=(
"[parent] `transfer_to_agent` tool returned result:"
" {'result': None}"
)
),
],
role="user",
),
]


@pytest.mark.asyncio
async def test_authentication_events_are_filtered():
"""Test that authentication function calls and responses are filtered out."""
Expand Down