Skip to content

[BUG] Validation error on datetime #253

Closed

Description

@Elpaggio

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

Activity

  1. jope-bm commented on Aug 22, 2025

    @jope-bm
    Contributor

    @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: #266

  2. claude commented on Aug 22, 2025

    @claude
    Contributor

    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:

    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.

  3. added a commit that references this issue on Aug 22, 2025
    8d95487
  4. added this to the v0.14.4 milestone on Aug 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions