Skip to content

AttributeError when using the GlobalInstructionPlugin with a Runner #3163

@henrywang0314

Description

@henrywang0314

Describe the bug

When using the GlobalInstructionPlugin with a Runner, an AttributeError is raised during the before_model_callback. The error message, 'CallbackContext' object has no attribute 'invocation_context', indicates that the plugin is trying to access an attribute that does not exist on the context object provided by the runner's execution flow.


To Reproduce

Steps to reproduce the behavior:

  1. Initialize a Runner and include GlobalInstructionPlugin in the plugins list.
  2. Call the runner.run_async() method to start an agent execution.
  3. The following error occurs.

Code:

# Simplified reproduction code based on provided snippets
from google.adk.runners import Runner
from google.adk.plugins import GlobalInstructionPlugin
# ... other necessary imports for agent, services, etc.

async def get_runner_async():
    """Returns a runner configured with the GlobalInstructionPlugin."""
    # Assume root_agent and services are defined elsewhere
    root_agent = ... 
    artifact_service = ...
    session_service = ...
    memory_service = ...
    
    runner = Runner(
        app_name="my_app",
        agent=root_agent,
        artifact_service=artifact_service,
        session_service=session_service,
        memory_service=memory_service,
        plugins=[
            GlobalInstructionPlugin(
                global_instruction="This is a global instruction."
            )
        ]
    )
    return runner

async def main():
    """Main execution function to demonstrate the bug."""
    runner = await get_runner_async()
    
    # The error occurs when run_async is called and the plugin's callback is triggered
    async for event in runner.run_async(
        user_id="test_user",
        session_id="test_session",
        new_message="Hello, agent!",
    ):
        print(event)

# To run:
# import asyncio
# asyncio.run(main())

Metadata

Metadata

Assignees

Labels

agent engine[Component] This issue is related to Agent Engine deployment

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions