-
Notifications
You must be signed in to change notification settings - Fork 2.8k
fix: Strip timezone for PostgreSQL timestamps in DatabaseSessionService #4365
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?
fix: Strip timezone for PostgreSQL timestamps in DatabaseSessionService #4365
Conversation
Summary of ChangesHello @filipecaixeta, 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 resolves a critical Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Changelog
Activity
Using Gemini Code AssistThe 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 by creating a comment using either
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 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
|
|
Response from ADK Triaging Agent Hello @filipecaixeta, thank you for creating this PR! This PR is a bug fix. Could you please associate the GitHub issue with this PR? If there is no existing issue, could you please create one? In addition, could you please provide logs or a screenshot after the fix is applied? This information will help reviewers to review your PR more efficiently. Thanks! |
PostgreSQL's default TIMESTAMP type is WITHOUT TIME ZONE, which cannot accept timezone-aware datetime objects from Python. This causes a DataError when using asyncpg: "can't subtract offset-naive and offset-aware datetimes". The existing code already handled this for SQLite by stripping the timezone, but PostgreSQL was not handled. This fix applies the same treatment to PostgreSQL. Fixes the regression introduced in commit 1063fa5 which changed from database-generated timestamps (func.now()) to explicit Python datetimes.
0022eb3 to
e275b01
Compare
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 primarily addresses a timezone-related issue with PostgreSQL timestamps, which is a necessary fix. The change in DatabaseSessionService is correct and I've added a minor suggestion for simplification. Additionally, a significant portion of this PR introduces support for thought metadata in part conversions. This seems to be a new feature and is well-tested. I've included a suggestion to refactor some duplicated code in the converter to improve maintainability. It would be beneficial to update the PR description to reflect the addition of the thought feature for better clarity and tracking.
I am having trouble creating individual review comments. Click here to see my feedback.
src/google/adk/a2a/converters/part_converter.py (229-295)
There's significant code duplication in handling the conversion for function_call, function_response, code_execution_result, and executable_code. All these blocks follow the same pattern of creating a metadata dictionary and then a DataPart. This can be refactored to reduce repetition and improve maintainability by determining the part_data and metadata_type first, and then constructing the DataPart in a single common block.
part_data = None
metadata_type = None
if part.function_call:
part_data = part.function_call
metadata_type = A2A_DATA_PART_METADATA_TYPE_FUNCTION_CALL
elif part.function_response:
part_data = part.function_response
metadata_type = A2A_DATA_PART_METADATA_TYPE_FUNCTION_RESPONSE
elif part.code_execution_result:
part_data = part.code_execution_result
metadata_type = A2A_DATA_PART_METADATA_TYPE_CODE_EXECUTION_RESULT
elif part.executable_code:
part_data = part.executable_code
metadata_type = A2A_DATA_PART_METADATA_TYPE_EXECUTABLE_CODE
if part_data:
metadata = {
_get_adk_metadata_key(A2A_DATA_PART_METADATA_TYPE_KEY): metadata_type
}
if part.thought is not None:
metadata[_get_adk_metadata_key('thought')] = part.thought
return a2a_types.Part(
root=a2a_types.DataPart(
data=part_data.model_dump(by_alias=True, exclude_none=True),
metadata=metadata,
)
)src/google/adk/sessions/database_session_service.py (299-302)
The check for database dialects can be simplified to be more concise and maintainable. You can combine the checks into a single if statement using the in operator with a tuple of dialect names. This removes the need for intermediate variables and is easier to extend in the future.
if self.db_engine.dialect.name in ("sqlite", "postgresql"):
now = now.replace(tzinfo=None)
…ervice Adds unit tests to verify that timezone-aware datetimes are correctly converted to naive datetimes for SQLite and PostgreSQL dialects, which require naive timestamps for their default TIMESTAMP column types.
Summary
DataErrorwhen using PostgreSQL withasyncpgfor session storageTIMESTAMPtype isWITHOUT TIME ZONE, which cannot accept timezone-aware datetime objectsError
When creating a session with PostgreSQL + asyncpg, the following error occurs:
During the INSERT:
Where
$5and$6are timezone-aware datetimes being inserted intoTIMESTAMP WITHOUT TIME ZONEcolumns.Root Cause
Commit 1063fa5 changed from database-generated timestamps (
func.now()) to explicit Python datetimes (datetime.now(timezone.utc)). The SQLite case was handled by stripping the timezone, but PostgreSQL was overlooked.Test plan
Fixes regression from #1733