-
Notifications
You must be signed in to change notification settings - Fork 1.9k
feat: Add user_id property to ReadonlyContext #2875
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
feat: Add user_id property to ReadonlyContext #2875
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Summary of Changes
Hello @ammmr, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!
This pull request enhances the ReadonlyContext by adding a user_id property. This provides a direct and standardized way for agents, plugins, and callbacks to access the current user's identifier, which is crucial for implementing user-specific functionalities, improving logging, and streamlining session management. The change is additive and maintains backward compatibility.
Highlights
- Direct User ID Access: Introduces a user_id property to the ReadonlyContext class, enabling direct access to the user's ID from within agent contexts, plugins, and callbacks. This addresses current limitations where user_id was not easily accessible.
- Enhanced Observability and User-Scoped Operations: This change facilitates user-specific logging, tracing, and operations, improving debugging and monitoring capabilities. It also simplifies the implementation of features requiring user-level configurations or resources.
- Non-Breaking and Backward Compatible: The addition of the user_id property is an additive change, ensuring backward compatibility and a smooth integration without impacting existing functionalities.
Using Gemini Code Assist
The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.
Invoking Gemini
You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command>
or @gemini-code-assist <command>
. Below is a summary of the supported commands.
Feature | Command | Description |
---|---|---|
Code Review | /gemini review |
Performs a code review for the current pull request in its current state. |
Pull Request Summary | /gemini summary |
Provides a summary of the current pull request in its current state. |
Comment | @gemini-code-assist | Responds in comments when explicitly tagged, both in issue comments and review comments. |
Help | /gemini help |
Displays a list of available commands. |
Customization
To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/
folder in the base of the repository. Detailed instructions can be found here.
Limitations & Feedback
Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.
You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.
Footnotes
-
Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution. ↩
Response from ADK Triaging Agent Hello @ammmr, thank you for creating this PR! To help us review this PR, could you please create and associate a GitHub issue with this PR? Also, could you please add a This information will help reviewers to review your PR more efficiently. Thanks! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review
This pull request introduces a user_id
property to the ReadonlyContext
, which is a valuable addition for user-specific logging and operations. The change is well-motivated and includes a corresponding unit test. My review includes a suggestion to enhance the robustness of this new property by handling cases where a user_id
might not be present, which is a common scenario in user-based systems. By typing the return value as Optional[str]
and expanding the test coverage, we can ensure the new API is more resilient and prevent potential runtime errors.
@@ -52,3 +52,8 @@ def agent_name(self) -> str: | |||
def state(self) -> MappingProxyType[str, Any]: | |||
"""The state of the current session. READONLY field.""" | |||
return MappingProxyType(self._invocation_context.session.state) | |||
|
|||
@property | |||
def user_id(self) -> str: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The user_id
property is typed to return a str
. While the underlying InvocationContext
also types it as str
, it's common for user identifiers to be optional (e.g., for anonymous sessions). If _invocation_context.user_id
were to return None
, this property would violate its type hint, potentially leading to runtime errors in client code.
To make the API more robust, I suggest changing the return type to Optional[str]
. This makes it explicit that user_id
can be missing and requires consumers to handle this case.
def user_id(self) -> str: | |
def user_id(self) -> Optional[str]: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for the feedback. I understand your concern about the robustness of the API. However, I believe the current implementation with str
return type is correct based on the codebase design.
Looking at the Session
base class and its implementations (specifically InMemorySessionService
), the user_id
is a required field that cannot be None
:
- The
Session
base class definesuser_id
as a non-optionalstr
field - In
InMemorySessionService
, sessions are created with a requireduser_id
parameter (line 106 insrc/google/adk/sessions/in_memory_session_service.py
) - The
InvocationContext
that wraps the session also treatsuser_id
as a requiredstr
This architectural decision means that anonymous sessions are not supported in the current design - every session must have an associated user_id. Making it Optional[str]
would imply that anonymous sessions are possible, which would be misleading given the current implementation constraints.
If anonymous sessions become a requirement in the future, it would need to be a broader architectural change affecting the Session
base class and all its implementations, not just this readonly accessor.
Therefore, keeping the return type as str
accurately reflects the current API contract and ensures type consistency throughout the codebase.
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:
Changes made:
user_id
property to theReadonlyContext
class insrc/google/adk/agents/readonly_context.py
Impact:
This change will:
Before:
After:
This is a non-breaking change that adds a new readonly property to the existing ReadonlyContext interface.