mirror of
https://github.com/hwchase17/langchain.git
synced 2026-06-09 10:17:00 +00:00
fix(openrouter): merge fragmented reasoning_details in streaming (#36401)
## Description Fixes #36400 During streaming, `AIMessageChunk.__add__` list-concatenates `reasoning_details` in `additional_kwargs`, fragmenting a single entry into many. When `_convert_message_to_dict()` serializes conversation history back to the OpenRouter API for the next turn, these fragmented entries cause `BadRequestResponseError`. ### Changes - Add `_merge_reasoning_details()` helper that merges consecutive entries sharing the same `type` and `index` (streaming fragments) while preserving distinct entries (legitimate non-streaming data) - Metadata from later fragments (e.g. `signature`) is preserved in the merged result - Entries without `index` are never merged (safe for non-streaming responses) - Call `_merge_reasoning_details()` in `_convert_message_to_dict()` before serializing `reasoning_details` ### Why merge instead of drop? Non-streaming users (`invoke()`) rely on `reasoning_details` for structured metadata (`type`, `signature`, `format`, `index`). Dropping it entirely would be a regression. This approach fixes streaming while preserving non-streaming functionality, similar to `langchain-openai`'s `_implode_reasoning_blocks()`. ## Test plan - [x] Fragmented entries (same type + same index) are merged into one - [x] Distinct entries (different index) are preserved separately - [x] Entries without index are never merged - [x] Metadata from later fragments (e.g. signature) is preserved - [x] Single-entry lists pass through unchanged - [x] Round-trip (dict → message → dict) works correctly - [x] All 210 unit tests pass --------- Co-authored-by: Mason Daugherty <github@mdrxy.com> Co-authored-by: Mason Daugherty <mason@langchain.dev>
This commit is contained in:
@@ -1166,6 +1166,86 @@ def _format_message_content(content: Any) -> Any:
|
||||
return content
|
||||
|
||||
|
||||
def _merge_reasoning_run(run: list[dict[str, Any]]) -> dict[str, Any]:
|
||||
"""Merge a run of consecutive same-`(type, index)` reasoning fragments."""
|
||||
merged_entry: dict[str, Any] = {}
|
||||
text_parts: list[str] = []
|
||||
has_text = False
|
||||
for frag in run:
|
||||
for k, v in frag.items():
|
||||
if k == "text":
|
||||
has_text = True
|
||||
if v:
|
||||
text_parts.append(v)
|
||||
elif v is not None:
|
||||
merged_entry[k] = v
|
||||
if has_text:
|
||||
merged_entry["text"] = "".join(text_parts)
|
||||
return merged_entry
|
||||
|
||||
|
||||
def _merge_reasoning_details(
|
||||
details: list[dict[str, Any]],
|
||||
) -> list[dict[str, Any]]:
|
||||
"""Merge fragmented `reasoning_details` from streaming chunk concatenation.
|
||||
|
||||
During streaming, `AIMessageChunk.__add__` list-concatenates
|
||||
`reasoning_details` in `additional_kwargs`, fragmenting a single entry
|
||||
into many. When serialized back to the API via
|
||||
`_convert_message_to_dict`, these fragments cause
|
||||
`BadRequestResponseError` on multi-turn conversations (the provider
|
||||
rejects the malformed thinking block with `Invalid signature`).
|
||||
|
||||
Streaming deltas tag each fragment with the `index` of the entry it
|
||||
belongs to in the original (non-streamed) array, so this function groups
|
||||
consecutive entries by `(type, index)` and merges each group into one.
|
||||
Entries without an `index` are preserved as-is, since non-streaming
|
||||
responses can legitimately contain multiple entries.
|
||||
|
||||
Within a merged group, `text` values are concatenated in order. Other
|
||||
metadata fields (e.g. `format`, `signature`) use last-non-`None`-wins
|
||||
semantics, which preserves stable provider metadata without concatenating
|
||||
repeated strings — Anthropic-style reasoning streams emit a single
|
||||
signature-bearing fragment at the end of the block.
|
||||
|
||||
A list with zero or one items passes through unchanged.
|
||||
"""
|
||||
if not isinstance(details, list) or len(details) <= 1:
|
||||
return details
|
||||
|
||||
merged: list[dict[str, Any]] = []
|
||||
i = 0
|
||||
while i < len(details):
|
||||
entry = details[i]
|
||||
# Without an index we cannot distinguish streaming fragments from
|
||||
# distinct non-streaming entries, so leave them alone. Same for any
|
||||
# non-dict items that may have slipped in upstream.
|
||||
if not isinstance(entry, dict) or entry.get("index") is None:
|
||||
merged.append(entry)
|
||||
i += 1
|
||||
continue
|
||||
|
||||
entry_type = entry.get("type", "")
|
||||
entry_index = entry["index"]
|
||||
run = [entry]
|
||||
i += 1
|
||||
while i < len(details):
|
||||
nxt = details[i]
|
||||
if (
|
||||
isinstance(nxt, dict)
|
||||
and nxt.get("type", "") == entry_type
|
||||
and nxt.get("index") == entry_index
|
||||
):
|
||||
run.append(nxt)
|
||||
i += 1
|
||||
else:
|
||||
break
|
||||
|
||||
merged.append(entry if len(run) == 1 else _merge_reasoning_run(run))
|
||||
|
||||
return merged
|
||||
|
||||
|
||||
def _convert_message_to_dict(message: BaseMessage) -> dict[str, Any]: # noqa: C901, PLR0912
|
||||
"""Convert a LangChain message to an OpenRouter-compatible dict payload.
|
||||
|
||||
@@ -1217,14 +1297,15 @@ def _convert_message_to_dict(message: BaseMessage) -> dict[str, Any]: # noqa: C
|
||||
):
|
||||
message_dict["content"] = None
|
||||
# Preserve reasoning content for multi-turn conversations (e.g.
|
||||
# tool-calling loops). OpenRouter stores reasoning in "reasoning" and
|
||||
# optional structured details in "reasoning_details".
|
||||
# tool-calling loops). OpenRouter stores reasoning text in `reasoning`
|
||||
# and structured fragment details in `reasoning_details`; the latter
|
||||
# is merged before serialization to undo streaming fragmentation.
|
||||
if "reasoning_content" in message.additional_kwargs:
|
||||
message_dict["reasoning"] = message.additional_kwargs["reasoning_content"]
|
||||
if "reasoning_details" in message.additional_kwargs:
|
||||
message_dict["reasoning_details"] = message.additional_kwargs[
|
||||
"reasoning_details"
|
||||
]
|
||||
message_dict["reasoning_details"] = _merge_reasoning_details(
|
||||
message.additional_kwargs["reasoning_details"]
|
||||
)
|
||||
elif isinstance(message, SystemMessage):
|
||||
message_dict = {"role": "system", "content": message.content}
|
||||
elif isinstance(message, ToolMessage):
|
||||
|
||||
Reference in New Issue
Block a user