[BUG] compute_composite_score crashes with TypeError on timezone-aware datetimes during memory recall
Description
In lib/crewai/src/crewai/memory/types.py (line 364), compute_composite_score() calculates the age of a record as:
age_seconds = (datetime.utcnow() - record.created_at).total_seconds()
datetime.utcnow() produces an offset-naive datetime (and is deprecated since Python 3.12).
If a MemoryRecord has a timezone-aware created_at timestamp (such as datetime.now(timezone.utc), timestamps loaded from external stores/APIs with UTC offsets like +00:00 or Z), subtracting them raises:
TypeError: can't subtract offset-naive and offset-aware datetimes Because compute_composite_score() is called during every memory.recall() in unified_memory.py and RecallFlow in recall_flow.py, any memory record created with a timezone-aware datetime immediately crashes the agent's recall pipeline.
Steps to Reproduce
from datetime import datetime, timezone
from crewai.memory.types import MemoryRecord, MemoryConfig, compute_composite_score
# 1. Create a MemoryRecord with a timezone-aware UTC datetime
record = MemoryRecord(
content="User prefers Python over JavaScript",
created_at=datetime.now(timezone.utc)
)
# 2. Attempt to score the record (as done during memory.recall())
config = MemoryConfig()
score, reasons = compute_composite_score(record, semantic_score=0.85, config=config)
Expected behavior
compute_composite_score() should handle both offset-naive and timezone-aware datetimes gracefully without raising a TypeError, normalizing both to UTC before computing age_seconds.
Screenshots/Code snippets
from datetime import datetime, timezone
from crewai.memory.types import MemoryRecord, MemoryConfig, compute_composite_score
rec = MemoryRecord(content="test", created_at=datetime.now(timezone.utc))
compute_composite_score(rec, 0.8, MemoryConfig())
Operating System
macOS Sonoma
Python Version
3.12
crewAI Version
main (latest 1.10.x)
crewAI Tools Version
0.33.0
Virtual Environment
Venv
Evidence
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "crewai/memory/types.py", line 364, in compute_composite_score
age_seconds = (datetime.utcnow() - record.created_at).total_seconds()
~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~
TypeError: can't subtract offset-naive and offset-aware datetimes
Possible Solution
now = datetime.now(timezone.utc)
rec_time = record.created_at
if rec_time.tzinfo is None:
rec_time = rec_time.replace(tzinfo=timezone.utc)
else:
rec_time = rec_time.astimezone(timezone.utc)
age_seconds = max((now - rec_time).total_seconds(), 0.0)
Additional context
I have already reproduced the bug locally, and have unit tests and a fix ready. I would be happy to submit a pull request for this!
Hi Maintainers, I’m interested in working on this, I've already reproduced the issue locally and have the fix and ready to submit. Could you please assign this issue to me. Thanks
Hey @samvallad33, That is precisely what is implemented in PR #7596.
- Introduced
normalize_to_utc()to ensure naive, UTC, and offset-aware datetimes are cleanly normalized to UTC before computing age decay, completely eliminating theTypeError. - Added a clock-skew guard (
max((now - rec_time).total_seconds(), 0.0)) so future-skewed timestamps decay gracefully to age0.0. - Applied the same normalization to deep recall temporal query filters (
time_cutoff).
This issue is stale because it has been open for 30 days with no activity. Remove stale label or comment or this will be closed in 5 days.
@Vidit-Ostwal, whenever you have a moment, could you please take a look?
This issue is stale because it has been open for 30 days with no activity. Remove stale label or comment or this will be closed in 5 days.