Skip to content

Conversation

@aroun-coumar
Copy link

Description:

Closes #33883

Chat model cache keys are generated by serializing messages via dumps(messages). The optional BaseMessage.id field (a UUID used solely for tracing/threading) is included in this serialization, causing functionally identical messages to produce different cache keys. This results in repeated API calls, cache bloat, and degraded performance in production workloads (e.g., agents, RAG chains, long conversations).

This change normalizes messages only for cache key generation by stripping the nonsemantic id field using Pydantic V2’s model_copy(update={"id": None}). The normalization is applied in both synchronous and asynchronous cache paths (_generate_with_cache / _agenerate_with_cache) immediately before dumps().

normalized_messages = [
    msg.model_copy(update={"id": None})
    if getattr(msg, "id", None) is not None
    else msg
    for msg in messages
]
prompt = dumps(normalized_messages)

@github-actions github-actions bot added core Related to the package `langchain-core` fix labels Nov 10, 2025
@codspeed-hq
Copy link

codspeed-hq bot commented Nov 10, 2025

CodSpeed Performance Report

Merging #33915 will not alter performance

Comparing aroun-coumar:fix/cache-strip-message-ids (608e470) with master (69c7d1b)

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

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.

@aroun-coumar aroun-coumar force-pushed the fix/cache-strip-message-ids branch from dea75a2 to d1a96c7 Compare November 10, 2025 16:29
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` fix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant