@@ -78,10 +78,7 @@ import {
78
78
InteropZodType ,
79
79
isInteropZodSchema ,
80
80
} 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" ;
85
82
import {
86
83
type OpenAICallOptions ,
87
84
type OpenAIChatInput ,
@@ -1160,12 +1157,6 @@ abstract class BaseChatOpenAI<CallOptions extends BaseChatOpenAICallOptions>
1160
1157
| Record < string , any > ,
1161
1158
config ?: StructuredOutputMethodOptions < boolean >
1162
1159
) {
1163
- // ):
1164
- // | Runnable<BaseLanguageModelInput, RunOutput>
1165
- // | Runnable<
1166
- // BaseLanguageModelInput,
1167
- // { raw: BaseMessage; parsed: RunOutput }
1168
- // > {
1169
1160
// eslint-disable-next-line @typescript-eslint/no-explicit-any
1170
1161
let schema : InteropZodType < RunOutput > | Record < string , any > ;
1171
1162
let name ;
@@ -1206,34 +1197,39 @@ abstract class BaseChatOpenAI<CallOptions extends BaseChatOpenAICallOptions>
1206
1197
}
1207
1198
1208
1199
if ( method === "jsonMode" ) {
1209
- let outputFormatSchema : JsonSchema7Type | undefined ;
1210
1200
if ( isInteropZodSchema ( schema ) ) {
1211
1201
outputParser = StructuredOutputParser . fromZodSchema ( schema ) ;
1212
- outputFormatSchema = toJsonSchema ( schema ) ;
1213
1202
} else {
1214
1203
outputParser = new JsonOutputParser < RunOutput > ( ) ;
1215
1204
}
1205
+ const asJsonSchema = toJsonSchema ( schema ) ;
1216
1206
llm = this . withConfig ( {
1217
1207
response_format : { type : "json_object" } ,
1218
1208
ls_structured_output_format : {
1219
- kwargs : { method : "jsonMode " } ,
1220
- schema : outputFormatSchema ,
1209
+ kwargs : { method : "json_mode " } ,
1210
+ schema : { title : name ?? "extract" , ... asJsonSchema } ,
1221
1211
} ,
1222
1212
} as Partial < CallOptions > ) ;
1223
1213
} 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 ) ;
1224
1221
llm = this . withConfig ( {
1225
1222
response_format : {
1226
1223
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 ,
1233
1225
} ,
1234
1226
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
+ } ,
1237
1233
} ,
1238
1234
} as Partial < CallOptions > ) ;
1239
1235
if ( isInteropZodSchema ( schema ) ) {
@@ -1272,8 +1268,8 @@ abstract class BaseChatOpenAI<CallOptions extends BaseChatOpenAICallOptions>
1272
1268
} ,
1273
1269
} ,
1274
1270
ls_structured_output_format : {
1275
- kwargs : { method : "functionCalling " } ,
1276
- schema : asJsonSchema ,
1271
+ kwargs : { method : "function_calling " } ,
1272
+ schema : { title : functionName , ... asJsonSchema } ,
1277
1273
} ,
1278
1274
// Do not pass `strict` argument to OpenAI if `config.strict` is undefined
1279
1275
...( config ?. strict !== undefined ? { strict : config . strict } : { } ) ,
@@ -1300,6 +1296,7 @@ abstract class BaseChatOpenAI<CallOptions extends BaseChatOpenAICallOptions>
1300
1296
parameters : schema ,
1301
1297
} ;
1302
1298
}
1299
+ const asJsonSchema = toJsonSchema ( schema ) ;
1303
1300
llm = this . withConfig ( {
1304
1301
tools : [
1305
1302
{
@@ -1314,8 +1311,8 @@ abstract class BaseChatOpenAI<CallOptions extends BaseChatOpenAICallOptions>
1314
1311
} ,
1315
1312
} ,
1316
1313
ls_structured_output_format : {
1317
- kwargs : { method : "functionCalling " } ,
1318
- schema : toJsonSchema ( schema ) ,
1314
+ kwargs : { method : "function_calling " } ,
1315
+ schema : { title : functionName , ... asJsonSchema } ,
1319
1316
} ,
1320
1317
// Do not pass `strict` argument to OpenAI if `config.strict` is undefined
1321
1318
...( config ?. strict !== undefined ? { strict : config . strict } : { } ) ,
0 commit comments