Skip to content

Conversation

@Aman071106
Copy link
Contributor

@Aman071106 Aman071106 commented Jan 2, 2026

PR Title

refactor(core): eagerly validate trim_messages arguments


PR Description

This PR refactors trim_messages in langchain-core to validate its arguments (specifically token_counter, strategy, start_on, and include_system) eagerly at initialization time, rather than lazily when the returned Runnable is invoked.


Motivation

Previously, trim_messages was decorated with @_runnable_support, which deferred argument validation until the returned Runnable was actually invoked. This caused errors to surface late in the execution flow, making debugging harder.

This refactor removes the decorator and manually constructs a RunnableLambda, enabling fail-fast behavior by validating inputs immediately and improving developer experience.


Changes

  • Removed the @_runnable_support decorator from trim_messages.
  • Added eager validation for:
    • token_counter
    • compatibility between strategy and token_counter
    • other related arguments (start_on, include_system).
  • Manually handles the messages=None case to return a RunnableLambda after validation, preserving original behavior.
  • Updated test_trim_messages_bad_token_counter in test_utils.py to expect an eager ValueError.

Related Issues

  • Addresses the TODO in libs/core/langchain_core/messages/utils.py.

Checklist

  • I have run relevant tests locally and they pass
    (libs/core/tests/unit_tests/messages/test_utils.py)
  • Added/updated tests
    • Updated test_trim_messages_bad_token_counter to assert eager validation.

@Aman071106 Aman071106 requested a review from eyurtsev as a code owner January 2, 2026 16:54
@github-actions github-actions bot added the core `langchain-core` package issues & PRs label Jan 2, 2026
@codspeed-hq
Copy link

codspeed-hq bot commented Jan 2, 2026

Merging this PR will not alter performance

⚠️ 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 benchmarks
⏩ 21 skipped benchmarks1


Comparing Aman071106:fix/trim-messages-eager-validation (451bef2) with master (c1f1641)2

Open in CodSpeed

Footnotes

  1. 21 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

  2. No successful run was found on master (0438f8c) during the generation of this report, so c1f1641 was used instead as the comparison base. There might be some changes unrelated to this pull request in this report.

@Aman071106 Aman071106 changed the title core: refactor trim_messages to validate arguments eagerlyclea refactor(core): eagerly validate trim_messages arguments Jan 2, 2026
@github-actions github-actions bot added the refactor PRs that include a refactor label Jan 2, 2026
@Aman071106 Aman071106 force-pushed the fix/trim-messages-eager-validation branch 2 times, most recently from dc05a73 to a25057c Compare January 2, 2026 17:27
@Aman071106 Aman071106 marked this pull request as draft January 2, 2026 17:28
@Aman071106 Aman071106 force-pushed the fix/trim-messages-eager-validation branch 4 times, most recently from ddf588b to f3255c5 Compare January 6, 2026 18:30
@Aman071106 Aman071106 force-pushed the fix/trim-messages-eager-validation branch from f3255c5 to 3fd9376 Compare January 6, 2026 18:34
@github-actions github-actions bot added refactor PRs that include a refactor and removed refactor PRs that include a refactor labels Jan 6, 2026
@Aman071106 Aman071106 marked this pull request as ready for review January 6, 2026 19:06
@github-actions github-actions bot added refactor PRs that include a refactor and removed refactor PRs that include a refactor labels Jan 6, 2026
@mdrxy mdrxy changed the title refactor(core): eagerly validate trim_messages arguments refactor(core): eagerly validate trim_messages arguments Jan 8, 2026
@mdrxy mdrxy self-assigned this Jan 8, 2026
@github-actions github-actions bot added refactor PRs that include a refactor and removed refactor PRs that include a refactor labels Jan 8, 2026
@Aman071106
Copy link
Contributor Author

Hi , just a gentle bump on this. I've rebased on the latest main to ensure there are no conflicts with the recent 100+ commits, and verified the linting passes.

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

Labels

core `langchain-core` package issues & PRs refactor PRs that include a refactor

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants