Skip to content

Commit 3c94076

Browse files
authored
fix(langchain): Bind schemas for other types of pulled hub prompts (#9057)
1 parent 027390f commit 3c94076

File tree

3 files changed

+39
-10
lines changed

3 files changed

+39
-10
lines changed

.changeset/silver-dingos-wonder.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"langchain": patch
3+
---
4+
5+
fix(langchain): Bind schemas for other types of pulled hub prompts

langchain/src/hub/base.ts

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -163,17 +163,29 @@ export function bindOutputSchema<T extends Runnable>(loadedSequence: T) {
163163
"schema" in loadedSequence.first &&
164164
"last" in loadedSequence &&
165165
loadedSequence.last !== null &&
166-
typeof loadedSequence.last === "object" &&
167-
"bound" in loadedSequence.last &&
168-
loadedSequence.last.bound !== null &&
169-
typeof loadedSequence.last.bound === "object" &&
170-
"withStructuredOutput" in loadedSequence.last.bound &&
171-
typeof loadedSequence.last.bound.withStructuredOutput === "function"
166+
typeof loadedSequence.last === "object"
172167
) {
173-
// eslint-disable-next-line no-param-reassign
174-
loadedSequence.last.bound = loadedSequence.last.bound.withStructuredOutput(
175-
loadedSequence.first.schema
176-
);
168+
if (
169+
"bound" in loadedSequence.last &&
170+
loadedSequence.last.bound !== null &&
171+
typeof loadedSequence.last.bound === "object" &&
172+
"withStructuredOutput" in loadedSequence.last.bound &&
173+
typeof loadedSequence.last.bound.withStructuredOutput === "function"
174+
) {
175+
// eslint-disable-next-line no-param-reassign
176+
loadedSequence.last.bound =
177+
loadedSequence.last.bound.withStructuredOutput(
178+
loadedSequence.first.schema
179+
);
180+
} else if (
181+
"withStructuredOutput" in loadedSequence.last &&
182+
typeof loadedSequence.last.withStructuredOutput === "function"
183+
) {
184+
// eslint-disable-next-line no-param-reassign
185+
loadedSequence.last = loadedSequence.last.withStructuredOutput(
186+
loadedSequence.first.schema
187+
);
188+
}
177189
}
178190
return loadedSequence;
179191
}

langchain/src/hub/tests/hub.int.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,18 @@ test("Test LangChain Hub while loading model with dynamic imports and structured
113113
expect(typeof res.correctness).toBe("boolean");
114114
});
115115

116+
test("Test LangChain Hub while loading model with dynamic imports and structured output with no binding", async () => {
117+
const pulledPrompt = await nodePull("jacob/structured-output-2", {
118+
includeModel: true,
119+
});
120+
const res = await pulledPrompt.invoke({
121+
input:
122+
"Who is the current president of the USA as of today? You must use the provided tool for the latest info.",
123+
});
124+
expect(res).not.toBeInstanceOf(AIMessage);
125+
expect(typeof res.correctness).toBe("boolean");
126+
});
127+
116128
test("Test LangChain Hub while loading model not defined in a RunnableBinding", async () => {
117129
const promptA = await nodePull("hntrl/binding-manifest", {
118130
includeModel: true,

0 commit comments

Comments
 (0)