[BUG] compute_composite_score crashes with TypeError on timezone-aware datetimes during memory recall
@Rohitkanithi is already working on this.
Since Sep 17, 2026.
- Dominant language
- Python
- Stars
- 58.8k
- Forks
- 8.5k
- Avg merge
- 1d 15h
- Merged PRs (30d)
- 109
Description
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!
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Assessment
This issue has not been assessed yet.