Repository navigation
Description
🔴 Required Information
Describe the Bug:
c6586f1 turned DYNAMIC_INSTRUCTION_ROUTING on by default. Instructions that tools add (load_artifacts, load_mcp_resource) now go into a user-role content instead of system_instruction. Some code treats the last user-role content as something the user said, so that content is now read as user input:
-
Live: the model speaks before the user does. Take an agent with
load_artifactsand a session that already has an artifact. Onrun_live, the routed instruction is the only content in the request, soGeminiLlmConnection.send_historysends it as history withturn_complete = contents[-1].role == 'user', which isTrue(gemini_llm_connection.py:94). The model is told the user's turn is over and starts answering before the user has said anything. On Gemini 3.x Live, ADK also sends the'.'placeholder that forces a reply (line 110). With routing off, the instruction stays insystem_instructionand no history is sent. -
ModelArmorPlugin screens ADK's instruction instead of the user's prompt. On a step that continues after a tool call,
_insert_transient_user_contentputs the routed instruction after the function response (llm_request.py:360-374), so it is the last user content._latest_user_text_content(model_armor/_plugin.py:365) then picks the instruction. Model Armor screens framework text, can rewrite it if de-identification is on, or block the request if it flags something in the artifact list. The user's prompt isn't screened on that step. -
Interactions API: the instruction is sent as user input on every chained turn. With
previous_interaction_id, only the latest user contents are sent, and the routed instruction is now one of them. So each turn sends[instruction, user message]as input steps, while before it sent just the user message with the list in the system instruction. I tested this withgenerate_content_via_interactionspatched, not against the real API. My guess is that the older artifact lists then build up in the server-side chain, but I haven't confirmed it.
The commit message says the content is "placed before the current user turn". That's true for a turn that starts with a user message. It isn't true on tool-continuation steps (2), or when the user hasn't said anything yet (1).
Agents that set both static_instruction and instruction already sent their dynamic instruction this way before this commit. The change extends it to every agent that uses load_artifacts or MCP resources.
Steps to Reproduce:
- Check out main (b916291)
- Run either script below, then again with
ADK_DISABLE_DYNAMIC_INSTRUCTION_ROUTING=1
Expected Behavior:
- A new live session with no user input doesn't make the model respond.
- Model Armor screens the user's prompt on every model call.
- Interactions turns send only the user's new input.
Observed Behavior:
Live, routing on (default):
system_instruction_has_artifact_list: False
send_client_content: [([('user', 'The text between <<<BEGIN_SYSTEM_INSTRUCTION>>> and <<<END_S')], True)]
Live, ADK_DISABLE_DYNAMIC_INSTRUCTION_ROUTING=1:
system_instruction_has_artifact_list: True
(no send_client_content call)
Model Armor, routing on:
model call 1: Model Armor screened -> "What's the weather in SF?"
model call 2: Model Armor screened -> 'The text between <<<BEGIN_SYSTEM_INSTRUCTION>>> and <<<END_SYSTEM_INST'
With routing off, both calls screen the user's prompt.
Environment Details:
- ADK Library Version: main at b916291
- Desktop OS: macOS
- Python Version: 3.12.13
Model Information:
- Are you using LiteLLM: No
- Which model is being used: a fake live session through the real
GeminiLlmConnection(gemini-live-2.5-flash;gemini-3.1-flash-live-previewalso gets the'.'trigger), andMockModelfrom the unit tests for Model Armor
🟡 Optional Information
Regression:
Yes, since c6586f1 (it worked before that, and works with ADK_DISABLE_DYNAMIC_INSTRUCTION_ROUTING=1).
Minimal Reproduction Code:
Live (no network; a fake Gemini Live session behind the real GeminiLlmConnection):
import asyncio, contextlib
from google.adk.agents import LlmAgent
from google.adk.agents.live_request_queue import LiveRequestQueue
from google.adk.agents.run_config import RunConfig
from google.adk.models.base_llm import BaseLlm
from google.adk.models.gemini_llm_connection import GeminiLlmConnection
from google.adk.runners import InMemoryRunner
from google.adk.tools.load_artifacts_tool import load_artifacts_tool
from google.genai import types
record = {}
class FakeGeminiSession:
def __init__(self): self.closed = asyncio.Event()
async def send_client_content(self, turns=None, turn_complete=None):
record.setdefault('send_client_content', []).append(
([(t.role, (t.parts[0].text or '')[:60]) for t in turns], turn_complete))
async def send_realtime_input(self, **kw):
record.setdefault('send_realtime_input', []).append(kw)
async def send_tool_response(self, **kw): pass
async def receive(self):
await self.closed.wait()
if False: yield None
async def close(self): self.closed.set()
class FakeLive(BaseLlm):
model: str = 'gemini-live-2.5-flash'
async def generate_content_async(self, llm_request, stream=False):
if False: yield None
@contextlib.asynccontextmanager
async def connect(self, llm_request):
record['system_instruction_has_artifact_list'] = (
'You have a list of artifacts' in (llm_request.config.system_instruction or ''))
session = FakeGeminiSession()
try:
yield GeminiLlmConnection(session, model_version=self.model)
finally:
session.closed.set()
async def main():
agent = LlmAgent(name='assistant', model=FakeLive(), instruction='Be helpful.',
tools=[load_artifacts_tool])
runner = InMemoryRunner(agent=agent, app_name='app')
s = await runner.session_service.create_session(app_name='app', user_id='u')
await runner.artifact_service.save_artifact(
app_name='app', user_id='u', session_id=s.id, filename='report.txt',
artifact=types.Part.from_text(text='q3 numbers'))
queue = LiveRequestQueue()
async def consume():
async for _ in runner.run_live(user_id='u', session_id=s.id,
live_request_queue=queue, run_config=RunConfig()):
pass
task = asyncio.create_task(consume())
await asyncio.sleep(0.5) # the user hasn't said anything
queue.close()
try:
await asyncio.wait_for(task, 2)
except Exception:
task.cancel()
for k, v in record.items():
print(f'{k}: {v}')
asyncio.run(main())The Model Armor case: an agent with load_artifacts and one plain tool, a saved artifact, and a MockModel that calls the tool and then answers. Patch ModelArmorPlugin._sanitize_user_prompt to record the text it gets. I can paste that script too.
Additional Context:
I'm not sure what the intended fix is, since it's a design choice, so here are a few options:
GeminiLlmConnection.send_history(or the live flow) could leave routed instruction contents out of theturn_completedecision. Or live could keep tool instructions insystem_instruction, since live sessions have no prefix cache to protect.- ModelArmorPlugin could skip contents that are routed instructions. A marker on those contents would let any plugin tell them apart from user input.
- The Interactions path could send the routed instruction as a system instruction or leave it out of the chained input.
How often has this issue occurred?:
- Always (100%)
Activity
fix: keep routed tool instructions from being read as user input
surajksharma07 commentedon Nov 19, 2026
@AkshayShah03. Reproduced both the Live and Model Armor cases on main. Since c6586f1 landed after v2.14.1, released versions only hit this if routing is turned on explicitly.
Until this is sorted, setting ADK_DISABLE_DYNAMIC_INSTRUCTION_ROUTING=1 looks like the safest workaround. I also tried the approach from #8220 locally (system_instruction for live, identity-based skip in Model Armor), and both cases came back clean with routing still on.
Could you check whether that holds up in your setup? If you can, please also share the Model Armor script and confirm the Interactions build-up against the real API.
Since #8220 is already open, please test that branch thoroughly on your end before raising anything new, so we’re not duplicating PRs.
AkshayShah03 commentedon Nov 19, 2026
Thanks @surajksharma07. I ran all three scripts against #8220 (3c323cd) and against main (b916291):
send_client_content: [(['user', 'The text between <<<BEGIN_SYSTEM_INSTRUCTION>>>...'], True)], list not insystem_instructionsystem_instruction_has_artifact_list: True, nosend_client_content'The text between <<<BEGIN_SYSTEM_INSTRUCTION>>>...'"What's the weather in SF?"[instruction, user message]withprevious_interaction_idsetsystem_instructionSo #8220 fixes all three in my setup. I also tried an Interactions
Geminiwrapped inFallbackModel. Thereprevious_interaction_idis never set and the full history is sent every turn, so the instruction isn't stored server-side and #8220'sisinstance(model, Gemini)check doesn't leave a gap there.On the real API: I don't have a key set up, so the Interactions result above comes from patching
interactions_utils.generate_content_via_interactionsand recording what ADK would send. I haven't confirmed the server-side build-up, and I'll leave that claim as unverified in the issue.Model Armor script (no network; it patches the Model Armor client calls and records the text sent for screening):
Run it from the repo root (it uses
MockModelfromtests/unittests/testing_utils.py).surajksharma07 commentedon Nov 20, 2026
Thanks @AkshayShah03 for the full matrix and the Model Armor script, and for marking the Interactions server-side build-up as unverified. One correction to my earlier comment: c6586f1 is in 2.15.0 with routing on by default, so 2.15.0 users with load_artifacts or MCP resources now hit this without opting in. ADK_DISABLE_DYNAMIC_INSTRUCTION_ROUTING=1 is still the workaround until #8220 lands.
Merged #8220 (3c323cd) onto today’s main (c81876a) with no conflicts. Your live script now keeps the artifact list in system_instruction with no send_client_content, and Model Armor screens “What’s the weather in SF?” on call 2 (main still gives the routed instruction for both). The 5 new tests fail on main and pass with the PR. The 4 changed test files pass apart from test_send_to_model_keeps_non_audio_blobs_out_of_audio_cache, which fails the same way on plain main (it looks timing-dependent here).
@DeanChensj could you review #8220 and approve the 2 pending workflows? Since 2.15.0 has this on by default, it would be good to get it into the next release. ContextFilterPlugin, MultimodalToolResultsPlugin and the static_instruction + instruction live path read user contents the same way and are left for a follow-up.
fix: track transient instructions and gate dynamic instruction routing
fix: track transient instructions and gate dynamic instruction routing