Skip to content

fix(core): fix trim_messages misclassification of per-message token_counter#18

Open
LennyMalcolm0 wants to merge 5 commits into
masterfrom
devasign-clone/pr-35630
Open

fix(core): fix trim_messages misclassification of per-message token_counter#18
LennyMalcolm0 wants to merge 5 commits into
masterfrom
devasign-clone/pr-35630

Conversation

@LennyMalcolm0

Copy link
Copy Markdown
Owner

Fixes : langchain-ai#35629

I replaced the brittle annotation is BaseMessage check in trim_messages with get_type_hints() to resolve annotations to live types where possible and issubclass() to correctly match BaseMessage and its subclasses. This should fix misclassification for subclass annotations like HumanMessage, string/forward-reference annotations, and common cases involving postponed annotation evaluation. I also added a token_counter_is_per_message flag as an explicit escape hatch for lambdas and unannotated callables where auto-detection cannot work. Also added regression tests covering:

  • exact BaseMessage annotation
  • subclass annotation
  • string annotation
  • lambda with explicit override
  • unannotated function with explicit override
  • list-based counter backwards compatibility
  • precedence of get_num_tokens_from_messages

Cloned from langchain-ai#35630 for DevAsign stress testing.

@devasign-test-app

devasign-test-app Bot commented Jul 9, 2026

Copy link
Copy Markdown

AI review failed to complete. Please retry.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

trim_messages breaks when token_counter is a per-message callable (lambda, subclass annotation, or postponed annotations)

2 participants