Skip to content

Fix list_memories endpoint Pydantic validation error#3608

Merged
prateekchhikara merged 1 commit intomem0ai:mainfrom
frederikb96:fix/list-memories-pydantic-validation
Oct 18, 2025
Merged

Fix list_memories endpoint Pydantic validation error#3608
prateekchhikara merged 1 commit intomem0ai:mainfrom
frederikb96:fix/list-memories-pydantic-validation

Conversation

@frederikb96
Copy link
Copy Markdown
Contributor

The Problem

The list_memories endpoint (GET /api/v1/memories/) was returning 500 Internal Server Error when searching for memories.

What was broken:

  • Endpoint returned raw SQLAlchemy ORM objects instead of Pydantic MemoryResponse schema
  • Pydantic validation failed because ORM objects have nested relationships (app, categories) that need to be transformed
  • Error: ValidationError: Field required [type=missing, input_value=<Memory object>]

How It's Done Elsewhere

Looking at the same file, the filter_memories endpoint (lines 412-503) already handles this correctly:

  • Uses joinedload(Memory.app, Memory.categories) for eager loading
  • Uses transformer parameter in sqlalchemy_paginate to convert ORM → Pydantic schema
  • Builds MemoryResponse objects manually with proper field extraction

The Fix

Applied the same pattern to list_memories:

  1. Added eager loading with joinedload(Memory.app, Memory.categories)
  2. Added .distinct(Memory.id) to prevent duplicates from joins
  3. Added transformer lambda to convert ORM objects to MemoryResponse schema
  4. Integrated permission checking within the transformer

Type of change

  • Bug fix (non-breaking change which fixes an issue)

How Has This Been Tested?

  • Test Script - verified search now returns 200 OK with proper JSON response
  • Tested with both infer=True and infer=False memory additions
  • Search correctly returns memories with proper schema structure

Checklist:

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • My changes generate no new warnings
  • Follows existing pattern from filter_memories endpoint

- Add transformer to convert ORM objects to MemoryResponse schema
- Add eager loading with joinedload for app and categories
- Follow pattern from filter_memories endpoint (lines 412-503)
@parshvadaftari
Copy link
Copy Markdown
Contributor

Hey @frederikb96 thank you for raising this PR.
@VikramIyer125 Can you review this?

@VikramIyer125
Copy link
Copy Markdown
Contributor

Approved @frederikb96

@prateekchhikara prateekchhikara merged commit 8ede1df into mem0ai:main Oct 18, 2025
1 of 2 checks passed
@frederikb96 frederikb96 deleted the fix/list-memories-pydantic-validation branch October 19, 2025 07:34
garciaba79 pushed a commit to garciaba79/mem0 that referenced this pull request Feb 12, 2026
jamebobob pushed a commit to jamebobob/mem0-vigil-recall that referenced this pull request Mar 29, 2026
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.

4 participants