adk-python icon indicating copy to clipboard operation
adk-python copied to clipboard

AttributeError when using the GlobalInstructionPlugin with a Runner

Open henrywang0314 opened this issue 1 year ago • 4 comments

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())

henrywang0314 avatar Oct 14 '25 07:10 henrywang0314

Hello, While migrating related parts of GlobalInstructionPlugin, I encountered this error and have created this PR to address it. Although using a variable prefixed with an underscore (_) is not ideal, it seems to be the most straightforward and practical solution in this case.

UlookEE avatar Oct 15 '25 13:10 UlookEE

Hi thank you for pointing out this error, we merged @UlookEE . If this does not fix this problem please reopen the issue!

GWeale avatar Nov 03 '25 23:11 GWeale

I'm seeing an error while use this plugin 2025-11-09 23:56:42,792 - ERROR - google_adk.google.adk.cli.adk_web_server - Failed to load plugin <google.adk.plugins.global_instruction_plugin.GlobalInstructionPlugin object at 0x1436f28d0>: 'GlobalInstructionPlugin' object has no attribute 'rsplit'

adityapandey216 avatar Nov 09 '25 18:11 adityapandey216

@adityapandey216 I don't think this is an implementation error in the plugin.

UlookEE avatar Nov 10 '25 00:11 UlookEE