Skip to content

fix: resolve SQLite embedding TypeError by using sa_column=False#401

Open
octo-patch wants to merge 1 commit intoNevaMind-AI:mainfrom
octo-patch:fix/issue-382-sqlite-embedding-type-error
Open

fix: resolve SQLite embedding TypeError by using sa_column=False#401
octo-patch wants to merge 1 commit intoNevaMind-AI:mainfrom
octo-patch:fix/issue-382-sqlite-embedding-type-error

Conversation

@octo-patch
Copy link
Copy Markdown

Fixes #382

Problem

When running the SQLite test script (tests/test_sqlite.py), table creation fails with:

raise ValueError(f"{type_} has no matching SQLAlchemy type")
ValueError: <class 'list'> has no matching SQLAlchemy type

SQLiteResourceModel, SQLiteMemoryItemModel, and SQLiteMemoryCategoryModel all inherit embedding: list[float] | None from their respective parent base classes (Resource, MemoryItem, MemoryCategory). SQLModel's metaclass traverses the full MRO to discover annotated fields and attempts to create a SQLAlchemy column for every one of them. Since SQLAlchemy has no native mapping for list[float], it raises a ValueError.

The previous @property approach tried to shadow the inherited field, but Python properties do not prevent Pydantic v2 / SQLModel's metaclass from processing the annotation found in the parent class hierarchy.

Solution

Re-declare embedding in each SQLite model class with Field(default=None, sa_column=False). This tells SQLModel the field exists for Pydantic validation purposes only — no SQL column should be created for it.

The actual persistence of embeddings is unchanged: embedding_json (a Text column) stores the vector serialized as a JSON string, and the repository layer's _normalize_embedding / _prepare_embedding helpers handle conversion between list[float] and JSON on every read/write.

The now-unreachable @property / @embedding.setter bodies and their associated import json / import logging are also removed.

Testing

  • SQLite table creation no longer raises ValueError: <class 'list'> has no matching SQLAlchemy type
  • Read/write round-trips for embeddings are unaffected because all repository code already accessed embedding_json directly, not the property

…fixes NevaMind-AI#382)

SQLiteResourceModel, SQLiteMemoryItemModel, and SQLiteMemoryCategoryModel
all inherited `embedding: list[float] | None` from their parent base classes.
SQLModel tried to create a SQLAlchemy column for this field, which fails with:
    ValueError: <class 'list'> has no matching SQLAlchemy type

The @Property approach attempted to shadow the inherited field but does not
prevent SQLModel's metaclass from processing the annotation in the MRO.

Fix: re-declare `embedding` in each SQLite model with `sa_column=False`,
telling SQLModel it is a Pydantic-only field with no corresponding DB column.
The actual storage is handled by `embedding_json` (Text column) and the
repository layer's `_normalize_embedding` / `_prepare_embedding` helpers,
which already convert between list[float] and JSON strings.
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] sqlite backend embding issue

1 participant