Home / Class/ ChatGenerationChunk Class — langchain Architecture

ChatGenerationChunk Class — langchain Architecture

Architecture documentation for the ChatGenerationChunk class in chat_generation.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  5c4a35cf_6380_360a_522b_de585729ab01["ChatGenerationChunk"]
  fb3554e0_291b_93d2_d325_51461432ed8a["ChatGeneration"]
  5c4a35cf_6380_360a_522b_de585729ab01 -->|extends| fb3554e0_291b_93d2_d325_51461432ed8a
  5c4a35cf_6380_360a_522b_de585729ab01["ChatGenerationChunk"]
  5c4a35cf_6380_360a_522b_de585729ab01 -->|extends| 5c4a35cf_6380_360a_522b_de585729ab01
  0b5a237e_9feb_c3f3_db00_596824d6e26d["chat_generation.py"]
  5c4a35cf_6380_360a_522b_de585729ab01 -->|defined in| 0b5a237e_9feb_c3f3_db00_596824d6e26d
  d5a48499_b516_71d9_984b_1f8e6cbc5b4a["__add__()"]
  5c4a35cf_6380_360a_522b_de585729ab01 -->|method| d5a48499_b516_71d9_984b_1f8e6cbc5b4a

Relationship Graph

Source Code

libs/core/langchain_core/outputs/chat_generation.py lines 76–126

class ChatGenerationChunk(ChatGeneration):
    """`ChatGeneration` chunk.

    `ChatGeneration` chunks can be concatenated with other `ChatGeneration` chunks.
    """

    message: BaseMessageChunk
    """The message chunk output by the chat model."""
    # Override type to be ChatGeneration, ignore mypy error as this is intentional

    type: Literal["ChatGenerationChunk"] = "ChatGenerationChunk"  # type: ignore[assignment]
    """Type is used exclusively for serialization purposes."""

    def __add__(
        self, other: ChatGenerationChunk | list[ChatGenerationChunk]
    ) -> ChatGenerationChunk:
        """Concatenate two `ChatGenerationChunk`s.

        Args:
            other: The other `ChatGenerationChunk` or list of `ChatGenerationChunk` to
                concatenate.

        Raises:
            TypeError: If other is not a `ChatGenerationChunk` or list of
                `ChatGenerationChunk`.

        Returns:
            A new `ChatGenerationChunk` concatenated from self and other.
        """
        if isinstance(other, ChatGenerationChunk):
            generation_info = merge_dicts(
                self.generation_info or {},
                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)

Frequently Asked Questions

What is the ChatGenerationChunk class?
ChatGenerationChunk is a class in the langchain codebase, defined in libs/core/langchain_core/outputs/chat_generation.py.
Where is ChatGenerationChunk defined?
ChatGenerationChunk is defined in libs/core/langchain_core/outputs/chat_generation.py at line 76.
What does ChatGenerationChunk extend?
ChatGenerationChunk extends ChatGeneration, ChatGenerationChunk.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free