Home / Class/ ChatGeneration Class — langchain Architecture

ChatGeneration Class — langchain Architecture

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

Entity Profile

Dependency Diagram

graph TD
  fb3554e0_291b_93d2_d325_51461432ed8a["ChatGeneration"]
  e24b5be2_a888_4700_a118_b05d34a03395["Generation"]
  fb3554e0_291b_93d2_d325_51461432ed8a -->|extends| e24b5be2_a888_4700_a118_b05d34a03395
  0b5a237e_9feb_c3f3_db00_596824d6e26d["chat_generation.py"]
  fb3554e0_291b_93d2_d325_51461432ed8a -->|defined in| 0b5a237e_9feb_c3f3_db00_596824d6e26d
  7db01af2_978b_097a_83eb_b1e74fa919af["set_text()"]
  fb3554e0_291b_93d2_d325_51461432ed8a -->|method| 7db01af2_978b_097a_83eb_b1e74fa919af

Relationship Graph

Source Code

libs/core/langchain_core/outputs/chat_generation.py lines 17–73

class ChatGeneration(Generation):
    """A single chat generation output.

    A subclass of `Generation` that represents the response from a chat model that
    generates chat messages.

    The `message` attribute is a structured representation of the chat message. Most of
    the time, the message will be of type `AIMessage`.

    Users working with chat models will usually access information via either
    `AIMessage` (returned from runnable interfaces) or `LLMResult` (available via
    callbacks).
    """

    text: str = ""
    """The text contents of the output message.

    !!! warning "SHOULD NOT BE SET DIRECTLY!"

    """
    message: BaseMessage
    """The message output by the chat model."""

    # Override type to be ChatGeneration, ignore mypy error as this is intentional
    type: Literal["ChatGeneration"] = "ChatGeneration"  # type: ignore[assignment]
    """Type is used exclusively for serialization purposes."""

    @model_validator(mode="after")
    def set_text(self) -> Self:
        """Set the text attribute to be the contents of the message.

        Args:
            values: The values of the object.

        Returns:
            The values of the object with the text attribute set.

        Raises:
            ValueError: If the message is not a string or a list.
        """
        text = ""
        if isinstance(self.message.content, str):
            text = self.message.content
        # Extracts first text block from content blocks.
        # Skips blocks with explicit non-text type (e.g., thinking, reasoning).
        elif isinstance(self.message.content, list):
            for block in self.message.content:
                if isinstance(block, str):
                    text = block
                    break
                if isinstance(block, dict) and "text" in block:
                    block_type = block.get("type")
                    if block_type is None or block_type == "text":
                        text = block["text"]
                        break
        self.text = text
        return self

Extends

Frequently Asked Questions

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

Analyze Your Own Codebase

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

Try Supermodel Free