Skip to content

Commit

Permalink
core[patch]: Add check for bind tools in structured prompt (#5882)
Browse files Browse the repository at this point in the history
* core[patch]: Add check for bind tools in structured promot

* cr

* fix test
  • Loading branch information
bracesproul authored Jun 26, 2024
1 parent fa8edb4 commit b7e4264
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
18 changes: 16 additions & 2 deletions langchain-core/src/prompts/structured.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { BaseLanguageModel } from "../language_models/base.js";
import { BaseChatModel } from "../language_models/chat_models.js";
import { ChatPromptValueInterface } from "../prompt_values.js";
import {
RunnableLike,
Expand All @@ -22,7 +24,18 @@ function isWithStructuredOutput(
typeof x === "object" &&
x != null &&
"withStructuredOutput" in x &&
typeof x.withStructuredOutput === "function"
x.withStructuredOutput !== BaseLanguageModel.prototype.withStructuredOutput
);
}

function isBindTools(x: unknown): x is {
bindTools: (...arg: unknown[]) => Runnable;
} {
return (
typeof x === "object" &&
x != null &&
"bindTools" in x &&
x.bindTools !== BaseChatModel.prototype.bindTools
);
}

Expand Down Expand Up @@ -84,7 +97,8 @@ export class StructuredPrompt<

if (
isRunnableBinding(coerceable) &&
isWithStructuredOutput(coerceable.bound)
isWithStructuredOutput(coerceable.bound) &&
isBindTools(coerceable.bound)
) {
return super.pipe(
coerceable.bound
Expand Down
8 changes: 8 additions & 0 deletions langchain-core/src/prompts/tests/structured.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,23 @@ import {
StructuredOutputMethodParams,
StructuredOutputMethodOptions,
BaseLanguageModelInput,
ToolDefinition,
} from "../../language_models/base.js";
import { BaseMessage } from "../../messages/index.js";
import { Runnable, RunnableLambda } from "../../runnables/base.js";
import { RunnableConfig } from "../../runnables/config.js";
import { FakeListChatModel } from "../../utils/testing/index.js";
import { StructuredPrompt } from "../structured.js";
import { load } from "../../load/index.js";
import { StructuredToolInterface } from "../../tools.js";

class FakeStructuredChatModel extends FakeListChatModel {
override bindTools(
_tools: (StructuredToolInterface | ToolDefinition | Record<string, any>)[]
): Runnable {
return this.bind({});
}

withStructuredOutput<
RunOutput extends Record<string, any> = Record<string, any>
>(
Expand Down

0 comments on commit b7e4264

Please sign in to comment.