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

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.

Source

Thrown at libs/core/langchain_core/outputs/generation.py:80

            other: Another `GenerationChunk` to concatenate with.

        Raises:
            TypeError: If other is not a `GenerationChunk`.

        Returns:
            A new `GenerationChunk` concatenated from self and other.
        """
        if isinstance(other, GenerationChunk):
            generation_info = merge_dicts(
                self.generation_info or {},
                other.generation_info or {},
            )
            return GenerationChunk(
                text=self.text + other.text,
                generation_info=generation_info or None,
            )
        msg = f"unsupported operand type(s) for +: '{type(self)}' and '{type(other)}'"  # type: ignore[unreachable]
        raise TypeError(msg)

View on GitHub (pinned to e32fa9a52e)

Solutions

  1. Ensure both operands are GenerationChunk: wrap plain text with GenerationChunk(text=...) or build chunks via the model's streaming API before adding
  2. If aggregating manually, accumulate the .text attribute in a string (full_text += chunk.text) instead of adding chunks
  3. Check types before addition: if not isinstance(other, GenerationChunk): coerce or raise your own descriptive error

Example fix

# before
chunk = next(model.stream("hi"))
merged = chunk + " world"  # TypeError

# after
from langchain_core.outputs import GenerationChunk
merged = chunk + GenerationChunk(text=" world")
Defensive patterns

Strategy: type-guard

Validate before calling

from langchain_core.outputs import GenerationChunk

def safe_add(a, b):
    if not isinstance(b, GenerationChunk):
        b = GenerationChunk(text=str(getattr(b, "text", b)))
    return a + b

Type guard

from langchain_core.outputs import GenerationChunk

def is_generation_chunk(x) -> bool:
    return isinstance(x, GenerationChunk)

Try / catch

try:
    merged = chunk + other
except TypeError:
    # fall back to text-level accumulation
    merged = GenerationChunk(text=chunk.text + str(getattr(other, "text", other)))

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


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