Skip to content

fix: Ensure datetime serialization includes timezone info for RFC 3339 compliance#267

Closed
jope-bm wants to merge 2 commits into
mainfrom
claude/issue-253-20250822-1537
Closed

fix: Ensure datetime serialization includes timezone info for RFC 3339 compliance#267
jope-bm wants to merge 2 commits into
mainfrom
claude/issue-253-20250822-1537

Conversation

@jope-bm
Copy link
Copy Markdown
Contributor

@jope-bm jope-bm commented Aug 22, 2025

Add field_serializer methods to all datetime fields in memory schemas to ensure proper timezone-aware datetime serialization. This fixes validation errors in MCP tools like build_context where datetime strings were missing timezone information.

  • Added ensure_timezone_aware utility function
  • Updated EntitySummary, RelationSummary, ObservationSummary, MemoryMetadata
  • All datetime fields now serialize with timezone suffix (Z or +00:00)
  • Handles both timezone-aware and naive datetimes by converting to UTC

Fixes #253

Generated with Claude Code

claude Bot and others added 2 commits August 22, 2025 15:41
…9 compliance

Add field_serializer methods to all datetime fields in memory schemas to ensure
proper timezone-aware datetime serialization. This fixes validation errors in
MCP tools like build_context where datetime strings were missing timezone
information.

- Added ensure_timezone_aware utility function
- Updated EntitySummary, RelationSummary, ObservationSummary, MemoryMetadata
- All datetime fields now serialize with timezone suffix (Z or +00:00)
- Handles both timezone-aware and naive datetimes by converting to UTC

Fixes #253

Co-authored-by: jope-bm <jope-bm@users.noreply.github.com>
The datetime fix test was only for development verification.
Copy link
Copy Markdown
Member

@phernandez phernandez left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought pydandic did this for us?

@jope-bm
Copy link
Copy Markdown
Contributor Author

jope-bm commented Aug 22, 2025

I thought pydandic did this for us?

If the source datetime is naive (no tzinfo), it is serialized without Z or timezone. Which, of course, makes sense.

So maybe the real issue is that build_context is creating datetimes without timezone? I'm not sure how we'd be getting an ISO string without tzinfo otherwise.

@phernandez
Copy link
Copy Markdown
Member

🤷🏽 it could also be a platform thing, or some weird mcp tool thing. Whatever it takes.

@jope-bm
Copy link
Copy Markdown
Contributor Author

jope-bm commented Aug 22, 2025

@phernandez This is what Claude tells me:

Yes, the build_context tool is returning dates without timezone info.

  Issue found:
  - generated_at: Returns dates with timezone info (e.g., "2025-08-22T15:59:44.292047Z")
  - timeframe: Returns dates without timezone info (e.g., "2025-08-21T09:59:44.287982")

  Root cause:
  In src/basic_memory/services/context_service.py:157, the timeframe is set using:
  timeframe=since.isoformat() if since else None,

  The since datetime object comes from parse_timeframe() which uses the dateparser library but doesn't preserve timezone
  information, so .isoformat() produces a timestamp without the timezone suffix.

  Fix needed:
  The parse_timeframe() function should return timezone-aware datetimes, or the context service should add timezone info before
  calling .isoformat().

@jope-bm jope-bm closed this Aug 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] Validation error on datetime

2 participants