#40788·langchain

message_to_events drops AIMessage.additional_kwargs during replay

Author: aloktripathi1Created Sep 23, 2026Updated Sep 28, 2026
Labelsbugcoreexternal

Submission checklist

  • This is a bug, not a usage question.
  • I added a clear and descriptive title that summarizes this issue.
  • I used the GitHub search to find a similar question and didn't find it.
  • I am sure that this is a bug in LangChain rather than my code.
  • The bug is not resolved by updating to the latest stable version of LangChain (or the specific integration package).
  • This is not related to the langchain-community package.
  • I posted a self-contained, minimal, reproducible example. A maintainer can copy it and run it AS IS.

Package (Required)

  • langchain
  • langchain-openai
  • langchain-anthropic
  • langchain-classic
  • langchain-core
  • langchain-model-profiles
  • langchain-tests
  • langchain-text-splitters
  • langchain-chroma
  • langchain-deepseek
  • langchain-exa
  • langchain-fireworks
  • langchain-groq
  • langchain-huggingface
  • langchain-mistralai
  • langchain-nomic
  • langchain-ollama
  • langchain-openrouter
  • langchain-perplexity
  • langchain-qdrant
  • langchain-typesafe
  • langchain-xai
  • Other / not sure / general

Related Issues / PRs

No response

Reproduction Steps / Example Code (Python)

python
from langchain_core.language_models._compat_bridge import message_to_events
from langchain_core.messages import AIMessage

msg = AIMessage(
content="hello",
additional_kwargs={
"provider_state": {
"important": "preserve-me"
}
},
)

events = list(message_to_events(msg))

finish_event = next(
event for event in events
if event.get("event") == "message-finish"
)

print("Original:", msg.additional_kwargs)
print("Replayed:", finish_event.get("additional_kwargs"))

assert finish_event.get("additional_kwargs") == msg.additional_kwargs

Error Message and Stack Trace (if applicable)

bash

Description

message_to_events() does not preserve AIMessage.additional_kwargs when converting a finalized AIMessage into the v3 protocol event stream.

I am converting a finalized AIMessage with message_to_events() and expect the resulting message-finish event to retain the message's additional_kwargs.

Instead, the message-finish event does not contain additional_kwargs, causing the data to be lost during replay.

The behavior is inconsistent with the live streaming path, where additional_kwargs is preserved.

Expected behavior

The message-finish event produced by message_to_events() should preserve the original AIMessage.additional_kwargs.

For example:

Original:
{"provider_state": {"important": "preserve-me"}}

Replayed event:
{"provider_state": {"important": "preserve-me"}}

Actual behavior

The message-finish event does not contain the additional_kwargs field.

As a result, when the generated events are consumed to reconstruct the message, the original additional_kwargs are lost.

Why this matters

AIMessage.additional_kwargs can contain provider-specific information that is not represented by the standard message fields.

message_to_events() is used to replay finalized messages into the protocol event stream, including cache replay scenarios. Losing additional_kwargs during replay can therefore cause replayed execution to differ from the original execution.

This is also inconsistent with the existing live streaming path, which preserves additional_kwargs when assembling the final message.

There currently does not appear to be test coverage verifying that message_to_events() preserves AIMessage.additional_kwargs.

System Info

System Information

OS: Linux OS Version: #31-Ubuntu SMP PREEMPT_DYNAMIC Sat Aug 1 04:26:38 UTC 2026 Python Version: 3.11.15 (main, Jun 23 2026, 15:19:43) [Clang 22.1.3 ]

Package Information

langchain_core: 1.6.4 langsmith: 0.11.1 langchain_protocol: 0.0.18 langchain_tests: 1.1.9

Optional packages not installed

deepagents deepagents-cli

Other Dependencies

anyio: 4.14.2 distro: 1.9.0 httpx: 0.28.1 jsonpatch: 1.33 numpy: 2.3.5 orjson: 3.11.6 packaging: 26.3 pydantic: 2.13.4 pytest: 9.1.1 pytest-asyncio: 1.4.0 pytest-benchmark: 5.2.3 pytest-codspeed: 4.3.0 pytest-recording: 0.13.4 pytest-socket: 0.7.0 pyyaml: 6.0.3 requests: 2.33.0 requests-toolbelt: 1.0.0 rich: 14.2.0 sniffio: 1.3.1 syrupy: 5.5.3 tenacity: 9.1.4 typing-extensions: 4.16.0 uuid-utils: 0.17.0 vcrpy: 8.2.1 websockets: 16.0 wrapt: 2.0.1 xxhash: 3.6.0 zstandard: 0.25.0

Social handles (optional)

No response