fix(memory): replace deprecated datetime.utcnow() with timezone-aware alternative#6200
fix(memory): replace deprecated datetime.utcnow() with timezone-aware alternative#6200MeloMei wants to merge 2 commits into
Conversation
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (4)
🚧 Files skipped from review as they are similar to previous changes (3)
📝 WalkthroughWalkthroughAcross four memory module files, all ChangesUTC Timestamp Standardization
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
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. Comment |
There was a problem hiding this comment.
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 withdatetime.now(timezone.utc).replace(tzinfo=None)in multiple modules. - Updates default timestamp factories for
MemoryRecordfields 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.
Summary
Replace all
datetime.utcnow()calls in the memory module withdatetime.now(timezone.utc).replace(tzinfo=None), eliminatingDeprecationWarningon 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.pybackend already uses the recommendedpattern
datetime.now(timezone.utc).replace(tzinfo=None). This PRaligns the remaining memory module files with that convention.
Changes
memory/types.py—MemoryRecord.created_atandlast_accesseddefault factories;
compute_composite_score()age calculationmemory/unified_memory.py—update_record()timestampmemory/encoding_flow.py—execute_plans()timestampmemory/storage/lancedb_storage.py— schema placeholder,_row_to_record()fallback,touch_records()timestampAll replacements use
.replace(tzinfo=None)to preserve the existingnaive-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
memory/module. Other modules may alsocontain
utcnow()calls — those can be handled in follow-up PRs.DeprecationWarning; updating testsis left for a separate PR to keep this change focused.
Summary by CodeRabbit