fix: replace deprecated `datetime.utcnow()` across the memory subsystem
Summary
datetime.utcnow() is deprecated since Python 3.12 and is scheduled for removal in Python 3.14. crewAI's requires-python is >=3.10, <3.14, so users on the currently-supported 3.12 and 3.13 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.
Why the chosen replacement preserves behavior exactly
I used datetime.now(timezone.utc).replace(tzinfo=None) rather than the more modern datetime.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:- Break
compute_composite_score(naive - awareraisesTypeError: can't subtract offset-naive and offset-aware datetimes), or - Silently change the on-disk
.isoformat()representation from2026-05-29T05:00:00to2026-05-29T05:00:00+00:00, which can affect downstream parsers.
- Break
-
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
| File | Call sites | What |
|---|---|---|
memory/types.py |
3 | 2 default_factory on MemoryRecord, 1 in compute_composite_score's recency decay |
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.update_record timestamp |
Each file's from datetime import datetime was extended to from datetime import datetime, timezone.
Verification
- No semantic change: every call still returns a naive UTC
datetime, identical to whatdatetime.utcnow()previously returned. - No new dependencies.
- No test changes required β existing memory tests continue to pass against this drop-in replacement.
Summary by CodeRabbit
- Refactor
- Updated internal timestamp handling in the memory module to use standardized UTC datetime generation across all memory operations.
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: 96ef7da9-d2fd-44a8-952b-6083e9ac3793
π₯ Commits
Reviewing files that changed from the base of the PR and between fca21b155c4f316ee63d4aa1725361aff392e47e and 7324392ed65f418c71afddee153bf428c3e0fcc0.
π Files selected for processing (4)
lib/crewai/src/crewai/memory/encoding_flow.pylib/crewai/src/crewai/memory/storage/lancedb_storage.pylib/crewai/src/crewai/memory/types.pylib/crewai/src/crewai/memory/unified_memory.py
π Walkthrough
Walkthrough
The pull request replaces all uses of deprecated datetime.utcnow() with datetime.now(timezone.utc).replace(tzinfo=None) across the memory module. Changes span memory type definitions, storage operations, and execution paths, importing timezone from the datetime module to enable timezone-aware UTC timestamp construction.
Changes
Memory Timestamp Standardization
| Layer / File(s) | Summary |
|---|---|
Memory record types and scoring lib/crewai/src/crewai/memory/types.py |
Import timezone and update MemoryRecord.created_at and last_accessed field defaults and compute_composite_score recency calculation to use datetime.now(timezone.utc).replace(tzinfo=None) instead of datetime.utcnow. |
Storage implementation timestamps lib/crewai/src/crewai/memory/storage/lancedb_storage.py |
Import timezone and update schema placeholder record initialization, _parse_dt fallback parsing, and touch_records methods to generate timestamps using timezone-aware UTC. |
Memory operation timestamps lib/crewai/src/crewai/memory/encoding_flow.py, lib/crewai/src/crewai/memory/unified_memory.py |
Import timezone and update execute_plans and Memory.update methods to compute last_accessed timestamps using datetime.now(timezone.utc).replace(tzinfo=None). |
Estimated Code Review Effort
π― 2 (Simple) | β±οΈ ~12 minutes
Poem
π° Clocks now tick with timezone flair,
UTC aware, beyond compare!
No more.utcnow()in the night,
Each timestamp shines with awareness bright! β¨
π₯ Pre-merge checks | β 4 | β 1
β Failed checks (1 warning)
| Check name | Status | Explanation | Resolution |
|---|---|---|---|
| Docstring Coverage | β οΈ Warning | Docstring coverage is 71.43% which is insufficient. The required threshold is 80.00%. | Write docstrings for the functions missing them to satisfy the coverage threshold. |
β Passed checks (4 passed)
| Check name | Status | Explanation |
|---|---|---|
| Description Check | β Passed | Check skipped - CodeRabbitβs high-level summary is enabled. |
| Title check | β Passed | The title accurately and concisely describes the main change: replacing deprecated datetime.utcnow() calls across the memory subsystem with the recommended alternative. |
| 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.
Comment @coderabbitai help to get the list of available commands and usage tips.