basic-memory icon indicating copy to clipboard operation
basic-memory copied to clipboard

[BUG] Validation error on datetime

Open Elpaggio opened this issue 1 year ago • 2 comments

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:

  1. Install version 0.14.2
  2. Run command 'npx @modelcontextprotocol/inspector uvx basic-memory --project [WHATEVER] mcp'
  3. Use tool/feature 'build_context'
  4. 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

Image

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

Elpaggio avatar Aug 14 '25 14:08 Elpaggio

@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

jope-bm avatar Aug 22 '25 15:08 jope-bm

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:

  1. Created utility function ensure_timezone_aware() that converts naive datetimes to UTC

  2. Added @field_serializer decorators to all datetime fields in:

    • EntitySummary.created_at
    • RelationSummary.created_at
    • ObservationSummary.created_at
    • MemoryMetadata.generated_at
  3. All datetime fields now serialize with proper timezone information (+00:00 suffix)

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.

claude[bot] avatar Aug 22 '25 15:08 claude[bot]