[PR #14] [MERGED] Fix: pre-truncate oversized embedding input to prevent SDK hang #16

Closed
opened 2026-03-15 16:00:20 +03:00 by kerem · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/ForLoopCodes/contextplus/pull/14
Author: @dearcodes
Created: 3/4/2026
Status: Merged
Merged: 3/5/2026
Merged by: @ForLoopCodes

Base: mainHead: fix/pre-truncate-oversized-embed-input


📝 Commits (1)

  • 60acdba Fix: pre-truncate oversized embedding input to prevent Ollama SDK hang

📊 Changes

1 file changed (+3 additions, -1 deletions)

View changed files

📝 src/core/embeddings.ts (+3 -1)

📄 Description

Problem

The Ollama JS SDK hangs indefinitely (promise never resolves or rejects) when embedding input exceeds the model's context window (e.g. nomic-embed-text at 8192 tokens).

This means embedSingleAdaptive's retry logic never fires — no error is thrown, so isContextLengthError is never checked. The result is semantic_code_search failing with "Unable to embed oversized input after adaptive retries" for any project with large files.

Root Cause

Ollama server returns HTTP 400 with "the input length exceeds the context length", but the JS SDK (ollama@^0.6.3) doesn't surface this as a rejected promise — it hangs.

Reproduction

Any project containing large generated or auto-generated files (codegen output, type declarations, migration snapshots, etc.) triggers this. The text assembled at SearchIndex.index() (header + symbols + content) for these files far exceeds the model's context window.

Fix

Pre-truncate input text to 8000 chars before calling ollama.embed(). This is conservative for nomic-embed-text's 8192 token context. Truncation at index time preserves file headers and symbol names (most semantically relevant) while trimming excess file content.

Verification

Tested locally against a monorepo with generated files up to 2MB. Before fix: hangs/errors. After fix: indexes and searches correctly.


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/ForLoopCodes/contextplus/pull/14 **Author:** [@dearcodes](https://github.com/dearcodes) **Created:** 3/4/2026 **Status:** ✅ Merged **Merged:** 3/5/2026 **Merged by:** [@ForLoopCodes](https://github.com/ForLoopCodes) **Base:** `main` ← **Head:** `fix/pre-truncate-oversized-embed-input` --- ### 📝 Commits (1) - [`60acdba`](https://github.com/ForLoopCodes/contextplus/commit/60acdba5d9a2f643a1430f686653e51e033a8f1b) Fix: pre-truncate oversized embedding input to prevent Ollama SDK hang ### 📊 Changes **1 file changed** (+3 additions, -1 deletions) <details> <summary>View changed files</summary> 📝 `src/core/embeddings.ts` (+3 -1) </details> ### 📄 Description ## Problem The Ollama JS SDK **hangs indefinitely** (promise never resolves or rejects) when embedding input exceeds the model's context window (e.g. `nomic-embed-text` at 8192 tokens). This means `embedSingleAdaptive`'s retry logic never fires — no error is thrown, so `isContextLengthError` is never checked. The result is `semantic_code_search` failing with "Unable to embed oversized input after adaptive retries" for any project with large files. ## Root Cause Ollama server returns HTTP 400 with `"the input length exceeds the context length"`, but the JS SDK (`ollama@^0.6.3`) doesn't surface this as a rejected promise — it hangs. ## Reproduction Any project containing large generated or auto-generated files (codegen output, type declarations, migration snapshots, etc.) triggers this. The text assembled at `SearchIndex.index()` (`header + symbols + content`) for these files far exceeds the model's context window. ## Fix Pre-truncate input text to **8000 chars** before calling `ollama.embed()`. This is conservative for `nomic-embed-text`'s 8192 token context. Truncation at index time preserves file headers and symbol names (most semantically relevant) while trimming excess file content. ## Verification Tested locally against a monorepo with generated files up to 2MB. Before fix: hangs/errors. After fix: indexes and searches correctly. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
kerem 2026-03-15 16:00:20 +03:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/contextplus#16
No description provided.