Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/chatty-spies-shine.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"langchain": patch
---

allow for model strings in summarization middleware
10 changes: 8 additions & 2 deletions libs/langchain/src/agents/middleware/summarization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { REMOVE_ALL_MESSAGES } from "@langchain/langgraph";
import { createMiddleware } from "../middleware.js";
import { countTokensApproximately } from "./utils.js";
import { hasToolCalls } from "../utils.js";
import { initChatModel } from "../../chat_models/universal.js";

const DEFAULT_SUMMARY_PROMPT = `<role>
Context Extraction Assistant
Expand Down Expand Up @@ -57,7 +58,7 @@ const SEARCH_RANGE_FOR_TOOL_PAIRS = 5;
type TokenCounter = (messages: BaseMessage[]) => number | Promise<number>;

const contextSchema = z.object({
model: z.custom<BaseLanguageModel>(),
model: z.custom<string | BaseLanguageModel>(),
maxTokensBeforeSummary: z.number().optional(),
messagesToKeep: z.number().default(DEFAULT_MESSAGES_TO_KEEP),
tokenCounter: z
Expand Down Expand Up @@ -148,6 +149,11 @@ export function summarizationMiddleware(
} as InferInteropZodOutput<typeof contextSchema>;
const { messages } = state;

const model =
typeof config.model === "string"
? await initChatModel(config.model)
: config.model;

// Ensure all messages have IDs
ensureMessageIds(messages);

Expand Down Expand Up @@ -180,7 +186,7 @@ export function summarizationMiddleware(

const summary = await createSummary(
messagesToSummarize,
config.model,
model,
config.summaryPrompt,
tokenCounter
);
Expand Down
48 changes: 48 additions & 0 deletions libs/langchain/src/agents/middleware/tests/summarization.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,42 @@ import {
SystemMessage,
ToolMessage,
} from "@langchain/core/messages";

import { summarizationMiddleware } from "../summarization.js";
import { countTokensApproximately } from "../utils.js";
import { createAgent } from "../../index.js";
import { FakeToolCallingChatModel } from "../../tests/utils.js";

// Mock @langchain/anthropic to test model string usage without requiring the built package
vi.mock("@langchain/anthropic", async () => {
const { AIMessage } = await import("@langchain/core/messages");
return {
ChatAnthropic: class MockChatAnthropic {
lc_kwargs: Record<string, any>;

constructor(params?: any) {
this.lc_kwargs = params || {};
}

async invoke() {
return new AIMessage({ content: "Mocked response" });
}

getName() {
return "ChatAnthropic";
}

get _modelType() {
return "chat-anthropic";
}

get lc_runnable() {
return true;
}
},
};
});

describe("summarizationMiddleware", () => {
// Mock summarization model
function createMockSummarizationModel() {
Expand Down Expand Up @@ -335,4 +366,21 @@ describe("summarizationMiddleware", () => {
expect(nonSystemMessages.length).toBeGreaterThanOrEqual(messagesToKeep);
expect(nonSystemMessages.length).toBeLessThanOrEqual(messagesToKeep + 3); // Some buffer for safety
});

it("can be created using a model string", async () => {
const model = "anthropic:claude-sonnet-4-20250514";
const middleware = summarizationMiddleware({
model,
maxTokensBeforeSummary: 100,
messagesToKeep: 2,
});

const agent = createAgent({
model,
middleware: [middleware],
});

const result = await agent.invoke({ messages: [] });
expect(result.messages.at(-1)?.content).toBe("Mocked response");
});
});
Loading