Skip to content

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

@filipecaixeta

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)

Metadata

Metadata

Assignees

Labels

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

Type

No fields configured for Bug.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions