The Problem
I am working on a workflow where the initial user input cannot be passed as a simple static string. Instead, it requires heavy preprocessing beforehand, such as reading, parsing, and concatenating text from a multi-page PDF file.
Because of this, I need a way to pass this PREPROCESSED_USER_PROMPT into my root agent when the execution starts.
Migration Notice & My Assumptions
I noticed the ADK 2.0 migration guide mentions that Agents now subclass BaseNode and run within the new Workflow Graph engine:
Agents are now evaluated as individual nodes within the new Workflow Graph engine. Custom overrides of 1.x abstract methods... are no longer the correct way to drive execution. The Workflow Graph engine completely bypasses these legacy overrides.
Based on this, I suppose that directly driving the agent via something like root_agent.run(PREPROCESSED_USER_PROMPT) is no longer supported or aligned with the 2.0 architecture.
My Current Workaround
To get around this, I am currently leveraging before_model_callback to manually intercept and mutate the request payload right before it hits the LLM:
def simple_before_model_modifier(
callback_context: CallbackContext, llm_request: LlmRequest
) -> Optional[LlmResponse]:
# Intercept and overwrite the last user message with preprocessed data
if llm_request.contents and llm_request.contents[-1].role == "user":
if llm_request.contents[-1].parts:
llm_request.contents[-1].parts[0].text = PREPROCESSED_USER_PROMPT
root_agent = LlmAgent(
name="InitialWriterAgent",
model=GEMINI_MODEL,
include_contents="none",
instruction="""
You are a Creative Writing Assistant tasked with starting a story.
...
Topic: {initial_topic}
""",
output_key=STATE_CURRENT_DOC,
before_model_callback=simple_before_model_modifier,
)
Questions
- Is my current workaround utilizing
before_model_callback the expected way to handle this in 2.0?
- Are there other, more canonical patterns or best practices in ADK 2.0 for injecting preprocessed user data before a node executes (e.g., passing it through
CallbackContext.state at graph initialization)?
Would love to get some guidance on the recommended approach. Thanks!
The Problem
I am working on a workflow where the initial user input cannot be passed as a simple static string. Instead, it requires heavy preprocessing beforehand, such as reading, parsing, and concatenating text from a multi-page PDF file.
Because of this, I need a way to pass this
PREPROCESSED_USER_PROMPTinto my root agent when the execution starts.Migration Notice & My Assumptions
I noticed the ADK 2.0 migration guide mentions that Agents now subclass
BaseNodeand run within the new Workflow Graph engine:Based on this, I suppose that directly driving the agent via something like
root_agent.run(PREPROCESSED_USER_PROMPT)is no longer supported or aligned with the 2.0 architecture.My Current Workaround
To get around this, I am currently leveraging
before_model_callbackto manually intercept and mutate the request payload right before it hits the LLM:Questions
before_model_callbackthe expected way to handle this in 2.0?CallbackContext.stateat graph initialization)?Would love to get some guidance on the recommended approach. Thanks!