fix: chunk long text before embedding to prevent context-length errors - #59
Open
johnathanneals-dev wants to merge 1 commit into
Open
Conversation
The embed() path sends full text to the provider with no size handling. When input exceeds the model's context window (2048 tokens for nomic-embed-text, 8191 for text-embedding-3-small), the API returns 400 and the memory store fails. Adds automatic chunking at the create_embedding() level (above the provider abstraction, so all providers benefit): - Text exceeding 4000 chars is split into overlapping chunks - Each chunk is embedded independently - Chunk embeddings are mean-pooled + L2-normalized into one vector Also routes create_embeddings() (batch) through the same guard so both single and batch paths handle long content consistently. The 4000-char threshold safely fits within nomic-embed-text's 2048-token window even for dense technical content (~2-3 chars/token). The full memory text is stored unchanged — only embedding generation is affected.
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.
Closes #58.
create_embedding()andcreate_embeddings()pass full text to the provider with no size handling. When input exceeds the model's context window (2048 tokens fornomic-embed-text, 8191 fortext-embedding-3-small), the API returns 400 and the memory store fails.Fix
Adds automatic chunking at the
create_embedding()convenience function level — above the provider abstraction, so all 4 providers (OpenRouter, OpenAI, Ollama, Custom) benefit:embed()methodThe batch path
create_embeddings()now routes each text throughcreate_embedding()so both paths handle long content consistently.Threshold
4000 chars safely fits within
nomic-embed-text's 2048-token window even for dense technical content (~2-3 chars/token). Higher-capacity models liketext-embedding-3-smallhave more headroom but the conservative default prevents failures across all providers.The full memory text is stored unchanged in the database — only embedding generation is affected. Search recall is slightly reduced for chunked content (mean-pooling vs single-vector), but the memory is stored successfully instead of crashing.
Context: I flagged a related issue in PR #15's code review (stale embeddings on content update) — this is a complementary fix addressing the input-size surface.