langchain-ai/langchain · error · TypeError

unsupported operand type(s) for +: '{type(self)}' and '{type

Error message

unsupported operand type(s) for +: '{type(self)}' and '{type(other)}'

What it means

TypeError from ChatGenerationChunk.__add__: the + operator only accepts another ChatGenerationChunk or a list of ChatGenerationChunk. Anything else — a plain ChatGeneration, an AIMessageChunk, a str, a list mixing types — hits the final branch and raises, mirroring Python's native unsupported-operand error.

Source

Thrown at libs/core/langchain_core/outputs/chat_generation.py:137

                other.generation_info or {},
            )
            return ChatGenerationChunk(
                message=self.message + other.message,
                generation_info=generation_info or None,
            )
        if isinstance(other, list) and all(
            isinstance(x, ChatGenerationChunk) for x in other
        ):
            generation_info = merge_dicts(
                self.generation_info or {},
                *[chunk.generation_info for chunk in other if chunk.generation_info],
            )
            return ChatGenerationChunk(
                message=self.message + [chunk.message for chunk in other],
                generation_info=generation_info or None,
            )
        msg = f"unsupported operand type(s) for +: '{type(self)}' and '{type(other)}'"
        raise TypeError(msg)


def merge_chat_generation_chunks(
    chunks: list[ChatGenerationChunk],
) -> ChatGenerationChunk | None:
    """Merge a list of `ChatGenerationChunk`s into a single `ChatGenerationChunk`.

    Args:
        chunks: A list of `ChatGenerationChunk` to merge.

    Returns:
        A merged `ChatGenerationChunk`, or `None` if the input list is empty.
    """
    if not chunks:
        return None

    if len(chunks) == 1:
        return chunks[0]

View on GitHub (pinned to e32fa9a52e)

Solutions

  1. Add ChatGenerationChunk to ChatGenerationChunk — keep the wrapper, not the inner message
  2. In custom aggregation, use merge_chat_generation_chunks(chunks) or reduce with functools.reduce(operator.add) over chunks only
  3. Ensure list operands contain only ChatGenerationChunk instances (convert ChatGeneration first or skip non-chunks)

Example fix

# before
merged = chunk + chunk.message  # TypeError: AIMessageChunk not supported
merged = chunk + other_generation  # TypeError if other_generation is ChatGeneration

# after
from langchain_core.outputs import ChatGenerationChunk, merge_chat_generation_chunks
merged = chunk + ChatGenerationChunk(message=other_generation.message)
# or simply:
merged = merge_chat_generation_chunks([chunk1, chunk2, chunk3])
Defensive patterns

Strategy: type-guard

Validate before calling

from langchain_core.outputs import ChatGenerationChunk
if not isinstance(other, ChatGenerationChunk):
    other = ChatGenerationChunk(message=other.message) if hasattr(other, "message") else None
assert other is not None

Type guard

from langchain_core.outputs import ChatGenerationChunk

def is_chunk_list(xs: object) -> bool:
    return isinstance(xs, list) and all(isinstance(x, ChatGenerationChunk) for x in xs)

Try / catch

try:
    merged = chunk + other
except TypeError:
    merged = merge_chat_generation_chunks([chunk, other])  # normalize then merge

Prevention

When it happens

Trigger: chunk + chunk.message (adding the AIMessageChunk instead of the ChatGenerationChunk); summing a ChatGeneration with ChatGenerationChunks in custom streaming aggregation code; chunk + generation where generation is a base ChatGeneration from a non-streaming call.

Common situations: Custom streaming reducers/merge helpers that unpack .message too early; mixing results of stream() and invoke(); version upgrades where internal aggregation APIs changed and old workarounds now add mismatched types.

Related errors


AI-assisted analysis of langchain-ai/langchain@e32fa9a52e (2026-08-14). Data as JSON: /api/errors/9e9541d2717f484a. Report an issue: GitHub.