fix: resolve SQLite embedding TypeError by using sa_column=False#401
Open
octo-patch wants to merge 1 commit intoNevaMind-AI:mainfrom
Open
fix: resolve SQLite embedding TypeError by using sa_column=False#401octo-patch wants to merge 1 commit intoNevaMind-AI:mainfrom
octo-patch wants to merge 1 commit intoNevaMind-AI:mainfrom
Conversation
…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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #382
Problem
When running the SQLite test script (
tests/test_sqlite.py), table creation fails with:SQLiteResourceModel,SQLiteMemoryItemModel, andSQLiteMemoryCategoryModelall inheritembedding: list[float] | Nonefrom 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 forlist[float], it raises aValueError.The previous
@propertyapproach 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
embeddingin each SQLite model class withField(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(aTextcolumn) stores the vector serialized as a JSON string, and the repository layer's_normalize_embedding/_prepare_embeddinghelpers handle conversion betweenlist[float]and JSON on every read/write.The now-unreachable
@property/@embedding.setterbodies and their associatedimport json/import loggingare also removed.Testing
ValueError: <class 'list'> has no matching SQLAlchemy typeembedding_jsondirectly, not the property