Skip to content

Commit a3b4ec6

Browse files
ammmrhangfei
authored andcommitted
feat: Add user_id property to ReadonlyContext
Merge #2875 related: #2876 ## Reason for this change: Currently, there is no direct way to access the user_id from within agent contexts, plugins, or callbacks. This limitation prevents several important use cases: 1. **User-specific logging and tracing**: When debugging or monitoring agent behavior, it's crucial to associate actions with specific users for better observability. 2. **User-scoped operations**: Plugins and callbacks often need to perform user-specific operations, such as accessing user-specific resources or applying user-level configurations. 3. **Session management**: The user_id is a key component of session identification, but it's not accessible through the ReadonlyContext interface, requiring workarounds to access it. ## Changes made: - Added a `user_id` property to the `ReadonlyContext` class in `src/google/adk/agents/readonly_context.py` - The property exposes the user_id from the underlying invocation context as a readonly field ## Impact: This change will: - Enable plugins and callbacks to access the current user's ID directly through the context - Improve logging and tracing capabilities by allowing user-specific tracking - Simplify code that needs to perform user-scoped operations without requiring access to internal implementation details - Maintain backward compatibility as this is an additive change ### Before: - No direct way to access user_id from ReadonlyContext ### After: ```python @Property def user_id(self) -> str: """The id of the user. READONLY field.""" return self._invocation_context.user_id ``` This is a non-breaking change that adds a new readonly property to the existing ReadonlyContext interface. Co-authored-by: Hangfei Lin <hangfei@google.com> COPYBARA_INTEGRATE_REVIEW=#2875 from ammmr:ammmr-add-user_id-property-to-readonly-context 771734e PiperOrigin-RevId: 830170908
1 parent 116b26c commit a3b4ec6

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

src/google/adk/agents/readonly_context.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,11 @@ def session(self) -> Session:
6060
"""The current session for this invocation."""
6161
return self._invocation_context.session
6262

63+
@property
64+
def user_id(self) -> str:
65+
"""The id of the user. READONLY field."""
66+
return self._invocation_context.user_id
67+
6368
@property
6469
def run_config(self) -> Optional[RunConfig]:
6570
"""The run config of the current invocation. READONLY field."""

tests/unittests/agents/test_readonly_context.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1+
# Copyright 2025 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
115
from types import MappingProxyType
216
from unittest.mock import MagicMock
317

@@ -11,6 +25,7 @@ def mock_invocation_context():
1125
mock_context.invocation_id = "test-invocation-id"
1226
mock_context.agent.name = "test-agent-name"
1327
mock_context.session.state = {"key1": "value1", "key2": "value2"}
28+
mock_context.user_id = "test-user-id"
1429
return mock_context
1530

1631

@@ -31,3 +46,8 @@ def test_state_content(mock_invocation_context):
3146
assert isinstance(state, MappingProxyType)
3247
assert state["key1"] == "value1"
3348
assert state["key2"] == "value2"
49+
50+
51+
def test_user_id(mock_invocation_context):
52+
readonly_context = ReadonlyContext(mock_invocation_context)
53+
assert readonly_context.user_id == "test-user-id"

0 commit comments

Comments
 (0)