Skip to content

Conversation

@Aman071106
Copy link

Title

feat(core): eager validation for trim_messages configuration

Description

This PR addresses a TODO in langchain_core/messages/utils.py to ensure that validation errors for trim_messages arguments (such as invalid token_counter shortcuts or incompatible strategy parameters) are raised at initialization time rather than at runtime.

Issue Addressed

Previously, trim_messages was decorated with @_runnable_support. When used to create a Runnable (i.e., called without messages), validation was deferred until invoke() was called. This delayed feedback for invalid configurations.

Changes

  • Removed @_runnable_support decorator from trim_messages.
  • Implemented manual Runnable creation logic:
    • Arguments are now validated before checking if messages is None.
    • If messages is None, a RunnableLambda is returned with the already-validated arguments bound.
  • Updated test_trim_messages_bad_token_counter in tests/unit_tests/messages/test_utils.py to assert that ValueError is raised at initialization.

Verification

  • Ran existing unit tests: uv run --group test pytest tests/unit_tests/messages/test_utils.py
  • All tests passed, including the updated validation test.

@Aman071106 Aman071106 requested a review from eyurtsev as a code owner December 29, 2025 17:34
@github-actions github-actions bot added feature For PRs that implement a new feature; NOT A FEATURE REQUEST core `langchain-core` package issues & PRs and removed feature For PRs that implement a new feature; NOT A FEATURE REQUEST labels Dec 29, 2025
@codspeed-hq
Copy link

codspeed-hq bot commented Dec 29, 2025

CodSpeed Performance Report

Merging #34528 will not alter performance

Comparing Aman071106:feat/eager-validation-trim-messages (a4c4c1c) with master (03ae397)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
⏩ 21 skipped2

Footnotes

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

  2. 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.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant