tool_context._invocation_context.session.user_id returns the string default_user_id
🐛 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:
- Define the Agent: Use the provided
agent.pyfile to define and deploy theTableauAccessAgentin Google Agentspace. The agent includes thecheck_tableau_accesstool. - Invoke the Tool: In the Agentspace chat interface, enter the prompt:
"Do I have access to Tableau". - Check Logs: Observe the print statements or execution logs for the
check_tableau_accessfunction, specifically the lines:user_id = tool_context._invocation_context.session.user_id print(f"Connexion en tant qu'utilisateur: {user_id}") - Observe Error: The logged user ID will be
Connexion en tant qu'utilisateur: default_user_idinstead 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.
Facing same issue
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"
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?
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 ?
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
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!