fix: replace deprecated datetime.utcnow() across the memory subsystem#5970
fix: replace deprecated datetime.utcnow() across the memory subsystem#5970kratos0718 wants to merge 1 commit into
datetime.utcnow() across the memory subsystem#5970Conversation
`datetime.utcnow()` is deprecated since Python 3.12 (per [official docs](https://docs.python.org/3/library/datetime.html#datetime.datetime.utcnow)) and is scheduled for removal in Python 3.14. crewAI's `requires-python` range is `>=3.10, <3.14`, so users on supported 3.12 and 3.13 currently see a `DeprecationWarning` on every memory write/read, and the project cannot bump its upper bound to include 3.14 without first removing these calls. This PR replaces all 9 call sites across 4 files in the memory subsystem with `datetime.now(timezone.utc).replace(tzinfo=None)`, which is the exact behavioral equivalent (current UTC time as a timezone-naive datetime). I deliberately preserved naive datetimes rather than moving to timezone-aware ones because: 1. `MemoryRecord.created_at` and `last_accessed` may already be persisted as naive datetimes in users' LanceDB tables; switching to aware would either break subtractions in `compute_composite_score` (`naive - aware` raises `TypeError`) or silently change the on-disk `.isoformat()` representation (`...` vs `...+00:00`). 2. The goal is to remove the deprecation without changing any user-visible semantics. ## Files changed | File | Call sites | |------|-----------| | `memory/types.py` | 3 — 2 `default_factory` in `MemoryRecord`, 1 in `compute_composite_score` | | `memory/storage/lancedb_storage.py` | 4 — `created_at`/`last_accessed` writes, `_parse_dt` fallback, batch update timestamp | | `memory/encoding_flow.py` | 1 — `EncodingFlow` apply-actions timestamp | | `memory/unified_memory.py` | 1 — `Memory` record update timestamp | Each file's `from datetime import datetime` was extended to `from datetime import datetime, timezone`. ## Behavior Zero behavior change. Existing tests that compare or serialize these datetimes will continue to pass.
|
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)
📝 WalkthroughWalkthroughThe pull request replaces all uses of deprecated ChangesMemory Timestamp Standardization
Estimated Code Review Effort🎯 2 (Simple) | ⏱️ ~12 minutes Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 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 |
Summary
datetime.utcnow()is deprecated since Python 3.12 and is scheduled for removal in Python 3.14. crewAI'srequires-pythonis>=3.10, <3.14, so users on the currently-supported 3.12 and 3.13 see aDeprecationWarningon every memory write/read, and the project cannot bump its upper bound to include 3.14 without first removing these calls.This PR replaces all 9 call sites across 4 files in the memory subsystem.
Why the chosen replacement preserves behavior exactly
I used
datetime.now(timezone.utc).replace(tzinfo=None)rather than the more moderndatetime.now(timezone.utc)because:Persisted data compatibility.
MemoryRecord.created_atandlast_accessedmay already be on disk in users' LanceDB tables as naive datetimes via the existingdefault_factory=datetime.utcnow. Switching to aware datetimes would either:compute_composite_score(naive - awareraisesTypeError: can't subtract offset-naive and offset-aware datetimes), or.isoformat()representation from2026-05-29T05:00:00to2026-05-29T05:00:00+00:00, which can affect downstream parsers.Goal of the patch. Remove the deprecation without touching any user-visible semantics.
A follow-up could migrate the whole memory module to timezone-aware datetimes, but that's a separate, behavior-changing PR.
Files changed
memory/types.pydefault_factoryonMemoryRecord, 1 incompute_composite_score's recency decaymemory/storage/lancedb_storage.pycreated_at/last_accessedwrites,_parse_dtfallback, batch update timestampmemory/encoding_flow.pyEncodingFlow._apply_actionstimestampmemory/unified_memory.pyMemory.update_recordtimestampEach file's
from datetime import datetimewas extended tofrom datetime import datetime, timezone.Verification
datetime, identical to whatdatetime.utcnow()previously returned.Summary by CodeRabbit