Skip to content

"ADK Bug: before_model_callback causes AttributeError on Agent Engine" #3789

@Saoussen-CH

Description

@Saoussen-CH

ADK Bug Report: before_model_callback causes AttributeError on Agent Engine

Bug Summary

When deploying an ADK agent with before_model_callback to Vertex AI Agent Engine, the agent fails with:

AttributeError: 'LlmRequest' object has no attribute 'content'

This error occurs in ADK's internal _nl_planning.py:79 file, preventing the agent from processing any requests.

Impact

  • Severity: Critical - Agent cannot process any requests
  • Scope: Affects all agents using before_model_callback on Agent Engine
  • Workaround: None - must disable before_model_callback entirely

Environment

  • ADK Version: google-cloud-aiplatform[adk,agent_engines]>=1.112
  • Python Version: 3.11
  • Deployment Target: Vertex AI Agent Engine (not local runner)
  • Project: project-ddc15d84-7238-4571-a39
  • Location: us-central1

Steps to Reproduce

  1. Create an agent with before_model_callback:
from google.adk.agents import Agent

async def my_callback(callback_context, llm_request):
    """Simple callback that modifies system instruction"""
    print("Callback executed")
    # Any modification here triggers the bug
    return llm_request

agent = Agent(
    name="test_agent",
    model="gemini-2.0-flash-exp",
    instruction="Test agent",
    tools=[],
    before_model_callback=my_callback  # THIS TRIGGERS THE BUG
)
  1. Deploy to Agent Engine:
import vertexai
from vertexai import agent_engines

vertexai.init(project="YOUR_PROJECT", location="us-central1")

remote_app = agent_engines.create(
    agent=agent,
    requirements=["google-cloud-aiplatform[adk,agent_engines]>=1.112"],
    staging_bucket="gs://your-bucket"
)
  1. Query the deployed agent:
response = remote_app.query(user_id="test", message="Hello")
  1. Observe the error in logs:
ERROR: AttributeError: 'LlmRequest' object has no attribute 'content'

Full Error Traceback

Traceback (most recent call last):
  File ".../vertexai/agent_engines/templates/adk.py", line 971, in async_stream_query
    async for event in events_async:
  File ".../google/adk/runners.py", line 454, in run_async
    async for event in agen:
  File ".../google/adk/agents/llm_agent.py", line 460, in _run_async_impl
    async for event in agen:
  File ".../google/adk/flows/llm_flows/base_llm_flow.py", line 346, in run_async
    async for event in agen:
  File ".../google/adk/flows/llm_flows/base_llm_flow.py", line 433, in _run_one_step_async
    async for event in agen:
  File ".../google/adk/flows/llm_flows/base_llm_flow.py", line 510, in _postprocess_async
    async for event in agen:
  File ".../google/adk/flows/llm_flows/base_llm_flow.py", line 657, in _postprocess_run_processors_async
    async for event in agen:
  File ".../google/adk/flows/llm_flows/_nl_planning.py", line 79, in run_async
    or not llm_response.content
           ^^^^^^^^^^^^^^^^^^^^
  File ".../pydantic/main.py", line 1026, in __getattr__
    raise AttributeError(f'{type(self).__name__!r} object has no attribute {item!r}')
AttributeError: 'LlmRequest' object has no attribute 'content'

Root Cause Analysis

In _nl_planning.py:79, the code tries to access llm_response.content:

or not llm_response.content

However, after before_model_callback executes, the variable is actually an LlmRequest object (not LlmResponse), which doesn't have a content attribute.

This appears to be a type confusion bug in ADK's internal flow processing.

Expected Behavior

  • The before_model_callback should execute successfully
  • The agent should continue processing and return a response
  • Memory loading, prompt modification, and other callback use cases should work

Actual Behavior

  • The callback executes successfully
  • ADK's internal _nl_planning.py immediately crashes
  • Agent returns error code 498 with the AttributeError message
  • No response is generated

Use Case

We're trying to implement Memory Bank integration with a custom callback (since PreloadMemoryTool has its own bugs). This is the recommended approach per Google's documentation:

Workaround

None - The only option is to completely disable before_model_callback, which removes the ability to:

  • Load memories before each turn
  • Modify prompts dynamically
  • Implement custom preprocessing logic

Additional Context

  • The bug only occurs on Agent Engine (not local runner)
  • The bug occurs regardless of what the callback does (even an empty callback triggers it)
  • Multiple deployments confirmed the same error
  • Agent Engine resource IDs where bug was observed:
    • projects/773461168680/locations/us-central1/reasoningEngines/1794943936251297792
    • projects/773461168680/locations/us-central1/reasoningEngines/3116750426884538368
    • projects/773461168680/locations/us-central1/reasoningEngines/6440406951883964416

Logs

Cloud Logging query to see the error:

resource.type=aiplatform.googleapis.com/ReasoningEngine
AND resource.labels.reasoning_engine_id="6440406951883964416"
AND severity=ERROR

Request

Please fix the type confusion in _nl_planning.py:79 to properly handle LlmRequest objects returned from before_model_callback.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions