Skip to content

Commit 854471d

Browse files
authored
fix(langchain): don't check hasSupportForJsonSchemaOutput if specified providerStrategy explictly (#9334)
1 parent e4a3b3b commit 854471d

File tree

2 files changed

+27
-11
lines changed

2 files changed

+27
-11
lines changed

libs/langchain/src/agents/nodes/AgentNode.ts

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ import {
3939
ProviderStrategy,
4040
transformResponseFormat,
4141
ToolStrategyError,
42-
hasSupportForJsonSchemaOutput,
4342
} from "../responses.js";
4443

4544
type ResponseHandlerResult<StructuredResponseFormat> =
@@ -763,15 +762,6 @@ export class AgentNode<
763762
* check if the user requests a native schema output
764763
*/
765764
if (structuredResponseFormat?.type === "native") {
766-
/**
767-
* if the model does not support JSON schema output, throw an error
768-
*/
769-
if (!hasSupportForJsonSchemaOutput(model)) {
770-
throw new Error(
771-
"Model does not support native structured output responses. Please use a model that supports native structured output responses or use a tool output."
772-
);
773-
}
774-
775765
const jsonSchemaParams = {
776766
name: structuredResponseFormat.strategy.schema?.name ?? "extract",
777767
description: getSchemaDescription(

libs/langchain/src/agents/tests/responses.test.ts

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { z } from "zod/v3";
44
import { ChatOpenAI } from "@langchain/openai";
55
import { ChatAnthropic } from "@langchain/anthropic";
66

7-
import { createAgent, toolStrategy } from "../index.js";
7+
import { createAgent, toolStrategy, providerStrategy } from "../index.js";
88
import { FakeToolCallingModel, FakeToolCallingChatModel } from "./utils.js";
99
import { hasSupportForJsonSchemaOutput } from "../responses.js";
1010

@@ -306,6 +306,32 @@ describe("structured output handling", () => {
306306
});
307307
});
308308
});
309+
describe("providerStrategy", () => {
310+
describe("use provider strategy directly", () => {
311+
it("should not throw error if use provider strategy directly", async () => {
312+
const model = new FakeToolCallingModel({
313+
toolCalls: [
314+
[{ name: "extract-16", args: { foo: "bar" }, id: "call_2" }],
315+
],
316+
});
317+
const agent = createAgent({
318+
model,
319+
tools: [],
320+
responseFormat: providerStrategy(
321+
z.object({
322+
foo: z.string(),
323+
})
324+
),
325+
});
326+
327+
await expect(
328+
agent.invoke({
329+
messages: [{ role: "user", content: "hi" }],
330+
})
331+
).resolves.not.toThrowError();
332+
});
333+
});
334+
});
309335
});
310336

311337
describe("hasSupportForJsonSchemaOutput", () => {

0 commit comments

Comments
 (0)