-
Notifications
You must be signed in to change notification settings - Fork 2.9k
feat(langchain/createAgent): Add support for modelSettings to middleware
#9110
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
hntrl
merged 7 commits into
langchain-ai:v1
from
pokey:pokey/featlangchaincreateagent-add-support-for-calloptions-to-middleware
Oct 15, 2025
Merged
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
00d268f
feat(langchain/createAgent): Add support for callOptions to middleware
pokey b6feeaf
Merge branch 'v1' into pokey/featlangchaincreateagent-add-support-for…
pokey 73e737f
Merge branch 'v1' into pokey/featlangchaincreateagent-add-support-for…
pokey 4ed8742
Merge branch 'v1' into HEAD
pokey f53edc2
`modelSettings` instead of `callOptions`
pokey 041bf6a
Add more realistic test
pokey 705f4a1
Remove unnecessary tests
pokey File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
284 changes: 284 additions & 0 deletions
284
libs/langchain/src/agents/middlewareAgent/tests/callOptions.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,284 @@ | ||
| import { expect, describe, it, vi, type MockInstance } from "vitest"; | ||
| import { HumanMessage, AIMessage } from "@langchain/core/messages"; | ||
| import type { LanguageModelLike } from "@langchain/core/language_models/base"; | ||
|
|
||
| import { createAgent, createMiddleware } from "../index.js"; | ||
|
|
||
| function createMockModel(name = "ChatAnthropic", model = "anthropic") { | ||
| // Mock Anthropic model | ||
| const invokeCallback = vi | ||
| .fn() | ||
| .mockResolvedValue(new AIMessage("Response from model")); | ||
| return { | ||
| getName: () => name, | ||
| bindTools: vi.fn().mockReturnThis(), | ||
| _streamResponseChunks: vi.fn().mockReturnThis(), | ||
| bind: vi.fn().mockReturnThis(), | ||
| invoke: invokeCallback, | ||
| lc_runnable: true, | ||
| _modelType: model, | ||
| _generate: vi.fn(), | ||
| _llmType: () => model, | ||
| } as unknown as LanguageModelLike; | ||
| } | ||
|
|
||
| describe("callOptions middleware support", () => { | ||
| it("should pass callOptions from middleware to model.invoke", async () => { | ||
| const model = createMockModel(); | ||
| const middleware = createMiddleware({ | ||
| name: "testMiddleware", | ||
| modifyModelRequest: async (request) => { | ||
| return { | ||
| ...request, | ||
| callOptions: { | ||
| temperature: 0.5, | ||
| max_tokens: 100, | ||
| }, | ||
| }; | ||
| }, | ||
| }); | ||
|
|
||
| const agent = createAgent({ | ||
| model, | ||
| tools: [], | ||
| middleware: [middleware] as const, | ||
| }); | ||
|
|
||
| await agent.invoke({ | ||
| messages: [new HumanMessage("Hello, world!")], | ||
| }); | ||
|
|
||
| expect(model.invoke).toHaveBeenCalled(); | ||
| const callArgs = (model.invoke as unknown as MockInstance).mock.calls[0]; | ||
| const config = callArgs[1]; | ||
| expect(config).toHaveProperty("temperature", 0.5); | ||
| expect(config).toHaveProperty("max_tokens", 100); | ||
| }); | ||
|
|
||
| it("should pass headers from middleware callOptions to model.invoke", async () => { | ||
| const model = createMockModel(); | ||
| const middleware = createMiddleware({ | ||
| name: "testMiddleware", | ||
| modifyModelRequest: async (request) => { | ||
| return { | ||
| ...request, | ||
| callOptions: { | ||
| headers: { | ||
| "X-Custom-Header": "middleware-value", | ||
| "X-Middleware-Only": "middleware-only", | ||
| }, | ||
| }, | ||
| }; | ||
| }, | ||
| }); | ||
|
|
||
| const agent = createAgent({ | ||
| model, | ||
| tools: [], | ||
| middleware: [middleware] as const, | ||
| }); | ||
|
|
||
| await agent.invoke({ | ||
| messages: [new HumanMessage("Hello, world!")], | ||
| }); | ||
|
|
||
| expect(model.invoke).toHaveBeenCalled(); | ||
| const callArgs = (model.invoke as unknown as MockInstance).mock.calls[0]; | ||
| const config = callArgs[1]; | ||
| expect(config.headers).toEqual({ | ||
| "X-Custom-Header": "middleware-value", | ||
| "X-Middleware-Only": "middleware-only", | ||
| }); | ||
| }); | ||
|
|
||
| it("should pass headers from middleware callOptions to model.invoke config", async () => { | ||
| // eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
| const model = createMockModel() as any; | ||
|
|
||
| // Middleware that adds headers via callOptions | ||
| const middleware = createMiddleware({ | ||
| name: "testMiddleware", | ||
| modifyModelRequest: async (request) => { | ||
| return { | ||
| ...request, | ||
| callOptions: { | ||
| headers: { | ||
| "X-Middleware-Header": "from-middleware", | ||
| "X-Custom-Header": "custom-value", | ||
| }, | ||
| }, | ||
| }; | ||
| }, | ||
| }); | ||
|
|
||
| const agent = createAgent({ | ||
| model, | ||
| tools: [], | ||
| middleware: [middleware] as const, | ||
| }); | ||
|
|
||
| await agent.invoke({ | ||
| messages: [new HumanMessage("Hello, world!")], | ||
| }); | ||
|
|
||
| // Verify model.invoke was called with headers in the config | ||
| expect(model.invoke).toHaveBeenCalled(); | ||
| const callArgs = (model.invoke as unknown as MockInstance).mock.calls[0]; | ||
| const config = callArgs[1]; | ||
|
|
||
| expect(config.headers).toEqual({ | ||
| "X-Middleware-Header": "from-middleware", | ||
| "X-Custom-Header": "custom-value", | ||
| }); | ||
| }); | ||
|
|
||
| it("should handle callOptions without headers", async () => { | ||
| const model = createMockModel(); | ||
| const middleware = createMiddleware({ | ||
| name: "testMiddleware", | ||
| modifyModelRequest: async (request) => { | ||
| return { | ||
| ...request, | ||
| callOptions: { | ||
| temperature: 0.7, | ||
| }, | ||
| }; | ||
| }, | ||
| }); | ||
|
|
||
| const agent = createAgent({ | ||
| model, | ||
| tools: [], | ||
| middleware: [middleware] as const, | ||
| }); | ||
|
|
||
| await agent.invoke({ | ||
| messages: [new HumanMessage("Hello, world!")], | ||
| }); | ||
|
|
||
| expect(model.invoke).toHaveBeenCalled(); | ||
| const callArgs = (model.invoke as unknown as MockInstance).mock.calls[0]; | ||
| const config = callArgs[1]; | ||
| expect(config).toHaveProperty("temperature", 0.7); | ||
| expect(config.headers).toBeUndefined(); | ||
| }); | ||
|
|
||
| it("should only add headers when either config or callOptions has headers", async () => { | ||
| const model = createMockModel(); | ||
| const middleware = createMiddleware({ | ||
| name: "testMiddleware", | ||
| modifyModelRequest: async (request) => { | ||
| return { | ||
| ...request, | ||
| callOptions: { | ||
| temperature: 0.8, | ||
| }, | ||
| }; | ||
| }, | ||
| }); | ||
|
|
||
| const agent = createAgent({ | ||
| model, | ||
| tools: [], | ||
| middleware: [middleware] as const, | ||
| }); | ||
|
|
||
| await agent.invoke({ | ||
| messages: [new HumanMessage("Hello, world!")], | ||
| }); | ||
|
|
||
| expect(model.invoke).toHaveBeenCalled(); | ||
| const callArgs = (model.invoke as unknown as MockInstance).mock.calls[0]; | ||
| const config = callArgs[1]; | ||
| expect(config).not.toHaveProperty("headers"); | ||
| }); | ||
|
|
||
| it("should support multiple middleware with callOptions", async () => { | ||
| const model = createMockModel(); | ||
| const middleware1 = createMiddleware({ | ||
| name: "middleware1", | ||
| modifyModelRequest: async (request) => { | ||
| return { | ||
| ...request, | ||
| callOptions: { | ||
| temperature: 0.5, | ||
| headers: { | ||
| "X-Middleware-1": "value1", | ||
| }, | ||
| }, | ||
| }; | ||
| }, | ||
| }); | ||
|
|
||
| const middleware2 = createMiddleware({ | ||
| name: "middleware2", | ||
| modifyModelRequest: async (request) => { | ||
| return { | ||
| ...request, | ||
| callOptions: { | ||
| max_tokens: 200, | ||
| headers: { | ||
| "X-Middleware-2": "value2", | ||
| }, | ||
| }, | ||
| }; | ||
| }, | ||
| }); | ||
|
|
||
| const agent = createAgent({ | ||
| model, | ||
| tools: [], | ||
| middleware: [middleware1, middleware2] as const, | ||
| }); | ||
|
|
||
| await agent.invoke({ | ||
| messages: [new HumanMessage("Hello, world!")], | ||
| }); | ||
|
|
||
| expect(model.invoke).toHaveBeenCalled(); | ||
| const callArgs = (model.invoke as unknown as MockInstance).mock.calls[0]; | ||
| const config = callArgs[1]; | ||
| // Last middleware wins for overlapping properties | ||
| expect(config).toHaveProperty("max_tokens", 200); | ||
| expect(config.headers).toEqual({ | ||
| "X-Middleware-2": "value2", | ||
| }); | ||
| }); | ||
|
|
||
| it("should preserve signal from config when merging callOptions", async () => { | ||
| const model = createMockModel(); | ||
| const abortController = new AbortController(); | ||
| const middleware = createMiddleware({ | ||
| name: "testMiddleware", | ||
| modifyModelRequest: async (request) => { | ||
| return { | ||
| ...request, | ||
| callOptions: { | ||
| temperature: 0.5, | ||
| }, | ||
| }; | ||
| }, | ||
| }); | ||
|
|
||
| const agent = createAgent({ | ||
| model, | ||
| tools: [], | ||
| middleware: [middleware] as const, | ||
| }); | ||
|
|
||
| await agent.invoke( | ||
| { | ||
| messages: [new HumanMessage("Hello, world!")], | ||
| }, | ||
| { | ||
| signal: abortController.signal, | ||
| } | ||
| ); | ||
|
|
||
| expect(model.invoke).toHaveBeenCalled(); | ||
| const callArgs = (model.invoke as unknown as MockInstance).mock.calls[0]; | ||
| const config = callArgs[1]; | ||
| expect(config).toHaveProperty("signal"); | ||
| expect(config).toHaveProperty("temperature", 0.5); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cc @christian-bromann / @pokey is this a standard name in js?
@sydney-runkle this is extra config that we'll be passing to chat model invoke. We can either use the same standard name if it makes sense or follow whatever is convention in each language.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
to be more specific here I would suggest
modelCallOptionsas an alternative.