{"record":{"id":"9e9541d2717f484a","repo":"langchain-ai/langchain","slug":"unsupported-operand-type-s-for-type-self","errorCode":null,"errorMessage":"unsupported operand type(s) for +: '{type(self)}' and '{type(other)}'","messagePattern":"unsupported operand type\\(s\\) for \\+: '(.+?)' and '(.+?)'","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"libs/core/langchain_core/outputs/chat_generation.py","lineNumber":137,"sourceCode":"                other.generation_info or {},\n            )\n            return ChatGenerationChunk(\n                message=self.message + other.message,\n                generation_info=generation_info or None,\n            )\n        if isinstance(other, list) and all(\n            isinstance(x, ChatGenerationChunk) for x in other\n        ):\n            generation_info = merge_dicts(\n                self.generation_info or {},\n                *[chunk.generation_info for chunk in other if chunk.generation_info],\n            )\n            return ChatGenerationChunk(\n                message=self.message + [chunk.message for chunk in other],\n                generation_info=generation_info or None,\n            )\n        msg = f\"unsupported operand type(s) for +: '{type(self)}' and '{type(other)}'\"\n        raise TypeError(msg)\n\n\ndef merge_chat_generation_chunks(\n    chunks: list[ChatGenerationChunk],\n) -> ChatGenerationChunk | None:\n    \"\"\"Merge a list of `ChatGenerationChunk`s into a single `ChatGenerationChunk`.\n\n    Args:\n        chunks: A list of `ChatGenerationChunk` to merge.\n\n    Returns:\n        A merged `ChatGenerationChunk`, or `None` if the input list is empty.\n    \"\"\"\n    if not chunks:\n        return None\n\n    if len(chunks) == 1:\n        return chunks[0]","sourceCodeStart":119,"sourceCodeEnd":155,"githubUrl":"https://github.com/langchain-ai/langchain/blob/e32fa9a52eab3b61ad7a45399bfde59b3e580fc4/libs/core/langchain_core/outputs/chat_generation.py#L119-L155","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Add ChatGenerationChunk to ChatGenerationChunk — keep the wrapper, not the inner message","In custom aggregation, use merge_chat_generation_chunks(chunks) or reduce with functools.reduce(operator.add) over chunks only","Ensure list operands contain only ChatGenerationChunk instances (convert ChatGeneration first or skip non-chunks)"],"exampleFix":"# before\nmerged = chunk + chunk.message  # TypeError: AIMessageChunk not supported\nmerged = chunk + other_generation  # TypeError if other_generation is ChatGeneration\n\n# after\nfrom langchain_core.outputs import ChatGenerationChunk, merge_chat_generation_chunks\nmerged = chunk + ChatGenerationChunk(message=other_generation.message)\n# or simply:\nmerged = merge_chat_generation_chunks([chunk1, chunk2, chunk3])","handlingStrategy":"type-guard","validationCode":"from langchain_core.outputs import ChatGenerationChunk\nif not isinstance(other, ChatGenerationChunk):\n    other = ChatGenerationChunk(message=other.message) if hasattr(other, \"message\") else None\nassert other is not None","typeGuard":"from langchain_core.outputs import ChatGenerationChunk\n\ndef is_chunk_list(xs: object) -> bool:\n    return isinstance(xs, list) and all(isinstance(x, ChatGenerationChunk) for x in xs)","tryCatchPattern":"try:\n    merged = chunk + other\nexcept TypeError:\n    merged = merge_chat_generation_chunks([chunk, other])  # normalize then merge","preventionTips":["Use merge_chat_generation_chunks for aggregation instead of hand-rolled +","Add ChatGenerationChunk (not AIMessageChunk) operands only"],"tags":["streaming","type-mismatch","chat","aggregation"],"backgroundTag":null,"analyzedSha":"e32fa9a52eab3b61ad7a45399bfde59b3e580fc4","analyzedAt":"2026-08-14T18:42:09.092Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}