Skip to content

Commit b7e4264

Browse files
authored
core[patch]: Add check for bind tools in structured prompt (#5882)
* core[patch]: Add check for bind tools in structured promot * cr * fix test
1 parent fa8edb4 commit b7e4264

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

langchain-core/src/prompts/structured.ts

+16-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { BaseLanguageModel } from "../language_models/base.js";
2+
import { BaseChatModel } from "../language_models/chat_models.js";
13
import { ChatPromptValueInterface } from "../prompt_values.js";
24
import {
35
RunnableLike,
@@ -22,7 +24,18 @@ function isWithStructuredOutput(
2224
typeof x === "object" &&
2325
x != null &&
2426
"withStructuredOutput" in x &&
25-
typeof x.withStructuredOutput === "function"
27+
x.withStructuredOutput !== BaseLanguageModel.prototype.withStructuredOutput
28+
);
29+
}
30+
31+
function isBindTools(x: unknown): x is {
32+
bindTools: (...arg: unknown[]) => Runnable;
33+
} {
34+
return (
35+
typeof x === "object" &&
36+
x != null &&
37+
"bindTools" in x &&
38+
x.bindTools !== BaseChatModel.prototype.bindTools
2639
);
2740
}
2841

@@ -84,7 +97,8 @@ export class StructuredPrompt<
8497

8598
if (
8699
isRunnableBinding(coerceable) &&
87-
isWithStructuredOutput(coerceable.bound)
100+
isWithStructuredOutput(coerceable.bound) &&
101+
isBindTools(coerceable.bound)
88102
) {
89103
return super.pipe(
90104
coerceable.bound

langchain-core/src/prompts/tests/structured.test.ts

+8
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,23 @@ import {
44
StructuredOutputMethodParams,
55
StructuredOutputMethodOptions,
66
BaseLanguageModelInput,
7+
ToolDefinition,
78
} from "../../language_models/base.js";
89
import { BaseMessage } from "../../messages/index.js";
910
import { Runnable, RunnableLambda } from "../../runnables/base.js";
1011
import { RunnableConfig } from "../../runnables/config.js";
1112
import { FakeListChatModel } from "../../utils/testing/index.js";
1213
import { StructuredPrompt } from "../structured.js";
1314
import { load } from "../../load/index.js";
15+
import { StructuredToolInterface } from "../../tools.js";
1416

1517
class FakeStructuredChatModel extends FakeListChatModel {
18+
override bindTools(
19+
_tools: (StructuredToolInterface | ToolDefinition | Record<string, any>)[]
20+
): Runnable {
21+
return this.bind({});
22+
}
23+
1624
withStructuredOutput<
1725
RunOutput extends Record<string, any> = Record<string, any>
1826
>(

0 commit comments

Comments
 (0)