OpenViking icon indicating copy to clipboard operation
OpenViking copied to clipboard

Embedding truncation can return 29 characters more than 4 x max_input_tokens, so a file just over a character-limited provider's cap fails its add_resource task

Open ragnos-dev opened this issue 3 weeks ago • 2 comments

Version

OpenViking 0.5.3, built from upstream commit 4b92b3065. openviking/utils/embedding_input.py is the same on main today (last change #6134).

Summary

truncate_embedding_input() cuts ASCII text to exactly 4 * max_input_tokens characters and then appends EMBEDDING_TRUNCATION_SUFFIX ("\n...(truncated for embedding)", 29 characters). The truncated text is therefore longer than the bound it was cut to. Some providers set their input limit in characters at exactly that bound (here, 131,072 = 4 x 32,768 for a 32k-context embedding model), so the guard's own output is rejected.

We hit this indexing one large .jsonl file with parse_mode: "no_split". The whole file is one embedding input, so the 422 failed the entire add_resource task after every other file in the resource had already been written.

Minimal reproduction

from openviking.utils.embedding_input import truncate_embedding_input

out = truncate_embedding_input("a" * 200_000, 32768)
print(len(out))  # 131101, i.e. 4 * 32768 + 29

End to end: set embedding.max_input_tokens: 32768 with an OpenAI-compatible embedding provider that rejects inputs of 131,072 characters or more. Then call add_resource with parse_mode: "no_split" on a text file longer than 131,072 characters.

Observed

  • The provider returns 422 "input sequence should have less than 131072 characters" for an input of 131,101 characters.
  • The add_resource task fails as a whole, late, after the other files were written.

Expected

Truncated text never exceeds the bound it was truncated to, so a provider limit equal to 4 * max_input_tokens characters is respected. Ideally, one oversize file would fail or be skipped for vectorization on its own rather than failing the whole task.

Code locations

  • openviking/utils/embedding_input.py:29-30: ASCII branch returns text[:max_units].rstrip() + suffix, where max_units = max_tokens * 4. The non-ASCII branches (lines 38-49) also add the suffix after filling the full budget.
  • openviking/utils/embedding_utils.py:647 and :680: raw file content passes through this helper before vectorization (the no_split / content_only path).
  • openviking/models/embedder/base.py:342: the embedder-level guard calls the same helper for plain text (and at :332 for multimodal text parts), so it hands back the same 131,101-character string.

Workaround we use

We re-add the file without parse_mode: "no_split". Setting max_input_tokens a few tokens below the provider limit (for example 32760) also avoids it.

Ask

  1. Reserve the suffix inside the budget: cut the prefix to max_units - len(suffix) (or the token equivalent) so len(result) <= 4 * max_input_tokens for ASCII input. A one-line test of the reproduction above would cover it.
  2. Optionally, when a single file's embedding is rejected for length, record that file as failed or not vectorized instead of failing the whole add_resource task.

Related but different: #6129 and #6418 (estimate accuracy), #5760 (list inputs skip the guard), #931 (no guard, since fixed).

ragnos-dev avatar Nov 09 '26 16:11 ragnos-dev

Same family as #6129, and there are two independent defects in that function.

  1. The suffix is spent outside the budget. truncate_embedding_input() cuts to 4 * max_input_tokens characters, then appends the 29-character EMBEDDING_TRUNCATION_SUFFIX, so the guard's own output is always longer than the bound it enforces. Reserving len(EMBEDDING_TRUNCATION_SUFFIX) inside the budget fixes that half regardless of the tokenizer.

  2. The 4 chars/token constant is optimistic for the text that memory cards are actually made of. Measured on our stack (llama.cpp server, embeddinggemma-2-BF16, POST /tokenize, equal-length inputs): plain Russian prose 204 chars -> 51 tokens, 4.00 chars/token; Russian plus markdown plus viking:// URIs, 209 chars -> 69 tokens, 3.03 chars/token. That is a ~25% underestimate on exactly the mixed technical text, before any CJK or code is involved.

#6140 replaces the flat constant with per-script rates (CJK 1.0, Cyrillic 0.5, other 0.4 tokens/char). Worth checking whether it also budgets the suffix; if not, both fixes are needed and they are independent.

Workaround we run in the meantime: pin embedding.max_input_tokens to ~75% of the provider's character limit and keep parse_mode: "no_split" inputs under the same bound at ingest, so the guard never has to truncate at all.

godxxs avatar Nov 13 '26 13:11 godxxs

@godxxs I checked the current #6140 head (e7a561e86f80): it already reserves the default marker before selecting the prefix, using suffix_units = len(suffix) * _ORDINARY_TENTHS and subtracting that from the budget (source). That PR is still open.

The focused suffix/small-budget fix is #7307, also still open at 92a5364860f7. Its existing validation covers the character bound, including small positive budgets; it retains the approximate token estimate. The original provider's acceptance of 131071 versus 131072 characters remains unconfirmed. Your embeddinggemma tokenizer measurements address the separate estimate-accuracy question.

AI-only source comparison; no new provider/tokenizer requests or repeated tests.

r266-tech avatar Nov 13 '26 13:11 r266-tech