Checked other resources
Package (Required)
Related Issues / PRs
No response
Reproduction Steps / Example Code (Python)
from langchain_openrouter import ChatOpenRouter
from langchain_core.messages import AIMessage, HumanMessage
model = ChatOpenRouter(
model="anthropic/claude-sonnet-4-6",
api_key="your-key-here",
reasoning={"effort": "high", "summary": "auto"},
)
# Turn 1 — streaming
msgs = [HumanMessage(content="What is 2+2? Think step by step.")]
chunks = list(model.stream(msgs))
merged = chunks[0]
for c in chunks[1:]:
merged = merged + c
# Inspect: reasoning_details is fragmented (e.g. 6 entries instead of 1)
details = merged.additional_kwargs.get("reasoning_details", [])
print(f"reasoning_details count: {len(details)}") # Expected: 1, Actual: 6
# Turn 2 — fails
ai_msg = AIMessage(
content=merged.content,
additional_kwargs=merged.additional_kwargs,
response_metadata=merged.response_metadata,
)
msgs.append(ai_msg)
msgs.append(HumanMessage(content="Now what is 3+3?"))
# This raises BadRequestResponseError
chunks2 = list(model.stream(msgs))
Error Message and Stack Trace (if applicable)
BadRequestResponseError: Provider returned error
Description
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 are passed through as-is. The API rejects the malformed payload with BadRequestResponseError.
Non-streaming (invoke()) is unaffected — it returns complete entries in a single response.
The fix should merge fragmented reasoning_details entries (same type + same index) back into single entries before serialization. A PR with the fix and tests is ready.
System Info
System Information
OS: Darwin
OS Version: Darwin Kernel Version 25.3.0
Python Version: 3.11.14
Package Information
langchain_core: 1.2.23
langchain: 1.2.13
langchain_openrouter: 0.2.1
langchain_openai: 1.1.11
Other Dependencies
openrouter: 0.8.0
pydantic: 2.12.5
Checked other resources
Package (Required)
Related Issues / PRs
No response
Reproduction Steps / Example Code (Python)
Error Message and Stack Trace (if applicable)
Description
During streaming,
AIMessageChunk.__add__list-concatenatesreasoning_detailsinadditional_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 are passed through as-is. The API rejects the malformed payload withBadRequestResponseError.Non-streaming (
invoke()) is unaffected — it returns complete entries in a single response.The fix should merge fragmented
reasoning_detailsentries (sametype+ sameindex) back into single entries before serialization. A PR with the fix and tests is ready.System Info
System Information
Package Information
Other Dependencies