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
18 changes: 16 additions & 2 deletions src/google/adk/tools/load_artifacts_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -529,10 +529,24 @@ async def _append_artifacts_to_llm_request(

# Attach the content of the artifacts if the model requests them.
# This only adds the content to the model request, instead of the session.
if llm_request.contents and llm_request.contents[-1].parts:
# Transient user contents (e.g. from PreloadMemoryTool) may have been
# appended after the function response in llm_request.contents, so inspect
# the latest non-transient content instead of assuming contents[-1].
transient_content_ids = {
id(c) for c in getattr(llm_request, '_transient_user_contents', ())
}
last_content = next(
(
c
for c in reversed(llm_request.contents)
if id(c) not in transient_content_ids
),
None,
)
if last_content and last_content.parts:
available_names = set(artifact_names)
withheld_names = raw_names - available_names
for part in llm_request.contents[-1].parts:
for part in last_content.parts:
function_response = part.function_response
if function_response and function_response.name == 'load_artifacts':
response = function_response.response or {}
Expand Down
170 changes: 170 additions & 0 deletions tests/unittests/tools/test_load_artifacts_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -1788,3 +1788,173 @@ async def test_load_artifacts_process_artifact_names_no_withheld_fallback():
)
assert 'user:doc.txt' not in tool_context.loaded_names
assert len(llm_request.contents) == 1


@pytest.mark.asyncio
async def test_load_artifacts_after_transient_user_content():
"""Artifacts still load when transient user content follows function_response."""
artifact_name = 'schedule.csv'
artifact = types.Part.from_text(text='club,day,time,room\nreading,Thu,18:45,Maple\n')

tool_context = _StubToolContext({artifact_name: artifact})
llm_request = LlmRequest(
contents=[
types.Content(
role='user',
parts=[types.Part.from_text(text='Prepare reminder')],
),
types.Content(
role='model',
parts=[
types.Part.from_function_call(
name='load_artifacts',
args={'artifact_names': [artifact_name]},
)
],
),
types.Content(
role='user',
parts=[
types.Part(
function_response=types.FunctionResponse(
name='load_artifacts',
response={'artifact_names': [artifact_name]},
)
)
],
),
]
)
llm_request._insert_transient_user_content([
types.Content(
role='user',
parts=[types.Part.from_text(text='<PAST_CONVERSATIONS>Cedar</PAST_CONVERSATIONS>')],
)
])

await load_artifacts_tool.process_llm_request(
tool_context=tool_context, llm_request=llm_request
)

assert tool_context.loaded_names == [artifact_name]
assert llm_request.contents[-1].parts[0].text == f'Artifact {artifact_name} is:'
assert 'Maple' in llm_request.contents[-1].parts[1].text


@pytest.mark.asyncio
async def test_load_artifacts_with_preload_memory_tool_first():
"""PreloadMemoryTool before LoadArtifactsTool does not suppress artifact loading."""
from google.adk.memory.base_memory_service import SearchMemoryResponse
from google.adk.memory.memory_entry import MemoryEntry
from google.adk.tools.preload_memory_tool import PreloadMemoryTool

class _MemoryAndArtifactToolContext(_StubToolContext):

def __init__(self, artifacts_by_name: dict[str, types.Part]):
super().__init__(artifacts_by_name)
self.user_content = types.Content(
role='user',
parts=[types.Part.from_text(text='Prepare reminder')],
)

async def search_memory(self, query: str) -> SearchMemoryResponse:
del query
return SearchMemoryResponse(
memories=[
MemoryEntry(
author='user',
content=types.Content(
role='user',
parts=[types.Part.from_text(text='Fallback: Cedar Room')],
),
)
]
)

artifact_name = 'schedule.csv'
artifact = types.Part.from_text(text='Maple Room')
tool_context = _MemoryAndArtifactToolContext({artifact_name: artifact})
llm_request = LlmRequest(
contents=[
tool_context.user_content,
types.Content(
role='model',
parts=[
types.Part.from_function_call(
name='load_artifacts',
args={'artifact_names': [artifact_name]},
)
],
),
types.Content(
role='user',
parts=[
types.Part(
function_response=types.FunctionResponse(
name='load_artifacts',
response={'artifact_names': [artifact_name]},
)
)
],
),
]
)

await PreloadMemoryTool().process_llm_request(
tool_context=cast(ToolContext, tool_context), llm_request=llm_request
)
await load_artifacts_tool.process_llm_request(
tool_context=cast(ToolContext, tool_context), llm_request=llm_request
)

all_texts = [
part.text
for content in llm_request.contents
for part in (content.parts or [])
if part.text
]
assert any('Cedar Room' in t for t in all_texts)
assert any('Maple Room' in t for t in all_texts)


@pytest.mark.asyncio
async def test_load_artifacts_skips_prior_turn_when_new_user_turn_has_transient_content():
"""Does not re-load an earlier turn's load_artifacts on a subsequent user turn."""
artifact_name = 'schedule.csv'
artifact = types.Part.from_text(text='Maple Room')
tool_context = _StubToolContext({artifact_name: artifact})
llm_request = LlmRequest(
contents=[
types.Content(
role='user',
parts=[
types.Part(
function_response=types.FunctionResponse(
name='load_artifacts',
response={'artifact_names': [artifact_name]},
)
)
],
),
types.Content(
role='model',
parts=[types.Part.from_text(text='Here is the schedule.')],
),
types.Content(
role='user',
parts=[types.Part.from_text(text='Thanks!')],
),
]
)
llm_request._insert_transient_user_content([
types.Content(
role='user',
parts=[types.Part.from_text(text='<PAST_CONVERSATIONS>...</PAST_CONVERSATIONS>')],
)
])

await load_artifacts_tool.process_llm_request(
tool_context=tool_context, llm_request=llm_request
)

assert tool_context.loaded_names == []