Skip to content

fix: trim_messages breaks when token_counter is a per-message c...#35669

Closed
Zakir Jiwani (JiwaniZakir) wants to merge 1 commit into
langchain-ai:masterfrom
JiwaniZakir:fix/issue-35629
Closed

fix: trim_messages breaks when token_counter is a per-message c...#35669
Zakir Jiwani (JiwaniZakir) wants to merge 1 commit into
langchain-ai:masterfrom
JiwaniZakir:fix/issue-35629

Conversation

@JiwaniZakir

Copy link
Copy Markdown

Summary

This addresses #35629.

What changed

Implemented a fix based on the issue description. See the diff for specifics.

Testing

  • Verified against the existing test suite
  • Checked that the fix addresses the reported behavior

…unter (langchain-ai#35629)

The annotation identity check (`is BaseMessage`) failed for subclass
annotations, string annotations, and postponed annotations. Replace it
with `_is_per_message_token_counter` that uses `get_type_hints` for
proper resolution and `issubclass` for subclass support. Add
`token_counter_is_per_message` flag for unannotated callables and
lambdas.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
@codspeed-hq

codspeed-hq Bot commented Mar 8, 2026

Copy link
Copy Markdown

Merging this PR will degrade performance by 28.81%

⚠️ 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.

❌ 13 regressed benchmarks
⏩ 23 skipped benchmarks1

⚠️ Please fix the performance issues or acknowledge them on CodSpeed.

Performance Changes

Mode Benchmark BASE HEAD Efficiency
WallTime test_import_time[RunnableLambda] 439.3 ms 528.7 ms -16.91%
WallTime test_import_time[PydanticOutputParser] 475.2 ms 581.9 ms -18.34%
WallTime test_async_callbacks_in_sync 18.6 ms 26.1 ms -28.81%
WallTime test_import_time[InMemoryVectorStore] 550.3 ms 651.7 ms -15.55%
WallTime test_import_time[CallbackManager] 284.9 ms 337.5 ms -15.6%
WallTime test_import_time[HumanMessage] 239.9 ms 280.3 ms -14.42%
WallTime test_import_time[Runnable] 448.4 ms 524.4 ms -14.49%
WallTime test_import_time[tool] 484.6 ms 589.1 ms -17.75%
WallTime test_import_time[ChatPromptTemplate] 563.1 ms 680.2 ms -17.21%
WallTime test_import_time[LangChainTracer] 409.5 ms 490.8 ms -16.55%
WallTime test_import_time[BaseChatModel] 481.4 ms 567 ms -15.1%
WallTime test_import_time[InMemoryRateLimiter] 156.3 ms 183.6 ms -14.85%
WallTime test_import_time[Document] 170.8 ms 198.5 ms -13.95%

Comparing JiwaniZakir:fix/issue-35629 (ee3d55b) with master (fbfe4b8)

Open in CodSpeed

Footnotes

  1. 23 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.

@gautamvarmadatla

GAUTAM V DATLA (gautamvarmadatla) commented Mar 8, 2026

Copy link
Copy Markdown

Hi Zakir Jiwani (@JiwaniZakir) ! This is the exact same fix as my original PR #35630. Please stop spamming AI-generated duplicate fixes. And read the thread first on issue please

@ccurme ccurme (ccurme) left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Duplicated with #35630

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 fix For PRs that implement a fix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants