Skip to content

fix(langchain): preserve system messages during recall injection - #6098

Open
ktz03 wants to merge 1 commit into
volcengine:mainfrom
ktz03:fix/langchain-system-context-preservation
Open

ktz03 wants to merge 1 commit into
volcengine:mainfrom
ktz03:fix/langchain-system-context-preservation

Conversation

@ktz03

@ktz03 ktz03 commented Oct 15, 2026 •

Copy link
Copy Markdown
Contributor

Description

When recall context is injected, an existing typed SystemMessage is rebuilt with only flattened text. This drops its name, ID, additional fields and structured content blocks; a downstream filter_messages(include_names=[...]) can then omit the original named policy.

Preserve the existing message with model_copy, append one text block to list content, and keep the existing string formatting and caller input unchanged.

Human Involvement

  • A human participated in the implementation or review loop
  • This PR was generated entirely by AI agents without human participation in the loop

Related Issue

Fixes #6097

Type of Change

  • Bug fix (non-breaking change that fixes an issue)
  • New feature (non-breaking change that adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation update
  • Refactoring (no functional changes)
  • Performance improvement
  • Test update

Changes Made

  • Retain existing typed system-message fields when adding recalled context.
  • Preserve original content blocks and their fields without mutating the caller's message.
  • Add eight public runnable regressions for sync/async, string/block content and disabled/enabled injection.

Testing

  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • I have tested this on the following platforms:
    • Linux
    • macOS
    • Windows

The eight regressions are 4 failed / 4 passed on exact main and 8 passed with the fix. The scoped LangChain suites are Linux 231 passed / 1 SQLite skip; Windows 229 passed / 1 skip / 2 existing resource-path failures. Exact main with the same regressions is Windows 225 passed / 1 skip / 6 failures, including the four new regressions. Both Windows runs have the same two resource-path failures.

Ruff check/format (44 files), mypy (15 sources), wheel/sdist build, Twine, offline target install, the package's installed smoke and four installed public controls passed. Independent public name-filter controls are 3 normal / 3 divergent before the fix and all 6 normal after it. These overlapping selections are not added together.

Checklist

  • My code follows the project's coding style
  • I have performed a self-review of my code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • Any dependent changes have been merged and published

Screenshots (if applicable)

Not applicable.

Additional Notes

Validation used cached dependencies and the deterministic in-memory backend. Existing LangChain deprecation warnings remain. No deployed backend, live model, authentication, full-server, minimum-version matrix, provider cache-hit or performance claim is made.

The original integration/shared recording work by ehz0ah and host-history work by krsish remain intact. System messages remain excluded from persisted memory. This does not change the package layout proposed in #6017.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Backlog

Development

Successfully merging this pull request may close these issues.

[Bug]: LangChain recall injection drops existing SystemMessage fields and content blocks

1 participant