Skip to content

fix(lfx): preserve document metadata during conversion - #14677

Open
tandede wants to merge 1 commit into
langflow-ai:release-1.12.0from
tandede:fix/preserve-document-metadata
Open

fix(lfx): preserve document metadata during conversion#14677
tandede wants to merge 1 commit into
langflow-ai:release-1.12.0from
tandede:fix/preserve-document-metadata

Conversation

@tandede

@tandede tandede commented Aug 20, 2026

Copy link
Copy Markdown

Summary

  • Copy a LangChain Document's metadata before adding its page content to the converted Data object.
  • Add regression coverage proving that conversion does not modify an existing metadata text value on the source document.

Problem

Data.from_document() assigned document.metadata directly to a local variable and then wrote the page content into its text key. That write mutated the input Document, overwriting an existing metadata value or adding a new key as a side effect. Callers that reuse the original document after conversion therefore observe changed metadata.

The reverse conversion already copies Data.data before removing its text field. This change applies the same non-mutating boundary to from_document(). The converted Data output is unchanged; only the source document is preserved.

A shallow copy is sufficient because the conversion only writes one top-level key.

Validation

  • Verified the regression test fails before the fix because document.metadata["text"] changes from "Metadata text" to "Doc content".
  • uv run --isolated --package lfx pytest src/lfx/tests/unit/test_data_class.py src/lfx/tests/unit/schema src/lfx/tests/unit/helpers -q — 392 passed, 1 skipped.
  • Ruff formatting and lint checks pass for both changed files.
  • uv run --isolated --package lfx lfx --help completes successfully.

Summary by CodeRabbit

  • Bug Fixes
    • Improved document conversion to preserve the original metadata.
    • Ensured metadata remains intact when it includes a key matching the document’s content field.

@coderabbitai

coderabbitai Bot commented Aug 20, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: 30b9a780-59fa-474a-a1ad-d7a8d8faf6ec

📥 Commits

Reviewing files that changed from the base of the PR and between 1655191 and 8cf3bd4.

📒 Files selected for processing (2)
  • src/lfx/src/lfx/schema/data.py
  • src/lfx/tests/unit/test_data_class.py

Included review availability: Your plan provides up to 10 included reviews per hour; 9 remain after this review.


Walkthrough

JSON.from_document now copies document metadata before adding page content. The unit test verifies converted content and metadata while confirming that the source Document.metadata remains unchanged.

Changes

Document conversion

Layer / File(s) Summary
Metadata copy and conversion validation
src/lfx/src/lfx/schema/data.py, src/lfx/tests/unit/test_data_class.py
from_document copies metadata before adding the text field. The test verifies converted content and metadata and confirms that the original metadata is preserved.

Estimated code review effort: 1 (Trivial) | ~5 minutes

Merge Risk: ⚪ Minimal · up to 8cf3b

This change prevents document metadata from being mutated during conversion while preserving the converted output; no actionable merge-blocking risk remains after normal checks and review.

Suggested reviewers: ogabrielluiz

🚥 Pre-merge checks | ✅ 8 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 33.33% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (8 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the main change: preserving document metadata during conversion.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Test Coverage For New Implementations ✅ Passed The PR adds regression coverage in test_data_class.py; it verifies converted text/metadata and preserves a source text metadata value, matching the changed from_document behavior.
Test Quality And Coverage ✅ Passed The pytest regression test checks converted text and metadata, and verifies the source metadata remains unchanged when its text key conflicts; this directly covers the changed behavior.
Test File Naming And Structure ✅ Passed The changed test is in the unit directory, uses the pytest-discovered test_.py/test_ pattern, and covers conversion output plus preservation of conflicting metadata; frontend and integration rule...
Excessive Mock Usage Warning ✅ Passed The changed test uses a real LangChain Document and Data conversion. It adds no Mock, MagicMock, patch, or monkeypatch usage, so no excessive mocking is introduced.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@github-actions github-actions Bot added the bug Something isn't working label Aug 20, 2026
@tandede
tandede marked this pull request as ready for review August 20, 2026 10:22
@github-actions github-actions Bot added bug Something isn't working and removed bug Something isn't working labels Aug 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant