Replies: 6 comments
-
|
Response from ADK Answering Agent (experimental, answer may be inaccurate) TLDR: For local testing with Hello! That's a great question, and it touches on a common challenge when developing locally versus in a deployed environment. Here’s a breakdown of the recommended patterns for handling user context with ADK: 1. Handling
|
Beta Was this translation helpful? Give feedback.
-
|
@this-dave thank you for your questions :) You don't have a way to change user_id on ADK Web, as it's hard coded, because adk web is mainly targeted for development / testing tool https://github.com/google/adk-web/blob/3ddf6fabf4da669092e1329d92f6f4a855804d5a/src/app/components/chat/chat.component.ts#L174 but you are able to change user context , i.e. session state by calling the API server to inject some session state yourself:
as suggested by bot answer above, you should have some fallback logic to check permission via session state. BTW on local how do you check permission ? you still connect to a remote DB / server to check permission ? |
Beta Was this translation helpful? Give feedback.
-
|
Hi, as Sean point out, user_id is not something users can override at the moment. However, if you wish to change the state, you can also click the three dots next to the attachment buton in the chat input field. There you can specify what state you want to alter and sending the message along with this will update the state |
Beta Was this translation helpful? Give feedback.
-
|
Hi @this-dave , I am also working on a similar use case where I am trying to fetch the email. |
Beta Was this translation helpful? Give feedback.
-
|
hey @this-dave , when i tried the following code user_email = context.session.user_id It gives me - 'default-user-id'. Is the key for the email changes or do I need to do configure some settings to make the email available in the user_id? |
Beta Was this translation helpful? Give feedback.
-
|
For a newer versions of pytest and adk I found I had to do this. import pytest
import pytest_asyncio
from google.adk.sessions import InMemorySessionService
from google.adk.tools.tool_context import ToolContext
from google.adk.agents.invocation_context import InvocationContext
# assume the agent and tools are in a folder called julian_gregory
from julian_gregory.agent import root_agent
from julian_gregory.tools import decline_all_todays_events
@pytest_asyncio.fixture
async def tool_context_factory():
session_service = InMemorySessionService()
async def _factory(user_id: str | None):
session = await session_service.create_session(app_name="test_app", user_id="test_user")
invocation_context = InvocationContext(
session_service=session_service,
session=session,
invocation_id="123",
agent=root_agent,
)
return ToolContext(invocation_context)
return _factory
@pytest.mark.asyncio
async def test_decline_all_todays_events(tool_context_factory):
auth_user_context = await tool_context_factory("[email protected]")
result = decline_all_todays_events(tool_context=auth_user_context)
print(result)
return None |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I'm developing a multi-agent system where specialist agents need access to the current user's context (specifically their email) for authorization checks. The tools use ToolContext to access context.session.user_id.
When deployed to production (Vertex AI), the ToolContext works perfectly. However, during local development with adk web, the ToolContext appears to be empty or not properly initialized, making it impossible to test authorization flows locally.
Current Setup:
Problem:
When running adk web src/agents/, the ToolContext doesn't contain any session information, causing the authorization logic to fail.
Question:
Is there a way to inject or mock session data into ToolContext for local testing with adk web?
What's the recommended pattern for handling user context in tools that works both in production and local development?
Should we avoid ToolContext for user authentication and use a different approach?
Environment:
Google ADK version: latest
Python 3.13
Testing with: adk web
Any guidance on best practices for local development with user context would be appreciated!
Beta Was this translation helpful? Give feedback.
All reactions