[BUG] Validation error on datetime
Bug Description
When using build_context search results contain datetimes specified as a "date-time" type in the output schema.
These data does not adhere to the "ISO 8601 / RFC 3339 style date-time strings (with timezone)".
Steps To Reproduce
Steps to reproduce the behavior:
- Install version 0.14.2
- Run command 'npx @modelcontextprotocol/inspector uvx basic-memory --project [WHATEVER] mcp'
- Use tool/feature 'build_context'
- See error
Expected Behavior
created_at: "2025-08-12T13:44:45.344889" should be 2025-08-12T13:44:45.344889Z with a "Z" appended if no time zone information is available.
Actual Behavior
Validation Error: data.results[0].primary_result.created_at should match format "date-time"
created_at: "2025-08-12T13:44:45.344889"
Environment
- OS: [Windows 11]
- Python version: [e.g. 3.12.9]
- Basic Memory version: [e.g. 0.14.2]
- Installation method: uv
Additional Context
Possible Solution
Introduce a utility function like this:
def to_rfc3339(dt: Union[datetime, str]) -> datetime:
"""Converts a datetime object or ISO 8601 string to a timezone-aware UTC datetime, suitable for RFC 3339."""
if isinstance(dt, str):
dt = datetime.fromisoformat(dt)
if dt.tzinfo is None:
# Assume UTC if no timezone info
return dt.replace(tzinfo=timezone.utc)
return dt.astimezone(timezone.utc)
and use in search_repository.py and context_service.py
@claude Can you take a look at this and see if it is still an issue? It may have been fixed by this pull request: https://github.com/basicmachines-co/basic-memory/pull/266
Claude finished @jope-bm's task —— View job • claude/issue-253-20250822-1537 • Create PR ➔
Datetime validation issue investigation completed ✅
I investigated and fixed the datetime validation issue reported in this bug.
Todo List:
- [x] Examine current datetime handling in codebase
- [x] Check PR #266 changes for datetime fixes
- [x] Test build_context functionality to verify datetime issue
- [x] Implement fix for datetime timezone serialization
- [x] Run tests and linting
Issue Analysis
The bug was still present after PR #266. While the codebase correctly uses timezone-aware datetime objects (UTC), Pydantic's default JSON serialization wasn't including timezone information in the serialized strings, causing MCP validation to fail.
Root Cause: Datetime fields were being serialized as "2025-08-12T13:44:45.344889" instead of the required RFC 3339 format "2025-08-12T13:44:45.344889+00:00" or "2025-08-12T13:44:45.344889Z".
Solution Implemented
Added proper datetime serialization to ensure RFC 3339 compliance:
-
Created utility function
ensure_timezone_aware()that converts naive datetimes to UTC -
Added
@field_serializerdecorators to all datetime fields in:EntitySummary.created_atRelationSummary.created_atObservationSummary.created_atMemoryMetadata.generated_at
-
All datetime fields now serialize with proper timezone information (
+00:00suffix)
Files Modified
src/basic_memory/schemas/memory.py- Added timezone-aware datetime serialization
The fix ensures that all datetime responses from build_context and other MCP tools will pass validation by including proper timezone information as required by the RFC 3339 specification.