Fix TokenCountBatchingStrategy ignoring metadata when sizing batches - #6782
Open
subhashpolisetti wants to merge 1 commit into
Open
Conversation
The metadata-aware embedding models send `Document.getFormattedContent(MetadataMode.EMBED)` to the embedding API, but `TokenCountBatchingStrategy` estimated token counts with `MetadataMode.NONE`. Both are auto-configured defaults in the same application context: `OpenAiEmbeddingProperties` binds `MetadataMode.EMBED` into the model, while every vector store auto-configuration registers the no-argument `TokenCountBatchingStrategy`. The default path therefore counted the document text and then submitted that text plus its metadata. Batches exceeded the limit they were sized for, and the guard that rejects an oversized document measured a different string than the one submitted: with the default 8191 token count and 10% reserve, a document counted at 7001 tokens is accepted and then sent as 8440, past both the 7371 token budget the batch was sized for and the model's own input limit. Estimate with `MetadataMode.EMBED` instead. For models that do not send metadata the estimate becomes conservative rather than wrong, so batches stay within the limit either way. The constructor taking an explicit `MetadataMode` is unchanged, so an application embedding text only can still request `MetadataMode.NONE` directly. Signed-off-by: subhash polisetti <subhashr161347@gmail.com>
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.
The metadata-aware embedding models send
Document.getFormattedContent(MetadataMode.EMBED)to the embedding API, butTokenCountBatchingStrategyestimated token counts withMetadataMode.NONE. Both areauto-configured defaults in the same application context:
OpenAiEmbeddingPropertiesbindsMetadataMode.EMBEDinto the model, while every vector store auto-configuration registersthe no-argument
TokenCountBatchingStrategy. The default path therefore counted the documenttext and then submitted that text plus its metadata.
Batches exceeded the limit they were sized for, and the guard that rejects an oversized
document measured a different string than the one submitted: with the default 8191 token
count and 10% reserve, a document counted at 7001 tokens is accepted and then sent as 8440,
past both the 7371 token budget the batch was sized for and the model's own input limit.
Estimate with
MetadataMode.EMBEDinstead. For models that do not send metadata the estimatebecomes conservative rather than wrong, so batches stay within the limit either way. The
constructor taking an explicit
MetadataModeis unchanged, so an application embedding textonly can still request
MetadataMode.NONEdirectly.Testing
Added
batchRejectsDocumentWhoseMetadataPushesItOverTheLimitandbatchSplitsOnTheTokenCountIncludingMetadata, which both fail without the change (the metadatais not counted, so the oversized document is accepted and the three documents are placed in a
single batch) and pass with it.
batchWithExplicitMetadataModeNoneIgnoresMetadatacovers theopt-out and passes either way, pinning the behaviour of the constructor that takes an explicit
MetadataMode. Existing batching tests are unaffected. No credentials are needed: the tests usethe real
JTokkitTokenCountEstimatorwith small token limits../mvnw -pl spring-ai-model clean testpasses (837 tests).