{"record":{"id":"eaa14bbcc441372a","repo":"langchain-ai/langchain","slug":"unsupported-operand-type-s-for-type-self-eaa14b","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/generation.py","lineNumber":80,"sourceCode":"            other: Another `GenerationChunk` to concatenate with.\n\n        Raises:\n            TypeError: If other is not a `GenerationChunk`.\n\n        Returns:\n            A new `GenerationChunk` concatenated from self and other.\n        \"\"\"\n        if isinstance(other, GenerationChunk):\n            generation_info = merge_dicts(\n                self.generation_info or {},\n                other.generation_info or {},\n            )\n            return GenerationChunk(\n                text=self.text + other.text,\n                generation_info=generation_info or None,\n            )\n        msg = f\"unsupported operand type(s) for +: '{type(self)}' and '{type(other)}'\"  # type: ignore[unreachable]\n        raise TypeError(msg)\n","sourceCodeStart":62,"sourceCodeEnd":81,"githubUrl":"https://github.com/langchain-ai/langchain/blob/e32fa9a52eab3b61ad7a45399bfde59b3e580fc4/libs/core/langchain_core/outputs/generation.py#L62-L81","documentation":"Raised by GenerationChunk.__add__ when the right-hand operand of + is not a GenerationChunk. langchain-core implements additive merging only between two GenerationChunk objects (streaming tokens are concatenated and generation_info dicts merged). Adding a plain Generation, str, or any other type is unsupported and fails with this TypeError.","triggerScenarios":"Calling chunk + other where other is not a GenerationChunk: e.g. summing a stream with a str token, adding a base Generation, or mixing GenerationChunk with a custom subclass that skips the isinstance check path. Happens in custom streaming aggregation loops or when accumulating results from BaseChatModel._stream callbacks.","commonSituations":"Developers writing manual token-accumulation code around model.stream() often concatenate a raw string onto the chunk, or convert chunks to Generation (losing the subclass) and then try to add them back. Also appears when porting pre-0.1 streaming code that assumed __add__ accepted plain Generations.","solutions":["Ensure both operands are GenerationChunk: wrap plain text with GenerationChunk(text=...) or build chunks via the model's streaming API before adding","If aggregating manually, accumulate the .text attribute in a string (full_text += chunk.text) instead of adding chunks","Check types before addition: if not isinstance(other, GenerationChunk): coerce or raise your own descriptive error"],"exampleFix":"# before\nchunk = next(model.stream(\"hi\"))\nmerged = chunk + \" world\"  # TypeError\n\n# after\nfrom langchain_core.outputs import GenerationChunk\nmerged = chunk + GenerationChunk(text=\" world\")","handlingStrategy":"type-guard","validationCode":"from langchain_core.outputs import GenerationChunk\n\ndef safe_add(a, b):\n    if not isinstance(b, GenerationChunk):\n        b = GenerationChunk(text=str(getattr(b, \"text\", b)))\n    return a + b","typeGuard":"from langchain_core.outputs import GenerationChunk\n\ndef is_generation_chunk(x) -> bool:\n    return isinstance(x, GenerationChunk)","tryCatchPattern":"try:\n    merged = chunk + other\nexcept TypeError:\n    # fall back to text-level accumulation\n    merged = GenerationChunk(text=chunk.text + str(getattr(other, \"text\", other)))","preventionTips":["Type-annotate accumulators as GenerationChunk so mismatches surface in mypy","In streaming loops, add chunks only as delivered by the model's stream() API"],"tags":["streaming","type-error","outputs","core"],"backgroundTag":null,"analyzedSha":"e32fa9a52eab3b61ad7a45399bfde59b3e580fc4","analyzedAt":"2026-08-14T18:42:09.092Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}