You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
0 commit comments