Skip to content

fix(memory): replace deprecated datetime.utcnow() with timezone-aware alternative#6200

Open
MeloMei wants to merge 2 commits into
crewAIInc:mainfrom
MeloMei:fix/deprecated-datetime-utcnow
Open

fix(memory): replace deprecated datetime.utcnow() with timezone-aware alternative#6200
MeloMei wants to merge 2 commits into
crewAIInc:mainfrom
MeloMei:fix/deprecated-datetime-utcnow

Conversation

@MeloMei

@MeloMei MeloMei commented Jun 17, 2026

Copy link
Copy Markdown

Summary

Replace all datetime.utcnow() calls in the memory module with
datetime.now(timezone.utc).replace(tzinfo=None), eliminating
DeprecationWarning on Python 3.12+.

Motivation

datetime.utcnow() is deprecated since Python 3.12
(docs)
and scheduled for removal. It creates naive datetimes that are
indistinguishable from local time, which can cause subtle bugs
when mixed with timezone-aware datetimes.

The qdrant_edge_storage.py backend already uses the recommended
pattern datetime.now(timezone.utc).replace(tzinfo=None). This PR
aligns the remaining memory module files with that convention.

Changes

  • memory/types.pyMemoryRecord.created_at and last_accessed
    default factories; compute_composite_score() age calculation
  • memory/unified_memory.pyupdate_record() timestamp
  • memory/encoding_flow.pyexecute_plans() timestamp
  • memory/storage/lancedb_storage.py — schema placeholder,
    _row_to_record() fallback, touch_records() timestamp

All replacements use .replace(tzinfo=None) to preserve the existing
naive-datetime contract across the codebase.

Tests

Existing memory tests pass. The 4 pre-existing failures on Windows
(lancedb network guard, qdrant os.kill compat) are unrelated.

uv run pytest lib/crewai/tests/memory/ -x -q
# 13 passed, 4 failed (pre-existing)

Notes

  • This PR only addresses memory/ module. Other modules may also
    contain utcnow() calls — those can be handled in follow-up PRs.
  • Test files also emit the same DeprecationWarning; updating tests
    is left for a separate PR to keep this change focused.

Note: This PR was authored with AI assistance. Please apply the llm-generated label per CONTRIBUTING.md.

Summary by CodeRabbit

  • Refactor
    • Standardized timestamp handling across the memory system by introducing a centralized time utility function. All timestamp generation now uses a consistent approach, improving code maintainability and reducing redundancy.

Copilot AI review requested due to automatic review settings June 17, 2026 06:55
@coderabbitai

coderabbitai Bot commented Jun 17, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 5fe687b7-f23a-4476-aa5d-87c8c628751e

📥 Commits

Reviewing files that changed from the base of the PR and between c6f7f4b and 1365253.

📒 Files selected for processing (4)
  • lib/crewai/src/crewai/memory/encoding_flow.py
  • lib/crewai/src/crewai/memory/storage/lancedb_storage.py
  • lib/crewai/src/crewai/memory/types.py
  • lib/crewai/src/crewai/memory/unified_memory.py
🚧 Files skipped from review as they are similar to previous changes (3)
  • lib/crewai/src/crewai/memory/storage/lancedb_storage.py
  • lib/crewai/src/crewai/memory/types.py
  • lib/crewai/src/crewai/memory/encoding_flow.py

📝 Walkthrough

Walkthrough

Across four memory module files, all datetime.utcnow() calls are replaced with a shared _utc_now() helper that returns datetime.now(timezone.utc).replace(tzinfo=None). The change affects MemoryRecord field defaults, the compute_composite_score recency calculation, LanceDB storage schema rows and touch_records, execute_plans update timestamps, and Memory.update() last_accessed assignment.

Changes

UTC Timestamp Standardization

Layer / File(s) Summary
Central _utc_now() helper and MemoryRecord defaults
lib/crewai/src/crewai/memory/types.py
Introduces _utc_now() helper that returns naive UTC datetimes. Updates MemoryRecord.created_at and last_accessed default factories, and compute_composite_score recency calculation to use the helper.
LanceDB storage schema and datetime parsing
lib/crewai/src/crewai/memory/storage/lancedb_storage.py
LanceDBStorage imports timezone and _utc_now, updates schema placeholder rows to use a single now timestamp for both created_at and last_accessed, normalizes datetime inputs in _parse_dt to naive UTC, and updates touch_records to use the helper.
Encoding flow timestamp generation
lib/crewai/src/crewai/memory/encoding_flow.py
execute_plans removes direct datetime import and switches to _utc_now() for generating now timestamps when updating MemoryRecord objects.
Unified memory last_accessed update
lib/crewai/src/crewai/memory/unified_memory.py
Memory.update() imports _utc_now and switches from datetime.utcnow() to the helper when setting record last_accessed timestamps.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main change: replacing deprecated datetime.utcnow() calls with a timezone-aware alternative, which is the core objective reflected in all four modified files.
Docstring Coverage ✅ Passed Docstring coverage is 87.50% which is sufficient. The required threshold is 80.00%.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

This PR updates how “now” timestamps are generated across the memory subsystem, switching from datetime.utcnow() to datetime.now(timezone.utc) (while still persisting naive UTC datetimes).

Changes:

  • Replaces datetime.utcnow() usages with datetime.now(timezone.utc).replace(tzinfo=None) in multiple modules.
  • Updates default timestamp factories for MemoryRecord fields to use the new approach.
  • Updates recency scoring and storage “touch” timestamps to use the new approach.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.

File Description
lib/crewai/src/crewai/memory/unified_memory.py Updates record update timestamp generation to use timezone-based now() then strip tzinfo.
lib/crewai/src/crewai/memory/types.py Updates model timestamp defaults and recency scoring to use the new “UTC now” approach.
lib/crewai/src/crewai/memory/storage/lancedb_storage.py Updates storage-layer created/last_accessed timestamps and fallback datetime parsing default.
lib/crewai/src/crewai/memory/encoding_flow.py Updates plan execution timestamp generation to use the new “UTC now” approach.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread lib/crewai/src/crewai/memory/types.py Outdated
Comment thread lib/crewai/src/crewai/memory/storage/lancedb_storage.py Outdated
Comment thread lib/crewai/src/crewai/memory/storage/lancedb_storage.py Outdated
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants