Skip to content

Missing injected prompt in conversation context, prompt injected using before_model_callback method #3138

@zm003

Description

@zm003

Describe the bug
Extra prompt injection is implemented using before_model_callback method. However, this prompt injection will disappear whenever user giving new input to the LlmAgent.

For example:

# At t=0
==New Input==
-> User input: Hi
-> Before model callback: Ignore this user input: Index=0
-> Model response: Hi, how can I help you
# At t=1
==Conversation context==
-> User input: Hi
-> Model response: Hi, how can I help you?

==New Input==
-> User input: How are you?
-> Before model callback: Ignore this user input: Index=1`
-> Model response: I am fine. How about you? 

As you can see, the injected prompt("Ignore this user input: Index=0") by before_model_callback at t=0 is missing in the conversation context at t=1.

To Reproduce
Put this before_model_callback to LlmAgent

from google.adk.agents.callback_context import CallbackContext
from google.adk.models import LlmRequest, LlmResponse
from google.genai import types

index = 0
def before_model_callback(callback_context: CallbackContext, llm_request: LlmRequest) -> LlmRequest | None:
    """Inspects/modifies the callback context and LLM request before it's sent.

    Args:
        callback_context (CallbackContext): Model callback context.
        llm_request (LlmRequest): LLM request.
    """
    global index
    curr_context = callback_context.state.to_dict()
    logger.info(f"[Before Model callback] Current context: {curr_context}")
    llm_request.contents.append(
        types.Content(role="user", parts=[types.Part(text=f"Ignore this user input: Index={index}")])
    )
    index += 1

    return

Expected behavior
The prompt inserted by before_model_callback should remain accessible within the conversation context at any point in time.

Screenshots
t=0
Image

t=1
Image

Desktop (please complete the following information):

  • OS: [e.g. macOS, Linux, Windows]: macOS
  • Python version(python -V): 3.11.8
  • ADK version(pip show google-adk): 1.15.1

Model Information:

  • Are you using LiteLLM: Yes
  • Which model is being used: gpt-4o

Metadata

Metadata

Assignees

Labels

core[Component] This issue is related to the core interface and implementation

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions