From 7cf0d0135d195fbb5f40aae8071cfba2fa40dfb3 Mon Sep 17 00:00:00 2001
From: Echolonius <273862755+Echolonius@users.noreply.github.com>
Date: Sat, 28 Nov 2026 22:39:02 -0600
Subject: [PATCH] fix(tools): inspect latest non-transient content in
LoadArtifactsTool
---
src/google/adk/tools/load_artifacts_tool.py | 18 +-
.../tools/test_load_artifacts_tool.py | 170 ++++++++++++++++++
2 files changed, 186 insertions(+), 2 deletions(-)
diff --git a/src/google/adk/tools/load_artifacts_tool.py b/src/google/adk/tools/load_artifacts_tool.py
index b38558e14ba..da55b66db15 100644
--- a/src/google/adk/tools/load_artifacts_tool.py
+++ b/src/google/adk/tools/load_artifacts_tool.py
@@ -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 {}
diff --git a/tests/unittests/tools/test_load_artifacts_tool.py b/tests/unittests/tools/test_load_artifacts_tool.py
index bff64c68378..8c0e38fc16b 100644
--- a/tests/unittests/tools/test_load_artifacts_tool.py
+++ b/tests/unittests/tools/test_load_artifacts_tool.py
@@ -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='Cedar')],
+ )
+ ])
+
+ 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='...')],
+ )
+ ])
+
+ await load_artifacts_tool.process_llm_request(
+ tool_context=tool_context, llm_request=llm_request
+ )
+
+ assert tool_context.loaded_names == []