fix: batch token counting during indexing to prevent unbounded plugin requests - #39571
Open
mesrop2008 wants to merge 1 commit into
Open
fix: batch token counting during indexing to prevent unbounded plugin requests#39571mesrop2008 wants to merge 1 commit into
mesrop2008 wants to merge 1 commit into
Conversation
mesrop2008
requested review from
JohnJyong,
QuantumGhost and
laipz8200
as code owners
July 25, 2026 11:31
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.
Summary
Fixes #39560
Token counting during indexing sent the entire document's chunks to the
embedding plugin in a single, unbounded request via
get_text_embedding_num_tokens, while the embedding call itself was alreadybatched via
MAX_CHUNKSincached_embedding.py. For large documents thisproduced a single multi-megabyte request that could fail with
413 Payload Too Largefrom an intermediate proxy, OOM-kill a memory-constrainedplugin-daemon, or exceed a tokenizer backend's own batch limits (e.g. TEI, as
previously seen in #15035) — always before a single embedding request was
even made.
Changes
api/core/rag/embedding/token_counter.py:calculate_segment_token_countsnow batches texts using the sameModelPropertyKey.MAX_CHUNKSlogic already used bycached_embedding.py,concatenating per-batch results in order (the function already returned
one token count per document, so no summing is needed).
(
_iter_batches), so a single oversized chunk — or text that expandsafter encoding — can't produce an oversized batch even when the chunk
count alone would allow it.
Testing
MAX_CHUNKS, batching by byte-sizecap independent of chunk count, and the existing economy/empty-document/
high-quality code paths (updated to account for the new schema lookup).
dify-apiimage from this branch, swapped it into an existing DockerCompose stack via
docker-compose.override.yaml, and indexed a ~17 MBdocument split into ~100-character segments (18,500 chunks) with
high_qualityindexing. Worker logs show hundreds of small, successful.../text_embedding/num_tokensrequests instead of one unbounded request,and indexing completed without any 413/OOM/timeout failure at the
token-counting stage.
Screenshots
N/A — backend-only change; see worker log excerpt in the linked issue
discussion for before/after request pattern.
Checklist
uv run ruff format/uv run ruff checkon the changed files anduv run pyteston the affected test file to appease the lint gods