Propagate element metadata to chunks in MEDI chunkers#7516
Open
luisquintanilla wants to merge 1 commit intomainfrom
Open
Propagate element metadata to chunks in MEDI chunkers#7516luisquintanilla wants to merge 1 commit intomainfrom
luisquintanilla wants to merge 1 commit intomainfrom
Conversation
Fix #7465: All four IngestionChunker implementations (SectionChunker, HeaderChunker, SemanticSimilarityChunker, DocumentTokenChunker) now propagate IngestionDocumentElement.Metadata to IngestionChunk.Metadata. Design decisions: - First-wins merge strategy (TryAdd) for conflicting keys - Null metadata values skipped (element allows object?, chunk requires object) - Split elements: metadata goes to the first chunk only - Lazy allocation: dictionary only created when elements have metadata ElementsChunker (fixes SectionChunker, HeaderChunker, SemanticSimilarityChunker): - Added AccumulateMetadata/ApplyMetadata static helpers - Accumulates metadata as elements are processed - Applies to chunk on commit, then clears accumulator DocumentTokenChunker: - Added AccumulateMetadata static helper - Accumulates metadata during element iteration - Applies in FinalizeChunk, then clears accumulator Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Contributor
There was a problem hiding this comment.
Pull request overview
This PR addresses MEDI ingestion metadata loss by propagating IngestionDocumentElement.Metadata into IngestionChunk<string>.Metadata across the built-in chunkers, so downstream components (e.g., VectorStoreWriter) can persist element-derived metadata on produced chunks.
Changes:
- Added element-metadata accumulation/application logic to
ElementsChunker(affectingSectionChunker,HeaderChunker, andSemanticSimilarityChunker). - Added similar metadata accumulation/application to
DocumentTokenChunkerduring element iteration and chunk finalization. - Introduced a new test suite validating metadata propagation behavior for several chunkers and scenarios.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| test/Libraries/Microsoft.Extensions.DataIngestion.Tests/Chunkers/ChunkerMetadataPropagationTests.cs | Adds tests asserting element metadata is propagated to chunk metadata under various conditions. |
| src/Libraries/Microsoft.Extensions.DataIngestion/Chunkers/ElementsChunker.cs | Accumulates element metadata while building chunks and applies it when committing chunks. |
| src/Libraries/Microsoft.Extensions.DataIngestion/Chunkers/DocumentTokenChunker.cs | Accumulates element metadata during token chunking and applies it when finalizing chunks. |
Comment on lines
+71
to
75
| AccumulateMetadata(element, ref accumulatedMetadata); | ||
|
|
||
| int elementTokenCount = CountTokens(semanticContent.AsSpan()); | ||
| if (elementTokenCount + totalTokenCount <= _maxTokensPerChunk) | ||
| { |
Comment on lines
55
to
80
| { | ||
| continue; | ||
| } | ||
|
|
||
| AccumulateMetadata(element, ref accumulatedMetadata); | ||
|
|
||
| int contentToProcessTokenCount = _tokenizer.CountTokens(elementContent!, considerNormalization: false); | ||
| ReadOnlyMemory<char> contentToProcess = elementContent.AsMemory(); | ||
| while (stringBuilderTokenCount + contentToProcessTokenCount >= _maxTokensPerChunk) | ||
| { | ||
| int index = _tokenizer.GetIndexByTokenCount( | ||
| text: contentToProcess.Span, | ||
| maxTokenCount: _maxTokensPerChunk - stringBuilderTokenCount, | ||
| out string? _, | ||
| out int _, | ||
| considerNormalization: false); | ||
|
|
||
| unsafe | ||
| { | ||
| fixed (char* ptr = &MemoryMarshal.GetReference(contentToProcess.Span)) | ||
| { | ||
| _ = stringBuilder.Append(ptr, index); | ||
| } | ||
| } | ||
| yield return FinalizeChunk(); | ||
| yield return FinalizeChunk(ref accumulatedMetadata); | ||
|
|
Comment on lines
+26
to
+32
| private static IngestionChunker<string> CreateDocumentTokenChunker(int maxTokensPerChunk = 2_000) | ||
| { | ||
| var tokenizer = TiktokenTokenizer.CreateForModel("gpt-4o"); | ||
| return new DocumentTokenChunker(new(tokenizer) { MaxTokensPerChunk = maxTokensPerChunk, OverlapTokens = 0 }); | ||
| } | ||
|
|
||
| [Fact] |
|
MEDI is an acronym regularly used to reference Microsoft.Extensions.DependencyInjection - using it for this library is another reason why these AI technology packages should not be in the root Microsoft.Extensions namespace. |
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 #7465
All four built-in
IngestionChunkerimplementations (SectionChunker,HeaderChunker,SemanticSimilarityChunker,DocumentTokenChunker) now propagateIngestionDocumentElement.MetadatatoIngestionChunk<T>.Metadata.Problem
The chunkers never read element metadata, so any metadata attached to document elements (e.g., page numbers, source URIs, element types) was silently dropped during chunking. This meant
VectorStoreWriter- which already correctly persists chunk metadata - had nothing to write.Solution
ElementsChunker (internal, fixes 3 public chunkers)
AccumulateMetadata/ApplyMetadatastatic helpersDocumentTokenChunker (independent chunker)
AccumulateMetadatastatic helperFinalizeChunk, then clears the accumulatorDesign Decisions
Testing
ChunkerMetadataPropagationTestscovering all scenariosMicrosoft Reviewers: Open in CodeFlow