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

tool_context._invocation_context.session.user_id returns the string default_user_id

Open ldessemon2 opened this issue 1 year ago • 6 comments

🐛 Bug Report


Describe the bug

The check_tableau_access function, which is designed to retrieve the current user's ID (email) from the ToolContext to perform an access check, is receiving the hardcoded string "default_user_id" instead of the actual user email when called from Google Agentspace.

The relevant line in the agent.py file is:

user_id = tool_context._invocation_context.session.user_id

When this agent is invoked via Agentspace, tool_context._invocation_context.session.user_id returns "default_user_id" instead of the connected user's email (e.g., [email protected]). This prevents the tool from impersonating the correct user for Tableau Cloud access checks.


To Reproduce

A minimal agent configuration and the failing function are provided in the uploaded agent.py file.

Steps to reproduce the behavior:

  1. Define the Agent: Use the provided agent.py file to define and deploy the TableauAccessAgent in Google Agentspace. The agent includes the check_tableau_access tool.
  2. Invoke the Tool: In the Agentspace chat interface, enter the prompt: "Do I have access to Tableau".
  3. Check Logs: Observe the print statements or execution logs for the check_tableau_access function, specifically the lines:
    user_id = tool_context._invocation_context.session.user_id
    print(f"Connexion en tant qu'utilisateur: {user_id}")
    
  4. Observe Error: The logged user ID will be Connexion en tant qu'utilisateur: default_user_id instead of the expected user email (e.g., [email protected]). This ultimately leads to the Tableau access check failing or checking the wrong user's permissions.

Expected behavior

When the check_tableau_access function is called via an Agentspace invocation, the expression tool_context._invocation_context.session.user_id should return the actual email address of the user who is signed into Google Agentspace and interacting with the agent (in this case, [email protected] as mentioned in the agent instructions, or the general connected user's email).


Screenshots

Not applicable, as this is an internal variable/log issue.


Desktop (please complete the following information):

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

Model Information:

  • Are you using LiteLLM: No
  • Which model is being used: gemini-2.5-flash

Additional context

The agent instructions specify the user:

instruction="""
Vous êtes un assistant qui vérifie l'accès des utilisateurs à Tableau Cloud.
L'utilisateur est [email protected]
"""

However, the system is attempting to retrieve the user's ID dynamically from the invocation context:

def check_tableau_access(tool_context: ToolContext) -> str:
    try:
        user_id = tool_context._invocation_context.session.user_id
        # ... rest of the function ...

The issue seems to be that the Agentspace environment is not correctly populating the tool_context._invocation_context.session.user_id field with the authenticated user's ID. This may indicate an issue with how the Agentspace/ADK runtime environment handles user session context for tool execution.

ldessemon2 avatar Oct 05 '25 11:10 ldessemon2

Facing same issue

ssingh5bmcd avatar Oct 06 '25 11:10 ssingh5bmcd

I am using it like this, and works perfectly fine for me. You may be not be setting the user_id in the callback_context.state correctly or there might me different session during agent query.

invocation_context = tool_context._invocation_context
current_session_id = invocation_context.session.id if invocation_context and invocation_context.session else "unknown_session"
user_id = invocation_context.user_id if invocation_context and invocation_context.user_id else "unknown_user"

akshatshaw avatar Oct 09 '25 09:10 akshatshaw

Thanks for messaging! In our repro the ADK side is behaving as expected invocation_context.user_id mirrors whatever user_id was used when the Agentspace runtime created or resumed the session. Agentspace is giving the placeholder "default_user_id", so every downstream lookup (including check_tableau_access) takes that value. Once we take the signed-in user’s email into the request we send to ADk like RunAgentRequest.user_id/create_session(..., user_id=…) is set to the real principal, the tool sees the correct email with no additional changes. Does this help?

GWeale avatar Oct 10 '25 18:10 GWeale

Hi @akshatshaw Thanks What do you mean by "You may be not be setting the user_id in the callback_context.state correctly or there might me different session during agent query" ? Is it working on your side with Agentspace ? If yes, did you do something in particular on agentspace side ? Do you have a full example to share ?

ldessemon2 avatar Oct 10 '25 19:10 ldessemon2

Hi @GWeale Thanks So it's looks like a bug on Agentspace side. It's a black box, the request to ADK is handled by Agentspace . I opened a case

ldessemon2 avatar Oct 10 '25 20:10 ldessemon2

Okay, thank you for looking in this and thank you @akshatshaw for initially addressing this issue, I will close this. If you have any other issue with this feel free to reopen!

GWeale avatar Oct 13 '25 16:10 GWeale