Skip to content

Commit 3ada6ac

Browse files
authored
fix(openai): Fix ls structured output params (#8529)
1 parent ca5b9b1 commit 3ada6ac

File tree

2 files changed

+28
-28
lines changed

2 files changed

+28
-28
lines changed

libs/langchain-openai/src/chat_models.ts

Lines changed: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,7 @@ import {
7878
InteropZodType,
7979
isInteropZodSchema,
8080
} from "@langchain/core/utils/types";
81-
import {
82-
JsonSchema7Type,
83-
toJsonSchema,
84-
} from "@langchain/core/utils/json_schema";
81+
import { toJsonSchema } from "@langchain/core/utils/json_schema";
8582
import {
8683
type OpenAICallOptions,
8784
type OpenAIChatInput,
@@ -1160,12 +1157,6 @@ abstract class BaseChatOpenAI<CallOptions extends BaseChatOpenAICallOptions>
11601157
| Record<string, any>,
11611158
config?: StructuredOutputMethodOptions<boolean>
11621159
) {
1163-
// ):
1164-
// | Runnable<BaseLanguageModelInput, RunOutput>
1165-
// | Runnable<
1166-
// BaseLanguageModelInput,
1167-
// { raw: BaseMessage; parsed: RunOutput }
1168-
// > {
11691160
// eslint-disable-next-line @typescript-eslint/no-explicit-any
11701161
let schema: InteropZodType<RunOutput> | Record<string, any>;
11711162
let name;
@@ -1206,34 +1197,39 @@ abstract class BaseChatOpenAI<CallOptions extends BaseChatOpenAICallOptions>
12061197
}
12071198

12081199
if (method === "jsonMode") {
1209-
let outputFormatSchema: JsonSchema7Type | undefined;
12101200
if (isInteropZodSchema(schema)) {
12111201
outputParser = StructuredOutputParser.fromZodSchema(schema);
1212-
outputFormatSchema = toJsonSchema(schema);
12131202
} else {
12141203
outputParser = new JsonOutputParser<RunOutput>();
12151204
}
1205+
const asJsonSchema = toJsonSchema(schema);
12161206
llm = this.withConfig({
12171207
response_format: { type: "json_object" },
12181208
ls_structured_output_format: {
1219-
kwargs: { method: "jsonMode" },
1220-
schema: outputFormatSchema,
1209+
kwargs: { method: "json_mode" },
1210+
schema: { title: name ?? "extract", ...asJsonSchema },
12211211
},
12221212
} as Partial<CallOptions>);
12231213
} else if (method === "jsonSchema") {
1214+
const openaiJsonSchemaParams = {
1215+
name: name ?? "extract",
1216+
description: getSchemaDescription(schema),
1217+
schema,
1218+
strict: config?.strict,
1219+
};
1220+
const asJsonSchema = toJsonSchema(openaiJsonSchemaParams.schema);
12241221
llm = this.withConfig({
12251222
response_format: {
12261223
type: "json_schema",
1227-
json_schema: {
1228-
name: name ?? "extract",
1229-
description: getSchemaDescription(schema),
1230-
schema,
1231-
strict: config?.strict,
1232-
},
1224+
json_schema: openaiJsonSchemaParams,
12331225
},
12341226
ls_structured_output_format: {
1235-
kwargs: { method: "jsonSchema" },
1236-
schema: toJsonSchema(schema),
1227+
kwargs: { method: "json_schema" },
1228+
schema: {
1229+
title: openaiJsonSchemaParams.name,
1230+
description: openaiJsonSchemaParams.description,
1231+
...asJsonSchema,
1232+
},
12371233
},
12381234
} as Partial<CallOptions>);
12391235
if (isInteropZodSchema(schema)) {
@@ -1272,8 +1268,8 @@ abstract class BaseChatOpenAI<CallOptions extends BaseChatOpenAICallOptions>
12721268
},
12731269
},
12741270
ls_structured_output_format: {
1275-
kwargs: { method: "functionCalling" },
1276-
schema: asJsonSchema,
1271+
kwargs: { method: "function_calling" },
1272+
schema: { title: functionName, ...asJsonSchema },
12771273
},
12781274
// Do not pass `strict` argument to OpenAI if `config.strict` is undefined
12791275
...(config?.strict !== undefined ? { strict: config.strict } : {}),
@@ -1300,6 +1296,7 @@ abstract class BaseChatOpenAI<CallOptions extends BaseChatOpenAICallOptions>
13001296
parameters: schema,
13011297
};
13021298
}
1299+
const asJsonSchema = toJsonSchema(schema);
13031300
llm = this.withConfig({
13041301
tools: [
13051302
{
@@ -1314,8 +1311,8 @@ abstract class BaseChatOpenAI<CallOptions extends BaseChatOpenAICallOptions>
13141311
},
13151312
},
13161313
ls_structured_output_format: {
1317-
kwargs: { method: "functionCalling" },
1318-
schema: toJsonSchema(schema),
1314+
kwargs: { method: "function_calling" },
1315+
schema: { title: functionName, ...asJsonSchema },
13191316
},
13201317
// Do not pass `strict` argument to OpenAI if `config.strict` is undefined
13211318
...(config?.strict !== undefined ? { strict: config.strict } : {}),

libs/langchain-openai/src/tests/chat_models.test.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -323,8 +323,11 @@ test("OpenAI runs with structured output contain structured output options", asy
323323
expect(extra).toMatchObject({
324324
options: {
325325
ls_structured_output_format: {
326-
kwargs: { method: "jsonSchema" },
327-
schema: toJsonSchema(weatherSchema),
326+
kwargs: { method: "json_schema" },
327+
schema: {
328+
title: "get_current_weather",
329+
...toJsonSchema(weatherSchema),
330+
},
328331
},
329332
},
330333
});

0 commit comments

Comments
 (0)