Skip to content

fix(embedding): reject text over the model token limit - #20

Merged
HarshMN2345 merged 7 commits into
masterfrom
fix-embedding-context-length
Nov 18, 2026
Merged

HarshMN2345 merged 7 commits into
masterfrom
fix-embedding-context-length

Conversation

@HarshMN2345

@HarshMN2345 HarshMN2345 commented Nov 18, 2026 •

Copy link
Copy Markdown
Member

Fixes #15

What changed

  • Each model now accepts up to its own context length (nomic 8192, gemma 2048, bge-small 512, minilm 256), capped by a new EMBEDDING_MAX_TOKENS env var that defaults to 2048.
  • Text over the limit is rejected with a 400 before any embedding runs, instead of being silently truncated. Callers are responsible for chunking.
  • tokens no longer counts padding, which minilm's tokenizer.json turns on.
  • compute_sub_batch sizes batches from the configured max length instead of the fixed 100/40 MB per text.

Default cap

Peak memory for nomic on CPU, from cgroup memory.peak in a container limited to 5 GB:

Cap 1 text 4 texts
512 640 MB 898 MB
1024 762 MB 1379 MB
2048 1149 MB 2923 MB
4096 3031 MB out of memory
8192 out of memory out of memory

Sub-batching cannot split a single text, so 8192 cannot run within 5 GB. 2048 is the largest tested cap where a batch of 4 still fits. Self-hosters with more memory can raise it.

Testing

cargo fmt, cargo clippy, 26 unit tests and 10 ignored e2e tests pass.

Every "After" value was observed on the running service. "Before" values for the cosine, 20,002-token and padding rows were observed on master. The other "Before" rows follow from the 512 and 256 truncation limits and were not run separately.

Request Before After
nomic, two texts differing after 600 tokens (cosine) 1.0 0.998613
nomic, 2048 tokens truncated to 512 200, tokens: 2048
nomic, 2049 tokens truncated to 512 400
nomic, 20,002 tokens 200, tokens: 20002 400
minilm, 256 tokens 200 200, tokens: 256
minilm, 257 tokens truncated to 256 400
minilm, "hi" plus 100 words, tokens 204 105

Example error: text at index 1 has 20002 tokens, the limit for nomic-embed-text is 2048

16 texts of 2048 tokens completed under each memory limit:

Limit Peak
2 GB 1415 MB
3 GB 1786 MB
5 GB 2907 MB

Known limits

  • Breaking for callers sending long text. Appwrite's POST /v1/embeddings/text catches embedding errors and returns a generic error for the whole batch, so it needs to surface this 400 before it bumps to this image.
  • A single 2048-token nomic text peaks at about 1.15 GB, so containers below roughly 1.2 GB need a lower cap.
  • With EMBEDDING_POOL_SIZE above 1, sub-batches run concurrently and the budget does not account for that. Only a pool size of 1 was tested.
  • gemma and bge-small were not run, and the memory estimate is fitted to nomic.

@appwrite

appwrite Bot commented Nov 18, 2026 •

Copy link
Copy Markdown

🟢 Tier S · Ready to merge

The changed rejection path validates the full untruncated token count before inference and returns the intended 400 response.

The service now applies each model’s context limit, capped by EMBEDDING_MAX_TOKENS, and rejects over-limit input with a 400 before running inference. It counts unpadded input tokens, sizes inference sub-batches according to the configured limit, and documents the limits and memory guidance. Tests cover model-specific limits, rejection, and token counting.

Latest changes: The newest commits replace truncation with pre-inference token counting and rejection, map over-limit errors to HTTP 400, and update the documentation and integration tests accordingly.

Verdict New comments Fixed Still open
✅ Approved 0 0 0
📂 Walkthrough · 6
File Change
README.md Documents the token cap, rejection behavior, model limits, and memory considerations.
src/embedding.rs Configures model sequence lengths, counts unpadded input tokens before inference, rejects over-limit text, and scales sub-batches by sequence length.
src/error.rs Adds a distinct error variant for text that exceeds the token limit.
src/main.rs Maps over-limit errors to HTTP 400.
src/model.rs Defines per-model context lengths and tests the supported model limits.
tests/embed_e2e.rs Covers longer Nomic inputs, over-limit rejection, and token counts that exclude padding.

Reviewed the commits since e860c9c · Details · Comment @appwrite review to re-run, or mention @appwrite with a question.

@appwrite appwrite Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Tier S · Looks good to merge. Summary

@github-actions

github-actions Bot commented Nov 18, 2026 •

Copy link
Copy Markdown

Memory benchmark

Base af3536f vs head a96bc8f on ubuntu-latest.

Model nomic, EMBEDDING_POOL_SIZE=1, 12 requests of 16 texts (100 to 1,800 words), median of 2 runs. Retained is RSS 3s after the last request.

Concurrent requests Peak (base) Peak (head) Δ Retained (base) Retained (head) Δ Time (base) Time (head)
1 1759 MB 1759 MB +0% 591 MB 592 MB +0% 64.6s 64.7s
4 1772 MB 1776 MB +0% 600 MB 600 MB +0% 66.4s 65.2s

@HarshMN2345 HarshMN2345 changed the title fix(embedding): embed up to the model context and count those tokens fix(embedding): reject text over the model token limit Nov 18, 2026
@HarshMN2345
HarshMN2345 merged commit c3dc1bb into master Nov 18, 2026
4 checks passed
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 Report: nomic-embed-text silently truncates input at 512 tokens, and the reported token count is untruncated

1 participant