Home / File/ summarization.py — langchain Source File

summarization.py — langchain Source File

Architecture documentation for summarization.py, a python file in the langchain codebase. 13 imports, 0 dependents.

File python LangChainCore Runnables 13 imports 1 functions 1 classes

Entity Profile

Dependency Diagram

graph LR
  7fb364fc_5799_42dd_fceb_bf60f55185e9["summarization.py"]
  02f66451_d2a9_e7c3_9765_c3a7594721ad["uuid"]
  7fb364fc_5799_42dd_fceb_bf60f55185e9 --> 02f66451_d2a9_e7c3_9765_c3a7594721ad
  f3365e3c_fb7a_bb9a_bc79_059b06cb7024["warnings"]
  7fb364fc_5799_42dd_fceb_bf60f55185e9 --> f3365e3c_fb7a_bb9a_bc79_059b06cb7024
  2bf6d401_816d_d011_3b05_a6114f55ff58["collections.abc"]
  7fb364fc_5799_42dd_fceb_bf60f55185e9 --> 2bf6d401_816d_d011_3b05_a6114f55ff58
  f46e1812_79f8_1bd0_7815_2ba4471dc470["functools"]
  7fb364fc_5799_42dd_fceb_bf60f55185e9 --> f46e1812_79f8_1bd0_7815_2ba4471dc470
  feec1ec4_6917_867b_d228_b134d0ff8099["typing"]
  7fb364fc_5799_42dd_fceb_bf60f55185e9 --> feec1ec4_6917_867b_d228_b134d0ff8099
  9444498b_8066_55c7_b3a2_1d90c4162a32["langchain_core.messages"]
  7fb364fc_5799_42dd_fceb_bf60f55185e9 --> 9444498b_8066_55c7_b3a2_1d90c4162a32
  441a16eb_9814_4c10_a09b_4a5a4bbbfeab["langchain_core.messages.human"]
  7fb364fc_5799_42dd_fceb_bf60f55185e9 --> 441a16eb_9814_4c10_a09b_4a5a4bbbfeab
  a188dc39_6da2_34b5_82d2_3c9b8af6f61c["langchain_core.messages.utils"]
  7fb364fc_5799_42dd_fceb_bf60f55185e9 --> a188dc39_6da2_34b5_82d2_3c9b8af6f61c
  a2ed5244_833f_36b3_ad87_753172a0ffd1["langgraph.graph.message"]
  7fb364fc_5799_42dd_fceb_bf60f55185e9 --> a2ed5244_833f_36b3_ad87_753172a0ffd1
  e07f6d54_afcc_052d_d33f_8ccdcc46f752["langgraph.runtime"]
  7fb364fc_5799_42dd_fceb_bf60f55185e9 --> e07f6d54_afcc_052d_d33f_8ccdcc46f752
  f85fae70_1011_eaec_151c_4083140ae9e5["typing_extensions"]
  7fb364fc_5799_42dd_fceb_bf60f55185e9 --> f85fae70_1011_eaec_151c_4083140ae9e5
  a681398d_ed44_c914_1a44_5d174223b069["langchain.agents.middleware.types"]
  7fb364fc_5799_42dd_fceb_bf60f55185e9 --> a681398d_ed44_c914_1a44_5d174223b069
  86772f84_002d_6acf_1cd7_c03ca452c4b1["langchain.chat_models"]
  7fb364fc_5799_42dd_fceb_bf60f55185e9 --> 86772f84_002d_6acf_1cd7_c03ca452c4b1
  style 7fb364fc_5799_42dd_fceb_bf60f55185e9 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

"""Summarization middleware."""

import uuid
import warnings
from collections.abc import Callable, Iterable, Mapping
from functools import partial
from typing import Any, Literal, cast

from langchain_core.messages import (
    AIMessage,
    AnyMessage,
    MessageLikeRepresentation,
    RemoveMessage,
    ToolMessage,
)
from langchain_core.messages.human import HumanMessage
from langchain_core.messages.utils import (
    count_tokens_approximately,
    get_buffer_string,
    trim_messages,
)
from langgraph.graph.message import (
    REMOVE_ALL_MESSAGES,
)
from langgraph.runtime import Runtime
from typing_extensions import override

from langchain.agents.middleware.types import AgentMiddleware, AgentState, ContextT, ResponseT
from langchain.chat_models import BaseChatModel, init_chat_model

TokenCounter = Callable[[Iterable[MessageLikeRepresentation]], int]

DEFAULT_SUMMARY_PROMPT = """<role>
Context Extraction Assistant
</role>

<primary_objective>
Your sole objective in this task is to extract the highest quality/most relevant context from the conversation history below.
</primary_objective>

<objective_information>
You're nearing the total number of input tokens you can accept, so you must extract the highest quality/most relevant pieces of information from your conversation history.
This context will then overwrite the conversation history presented below. Because of this, ensure the context you extract is only the most important information to continue working toward your overall goal.
</objective_information>

<instructions>
The conversation history below will be replaced with the context you extract in this step.
You want to ensure that you don't repeat any actions you've already completed, so the context you extract from the conversation history should be focused on the most important information to your overall goal.

You should structure your summary using the following sections. Each section acts as a checklist - you must populate it with relevant information or explicitly state "None" if there is nothing to report for that section:

## SESSION INTENT
What is the user's primary goal or request? What overall task are you trying to accomplish? This should be concise but complete enough to understand the purpose of the entire session.

## SUMMARY
Extract and record all of the most important context from the conversation history. Include important choices, conclusions, or strategies determined during this conversation. Include the reasoning behind key decisions. Document any rejected options and why they were not pursued.

## ARTIFACTS
What artifacts, files, or resources were created, modified, or accessed during this conversation? For file modifications, list specific file paths and briefly describe the changes made to each. This section prevents silent loss of artifact information.

// ... (599 more lines)

Domain

Subdomains

Dependencies

  • collections.abc
  • functools
  • langchain.agents.middleware.types
  • langchain.chat_models
  • langchain_core.messages
  • langchain_core.messages.human
  • langchain_core.messages.utils
  • langgraph.graph.message
  • langgraph.runtime
  • typing
  • typing_extensions
  • uuid
  • warnings

Frequently Asked Questions

What does summarization.py do?
summarization.py is a source file in the langchain codebase, written in python. It belongs to the LangChainCore domain, Runnables subdomain.
What functions are defined in summarization.py?
summarization.py defines 1 function(s): _get_approximate_token_counter.
What does summarization.py depend on?
summarization.py imports 13 module(s): collections.abc, functools, langchain.agents.middleware.types, langchain.chat_models, langchain_core.messages, langchain_core.messages.human, langchain_core.messages.utils, langgraph.graph.message, and 5 more.
Where is summarization.py in the architecture?
summarization.py is located at libs/langchain_v1/langchain/agents/middleware/summarization.py (domain: LangChainCore, subdomain: Runnables, directory: libs/langchain_v1/langchain/agents/middleware).

Analyze Your Own Codebase

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

Try Supermodel Free