Skip to content

DatabaseSessionService fails with PostgreSQL + asyncpg: can't subtract offset-naive and offset-aware datetimes #4366

Closed

Description

@filipecaixeta

🔴 Required Information

Describe the Bug:
When using DatabaseSessionService with PostgreSQL and asyncpg, creating a new session fails with a DataError because timezone-aware Python datetime objects are being passed to PostgreSQL TIMESTAMP WITHOUT TIME ZONE columns.

Steps to Reproduce:

  1. Configure DatabaseSessionService with a PostgreSQL database using asyncpg driver
  2. Set SESSION_SERVICE_URI=postgresql+asyncpg://user:pass@localhost:5432/db
  3. Create a new session via the ADK agent
  4. Error occurs on session creation

Expected Behavior:
Session should be created successfully in the PostgreSQL database.

Observed Behavior:

sqlalchemy.dialects.postgresql.asyncpg.Error: <class 'asyncpg.exceptions.DataError'>: 
invalid input for query argument $5: datetime.datetime(2026, 2, 3, 21, 32, 50, 353909, 
tzinfo=datetime.timezone.utc) (can't subtract offset-naive and offset-aware datetimes)

During the INSERT:

INSERT INTO sessions (app_name, user_id, id, state, create_time, update_time) 
VALUES ($1, $2, $3, $4, $5, $6)

Environment Details:

  • ADK Library Version: 1.22.1+
  • Desktop OS: macOS (also affects Linux)
  • Python Version: 3.13

Model Information:

  • Are you using LiteLLM: N/A (issue is in session service, not model)
  • Which model is being used: N/A

🟡 Optional Information

Regression:
Yes. This worked prior to commit 1063fa5 which changed from database-generated timestamps (func.now()) to explicit Python datetimes (datetime.now(timezone.utc)).

The SQLite case was correctly handled by stripping the timezone:

now = datetime.now(timezone.utc)
is_sqlite = self.db_engine.dialect.name == "sqlite"
if is_sqlite:
    now = now.replace(tzinfo=None)

But PostgreSQL was not handled, even though its default TIMESTAMP type is also WITHOUT TIME ZONE.

Logs:

sqlalchemy.dialects.postgresql.asyncpg.Error: <class 'asyncpg.exceptions.DataError'>: 
invalid input for query argument $5: datetime.datetime(2026, 2, 3, 21, 32, 50, 353909, 
tzinfo=datetime.timezone.utc) (can't subtract offset-naive and offset-aware datetimes)

Minimal Reproduction Code:

from google.adk.sessions import DatabaseSessionService

# Configure with PostgreSQL + asyncpg
session_service = DatabaseSessionService(
    db_url="postgresql+asyncpg://user:pass@localhost:5432/agent_sessions"
)

# This will fail when creating any session

How often has this issue occurred?:

  • Always (100%) - when using PostgreSQL with asyncpg

Fix:
PR #4365 addresses this by also stripping the timezone for PostgreSQL:

is_postgres = self.db_engine.dialect.name == "postgresql"
if is_sqlite or is_postgres:
    now = now.replace(tzinfo=None)

Activity

  1. added
    services[Component] This issue is related to runtime services, e.g. sessions, memory, artifacts, etc
    on Feb 3, 2026
  2. added theissue type on Feb 3, 2026
  3. self-assigned this
    on Feb 4, 2026
  4. marcopellegrino-devoteam commented on Feb 6, 2026

    @marcopellegrino-devoteam

    Found the same issue.
    Only from ADK 1.24.0
    It works with 1.23.0

  5. mariokorte commented on Feb 6, 2026

    @mariokorte

    +1 here for 1.24.

    I’ve added a monkey_patch in my project with the content of #4365 and this works for now.

  6. aidanandrews22 commented on Feb 7, 2026

    @aidanandrews22

    +1

    Here is a minimal monkey patch that fixes it. Just add this at the top of an early imported file (before you use any adk stuff):

    Patch: use a datetime subclass so we don't touch immutable built-in

    from datetime import datetime as _RealDateTime, timezone
    import google.adk.sessions.database_session_service as _db_session_mod

    class _NaiveUTCDateTime(_RealDateTime):
    @classmethod
    def now(cls, tz=None):
    r = _RealDateTime.now(tz or timezone.utc)
    return r.replace(tzinfo=None) if r.tzinfo else r

    _db_session_mod.datetime = _NaiveUTCDateTime

  7. karticksaxena commented on Feb 8, 2026

    @karticksaxena

    Facing the same issue, for now reverted to the previous version 1.23.0

  8. marked create_session bug #4419 as a duplicate of this issue on Feb 9, 2026
  9. added 2 commits that reference this issue on Feb 10, 2026
    c8ce8cf
    56a3317
  10. wuliang229 commented on Feb 10, 2026

    @wuliang229
    Collaborator

    @mariokorte @filipecaixeta @The-Batman-Code @aidanandrews22 should be fixed by 19b6076

  11. added 2 commits that reference this issue on Mar 21, 2026
    f628094
    faa7d4e
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

services[Component] This issue is related to runtime services, e.g. sessions, memory, artifacts, etc

Type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions