[BUG] Preserve scalar loader metadata during RAG ingestion
Python PR scope
Fix the reproduced loss of LoaderResult.metadata in RAG.add(). Current code initializes each chunk's metadata from only caller-supplied metadata, dropping loader fields such as JSON format/type/size.
Difficulty: easy. Allocation index: #8118. Project: #8119.
Cohort allocation
This ticket is reserved for maintainer-coordinated allocation to the Stanford semester collaboration. Please wait for @lorenzejay to confirm an assignee before implementing. A comment expresses interest; it does not establish ownership. Public good first issue / help wanted discovery labels will be considered after cohort allocation.
GitHub usernames have not yet been supplied, so no individual assignment is implied.
Acceptance criteria
- [ ] Add a regression with a fake loader returning scalar metadata and a fake collection; show failure on current code and success after the fix.
- [ ] Merge loader and caller metadata with an explicit precedence rule; caller fields should be able to override descriptive loader fields.
- [ ] Protect generated source, doc_id, data_type, and chunk_index from accidental overrides.
- [ ] Do not mutate the caller's dictionary or the LoaderResult metadata.
- [ ] Define handling of unsupported loader metadata value types using existing storage conventions without changing unrelated caller metadata behavior.
Dependencies and coordination
Can start independently of the other cohort implementation tickets.
Evidence
Executed the actual RAG.add method extracted from source with fake loader/chunker/embeddings/collection. Loader metadata {'format': 'json', 'size': 1} was absent from stored metadata, while caller metadata {'tag': 'user'} survived. The same implementation was checked on current GitHub main.
Starting points
- lib/crewai-tools/src/crewai_tools/rag/core.py
- lib/crewai-tools/src/crewai_tools/rag/base_loader.py
- lib/crewai-tools/src/crewai_tools/rag/loaders/json_loader.py
- lib/crewai-tools/tests/rag
Submission
The contributor implements this scope and submits one focused PR linked to this open issue. Include full Python annotations, behavioral tests, and relevant quality checks. No paid API calls are required for the tests. Preserve existing APIs outside the scoped feature. For newly found defects, reproduce them and check active issues/PRs before fixing them. Docs edits, when needed, belong under docs/edge/ and must sync ar, ko, pt-BR per DOCS_TRANSLATIONS.md. Follow .github/CONTRIBUTING.md. Issue prepared with AI assistance.
I would like to work on this issue. Could you assign me if a cohort slot is available? I will wait for maintainer confirmation before implementing, as requested in the allocation notes.
This issue is stale because it has been open for 30 days with no activity. Remove stale label or comment or this will be closed in 5 days.
Hey @Lorenze Jay! I'm alanhuang05 on GitHub, in the CS 146S cohort. I noticed #8134 is still open with no PR and was recently marked stale. Is it available for reassignment? I'd be happy to take it and submit a focused fix with regression tests. Thanks!
Hi @lorenzejay and @pei711! I'm also part of the Stanford CS 146S cohort and went ahead and submitted [PR #9480] to address this issue, since the assignment deadline is coming up.
The fix is pretty small and focused. It preserves loader metadata during RAG ingestion, handles metadata precedence and sanitization, and includes regression tests. The relevant tests are passing locally.
I realize this issue was already assigned, so apologies for jumping in! I'm happy to coordinate with the existing assignee, make any requested changes, or close my PR if you'd prefer. Just wanted to get a contribution submitted for the class deadline.
Thanks so much!