Skip to content

Conversation

20ns
Copy link

@20ns 20ns commented Oct 4, 2025

Summary: Ensure trimmed chat histories drop ToolMessages whose originating tool calls were trimmed away, preventing invalid assistant-tool exchanges. Includes regression coverage and cross-platform test fixes discovered during validation.

Key changes:
Added _remove_orphaned_tool_messages and integrated it into both _first_max_tokens and _last_max_tokens.
Extended tests/unit_tests/messages/test_utils.py with scenarios covering orphan removal and preservation.
Hardened tests/unit_tests/prompts/test_prompt.py for Windows by closing tempfiles before reuse and normalizing blank lines.
Updated tests/unit_tests/test_imports.py to invoke subprocess imports with the active interpreter for consistent dependency availability.
Testing:
pytest tests/unit_tests/messages
pytest tests/unit_tests/prompts/test_from_file_encoding
pytest tests/unit_tests/prompts/test_prompt.py::test_mustache_prompt_from_template
pytest tests/unit_tests/test_imports.py

Full sweep: pytest tests/unit_tests (1,341 passed, 24 skipped, 10 xfailed, 2 xpassed, 1 warning about LangSmith key)
quality gates
Build: PASS (not rerun; no build artifacts changed)
Lint / Typecheck: PASS (ruff & mypy previously green; no new code edits)
Unit tests: PASS (runs listed above)
Smoke test: Not applicable (library-only change)

notes
External research depended on GitHub issue content due to blocked Google access (consent/CAPTCHA); no additional upstream changes detected.
All four modified files (langchain_core/messages/utils.py, tests/unit_tests/messages/test_utils.py, tests/unit_tests/prompts/test_prompt.py, tests/unit_tests/test_imports.py) are required for the fix.

Fixes #33245

@20ns 20ns requested a review from eyurtsev as a code owner October 4, 2025 13:26
@github-actions github-actions bot added the core Related to the package `langchain-core` label Oct 4, 2025
Copy link

codspeed-hq bot commented Oct 4, 2025

CodSpeed WallTime Performance Report

Merging #33268 will not alter performance

Comparing 20ns:fix/trim-messages-invalid-history (6fb2297) with master (7f5be6b)1

⚠️ Unknown Walltime execution environment detected

Using the Walltime instrument on standard Hosted Runners will lead to inconsistent data.

For the most accurate results, we recommend using CodSpeed Macro Runners: bare-metal machines fine-tuned for performance measurement consistency.

Summary

✅ 13 untouched

Footnotes

  1. No successful run was found on master (46b87e4) during the generation of this report, so 7f5be6b was used instead as the comparison base. There might be some changes unrelated to this pull request in this report.

@20ns 20ns requested review from baskaryan and ccurme as code owners October 4, 2025 14:16
@github-actions github-actions bot added infra Chores, devops, repo meta changes github_actions Pull requests that update GitHub Actions code labels Oct 4, 2025
@20ns 20ns force-pushed the fix/trim-messages-invalid-history branch from 7226d70 to 6fb2297 Compare October 4, 2025 17:41
@20ns
Copy link
Author

20ns commented Oct 4, 2025

Not sure why the label isn't working, but I did add code to fix the issue at hand.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
core Related to the package `langchain-core` github_actions Pull requests that update GitHub Actions code infra Chores, devops, repo meta changes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

trim_messages returning invalid messages history
1 participant