Add metadata support to Knowledge Source classes #3813
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.
Add metadata support to Knowledge Source classes
Summary
Implements metadata support for Knowledge Source embeddings to enable file tracking, chunk identification, and targeted deletion/filtering as requested in #3812. All Knowledge Source classes now emit chunks as dictionaries with metadata (filepath, chunk_index, source_type) instead of plain strings, while maintaining backward compatibility through the
_coerce_to_records()function.Key Changes:
_coerce_to_records()function to handle both string and dict chunk formats for backward compatibilityKnowledgeStorage.save()signature to acceptlist[str] | list[dict[str, Any]]filepath: Source file pathchunk_index: Index of chunk within filesource_type: Type of source (text_file, pdf, csv, json, excel, string, docling)sheet_name: (Excel only) Sheet name for multi-sheet workbooksConversionResult.input.fileinstead of indexing, preventing misalignment when files fail conversionReview & Testing Checklist for Human
This is a high-risk change that modifies core data structures and storage behavior. Please verify:
Test Plan
uv run pytest tests -vvNotes
chunksattribute in BaseKnowledgeSource is still typed aslist[str]but now containslist[dict]. This should be addressed in a follow-up PR to avoid type confusion.Session: https://app.devin.ai/sessions/030655ad0c344a22b079b478b9f4b015
Requested by: João (joao@crewai.com)