Skip to content

server.py: datetime.utcnow() is deprecated (Python 3.12) and removed in Python 3.14 — use datetime.now(timezone.utc) #1647

@Dhivya-Bharathy

Description

@Dhivya-Bharathy

Severity

Severity Category Issue Type
Medium Reliability / Forward Compatibility Deprecated stdlib usage

Summary

praisonaiui/server.py calls datetime.utcnow() in two places to timestamp chat messages and session events. datetime.utcnow() was deprecated in Python 3.12 and will be removed in Python 3.14. On Python 3.13 it already emits a DeprecationWarning. This will turn into a runtime error in the next Python release — silently breaking all chat session timestamps without any code change by the user.


Affected Components

Component Location Call
server.py Line 773 "created_at": datetime.utcnow().isoformat()
server.py Line 851 "timestamp": datetime.utcnow().isoformat()

File: praisonaiui/server.py
Package: aiui 0.3.111 (installed as praisonaiui)


Expected vs Actual Behavior

Behavior
Expected Timestamps use datetime.now(timezone.utc) — the stdlib-recommended, timezone-aware replacement
Actual datetime.utcnow() is called — naive UTC datetime, deprecated, emits DeprecationWarning on Python 3.12+, removed in Python 3.14

Reproduction Steps

# Verify the call is present in the installed package
import inspect, importlib, praisonaiui.server as srv

src = inspect.getsource(srv)
for i, line in enumerate(src.splitlines(), 1):
    if "utcnow" in line:
        print(f"Line {i}: {line.strip()}")

Output:

Line 773: "created_at": datetime.utcnow().isoformat(),
Line 851: "timestamp": datetime.utcnow().isoformat(),

Or verify with Python warnings enabled:

python -W all -c "from datetime import datetime; datetime.utcnow()"
DeprecationWarning: datetime.utcnow() is deprecated and scheduled for removal in a future version.
Use timezone-aware objects to represent datetimes in UTC: datetime.now(timezone.utc).

Root Cause Analysis

datetime.utcnow() returns a naive datetime object (no tzinfo). The replacement API datetime.now(timezone.utc) returns a timezone-aware datetime. Python 3.12 formally deprecated utcnow() with a scheduled removal. On Python 3.13 today, this produces warnings in -W error environments (common in CI) and will raise AttributeError once removed in Python 3.14.


Timeline

Python Version Status
3.11 and earlier utcnow() works silently
3.12 DeprecationWarning added
3.13 (current LTS) Warning emitted, -W error CI pipelines fail
3.14 (upcoming) utcnow() removed — runtime AttributeError

Impact Analysis

Dimension Detail
Affected environments All users on Python 3.13+ with warnings enabled; all users on Python 3.14+
Failure mode AttributeError: type object 'datetime' has no attribute 'utcnow' on Python 3.14
Affected features Chat session creation (created_at), message timestamps (timestamp)
Fix complexity Very low — 2-line substitution

Why This Matters

Python 3.14 has a scheduled release in October 2025. Projects that are already running on Python 3.13 (the current CPython release) are one minor version upgrade away from a broken dashboard. Because both calls sit in request-handling paths, the failure will be immediate and visible to all users — not a rarely triggered edge case.


Suggested Fix

# Before (deprecated)
from datetime import datetime

"created_at": datetime.utcnow().isoformat(),   # line 773
"timestamp":  datetime.utcnow().isoformat(),   # line 851

# After (correct, timezone-aware)
from datetime import datetime, timezone

"created_at": datetime.now(timezone.utc).isoformat(),   # line 773
"timestamp":  datetime.now(timezone.utc).isoformat(),   # line 851

The .isoformat() output format is identical for consumers — this is a drop-in replacement with no API-surface change.


Backward Compatibility

Fully backward compatible. datetime.now(timezone.utc).isoformat() produces the same ISO 8601 string as datetime.utcnow().isoformat(), the only difference being the +00:00 suffix added by the aware version. If downstream consumers need the exact naive format, .replace(tzinfo=None) can be added, but the aware format is strictly more correct and informative.


Suggested Regression Test

import re
from praisonaiui import server

def test_no_utcnow_in_server():
    """server.py must not call the deprecated datetime.utcnow()."""
    import inspect
    source = inspect.getsource(server)
    assert "utcnow" not in source, (
        "datetime.utcnow() is deprecated (Python 3.12) and removed in Python 3.14. "
        "Use datetime.now(timezone.utc) instead."
    )

Acceptance Criteria

  • Both datetime.utcnow() calls in server.py (lines 773, 851) replaced with datetime.now(timezone.utc)
  • from datetime import datetime, timezone import updated in server.py
  • No DeprecationWarning emitted when running the dashboard under python -W error
  • Regression test added that asserts utcnow is absent from server.py

Environment

OS      : Windows 10 (19045)
Python  : 3.13
Package : aiui 0.3.111 (praisonaiui)
Install : pip install "praisonai[claw]"

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions