@langchain/openai 0.5.17 → 0.5.18

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,8 +1,42 @@
1
1
  "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
2
35
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ChatOpenAI = exports.ChatOpenAICompletions = exports.ChatOpenAIResponses = void 0;
36
+ exports.ChatOpenAI = void 0;
4
37
  exports.messageToOpenAIRole = messageToOpenAIRole;
5
38
  exports._convertMessagesToOpenAIParams = _convertMessagesToOpenAIParams;
39
+ const z4 = __importStar(require("zod/v4/core"));
6
40
  const openai_1 = require("openai");
7
41
  const messages_1 = require("@langchain/core/messages");
8
42
  const outputs_1 = require("@langchain/core/outputs");
@@ -12,33 +46,13 @@ const base_1 = require("@langchain/core/language_models/base");
12
46
  const runnables_1 = require("@langchain/core/runnables");
13
47
  const output_parsers_1 = require("@langchain/core/output_parsers");
14
48
  const openai_tools_1 = require("@langchain/core/output_parsers/openai_tools");
49
+ const zod_1 = require("openai/helpers/zod");
15
50
  const types_1 = require("@langchain/core/utils/types");
16
51
  const json_schema_1 = require("@langchain/core/utils/json_schema");
17
52
  const azure_js_1 = require("./utils/azure.cjs");
18
53
  const openai_js_1 = require("./utils/openai.cjs");
19
54
  const openai_format_fndef_js_1 = require("./utils/openai-format-fndef.cjs");
20
55
  const tools_js_1 = require("./utils/tools.cjs");
21
- const _FUNCTION_CALL_IDS_MAP_KEY = "__openai_function_call_ids__";
22
- function isBuiltInTool(tool) {
23
- return "type" in tool && tool.type !== "function";
24
- }
25
- function isBuiltInToolChoice(tool_choice) {
26
- return (tool_choice != null &&
27
- typeof tool_choice === "object" &&
28
- "type" in tool_choice &&
29
- tool_choice.type !== "function");
30
- }
31
- function isReasoningModel(model) {
32
- return model && /^o\d/.test(model);
33
- }
34
- function isStructuredOutputMethodParams(x
35
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
36
- ) {
37
- return (x !== undefined &&
38
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
39
- typeof x.schema ===
40
- "object");
41
- }
42
56
  function extractGenericMessageCustomRole(message) {
43
57
  if (message.role !== "system" &&
44
58
  message.role !== "developer" &&
@@ -199,7 +213,6 @@ const completionsApiContentBlockConverter = {
199
213
  },
200
214
  };
201
215
  // Used in LangSmith, export is important here
202
- // TODO: put this conversion elsewhere
203
216
  function _convertMessagesToOpenAIParams(messages, model) {
204
217
  // TODO: Function messages do not support array content, fix cast
205
218
  return messages.flatMap((message) => {
@@ -248,1803 +261,498 @@ function _convertMessagesToOpenAIParams(messages, model) {
248
261
  id: message.additional_kwargs.audio.id,
249
262
  },
250
263
  };
251
- return [
252
- completionParam,
253
- audioMessage,
254
- ];
264
+ return [completionParam, audioMessage];
255
265
  }
256
266
  return completionParam;
257
267
  });
258
268
  }
259
- /** @internal */
260
- class BaseChatOpenAI extends chat_models_1.BaseChatModel {
261
- _llmType() {
262
- return "openai";
269
+ const _FUNCTION_CALL_IDS_MAP_KEY = "__openai_function_call_ids__";
270
+ function _convertReasoningSummaryToOpenAIResponsesParams(reasoning) {
271
+ // combine summary parts that have the the same index and then remove the indexes
272
+ const summary = (reasoning.summary.length > 1
273
+ ? reasoning.summary.reduce((acc, curr) => {
274
+ const last = acc.at(-1);
275
+ if (last.index === curr.index) {
276
+ last.text += curr.text;
277
+ }
278
+ else {
279
+ acc.push(curr);
280
+ }
281
+ return acc;
282
+ }, [{ ...reasoning.summary[0] }])
283
+ : reasoning.summary).map((s) => Object.fromEntries(Object.entries(s).filter(([k]) => k !== "index")));
284
+ return {
285
+ ...reasoning,
286
+ summary,
287
+ };
288
+ }
289
+ function _convertMessagesToOpenAIResponsesParams(messages, model, zdrEnabled) {
290
+ return messages.flatMap((lcMsg) => {
291
+ const additional_kwargs = lcMsg.additional_kwargs;
292
+ let role = messageToOpenAIRole(lcMsg);
293
+ if (role === "system" && isReasoningModel(model))
294
+ role = "developer";
295
+ if (role === "function") {
296
+ throw new Error("Function messages are not supported in Responses API");
297
+ }
298
+ if (role === "tool") {
299
+ const toolMessage = lcMsg;
300
+ // Handle computer call output
301
+ if (additional_kwargs?.type === "computer_call_output") {
302
+ const output = (() => {
303
+ if (typeof toolMessage.content === "string") {
304
+ return {
305
+ type: "computer_screenshot",
306
+ image_url: toolMessage.content,
307
+ };
308
+ }
309
+ if (Array.isArray(toolMessage.content)) {
310
+ const oaiScreenshot = toolMessage.content.find((i) => i.type === "computer_screenshot");
311
+ if (oaiScreenshot)
312
+ return oaiScreenshot;
313
+ const lcImage = toolMessage.content.find((i) => i.type === "image_url");
314
+ if (lcImage) {
315
+ return {
316
+ type: "computer_screenshot",
317
+ image_url: typeof lcImage.image_url === "string"
318
+ ? lcImage.image_url
319
+ : lcImage.image_url.url,
320
+ };
321
+ }
322
+ }
323
+ throw new Error("Invalid computer call output");
324
+ })();
325
+ return {
326
+ type: "computer_call_output",
327
+ output,
328
+ call_id: toolMessage.tool_call_id,
329
+ };
330
+ }
331
+ return {
332
+ type: "function_call_output",
333
+ call_id: toolMessage.tool_call_id,
334
+ id: toolMessage.id?.startsWith("fc_") ? toolMessage.id : undefined,
335
+ output: typeof toolMessage.content !== "string"
336
+ ? JSON.stringify(toolMessage.content)
337
+ : toolMessage.content,
338
+ };
339
+ }
340
+ if (role === "assistant") {
341
+ // if we have the original response items, just reuse them
342
+ if (!zdrEnabled &&
343
+ lcMsg.response_metadata.output != null &&
344
+ Array.isArray(lcMsg.response_metadata.output) &&
345
+ lcMsg.response_metadata.output.length > 0 &&
346
+ lcMsg.response_metadata.output.every((item) => "type" in item)) {
347
+ return lcMsg.response_metadata.output;
348
+ }
349
+ // otherwise, try to reconstruct the response from what we have
350
+ const input = [];
351
+ // reasoning items
352
+ if (additional_kwargs?.reasoning && !zdrEnabled) {
353
+ const reasoningItem = _convertReasoningSummaryToOpenAIResponsesParams(additional_kwargs.reasoning);
354
+ input.push(reasoningItem);
355
+ }
356
+ // ai content
357
+ let { content } = lcMsg;
358
+ if (additional_kwargs?.refusal) {
359
+ if (typeof content === "string") {
360
+ content = [{ type: "output_text", text: content, annotations: [] }];
361
+ }
362
+ content = [
363
+ ...content,
364
+ { type: "refusal", refusal: additional_kwargs.refusal },
365
+ ];
366
+ }
367
+ input.push({
368
+ type: "message",
369
+ role: "assistant",
370
+ ...(lcMsg.id && !zdrEnabled && lcMsg.id.startsWith("msg_")
371
+ ? { id: lcMsg.id }
372
+ : {}),
373
+ content: typeof content === "string"
374
+ ? content
375
+ : content.flatMap((item) => {
376
+ if (item.type === "text") {
377
+ return {
378
+ type: "output_text",
379
+ text: item.text,
380
+ // @ts-expect-error TODO: add types for `annotations`
381
+ annotations: item.annotations ?? [],
382
+ };
383
+ }
384
+ if (item.type === "output_text" || item.type === "refusal") {
385
+ return item;
386
+ }
387
+ return [];
388
+ }),
389
+ });
390
+ const functionCallIds = additional_kwargs?.[_FUNCTION_CALL_IDS_MAP_KEY];
391
+ if ((0, messages_1.isAIMessage)(lcMsg) && !!lcMsg.tool_calls?.length) {
392
+ input.push(...lcMsg.tool_calls.map((toolCall) => ({
393
+ type: "function_call",
394
+ name: toolCall.name,
395
+ arguments: JSON.stringify(toolCall.args),
396
+ call_id: toolCall.id,
397
+ ...(zdrEnabled ? { id: functionCallIds?.[toolCall.id] } : {}),
398
+ })));
399
+ }
400
+ else if (additional_kwargs?.tool_calls) {
401
+ input.push(...additional_kwargs.tool_calls.map((toolCall) => ({
402
+ type: "function_call",
403
+ name: toolCall.function.name,
404
+ call_id: toolCall.id,
405
+ arguments: toolCall.function.arguments,
406
+ ...(zdrEnabled ? { id: functionCallIds?.[toolCall.id] } : {}),
407
+ })));
408
+ }
409
+ const toolOutputs = lcMsg.response_metadata.output?.length
410
+ ? lcMsg.response_metadata.output
411
+ : additional_kwargs.tool_outputs;
412
+ const fallthroughCallTypes = [
413
+ "computer_call",
414
+ "mcp_call",
415
+ "code_interpreter_call",
416
+ "image_generation_call",
417
+ ];
418
+ if (toolOutputs != null) {
419
+ const castToolOutputs = toolOutputs;
420
+ const fallthroughCalls = castToolOutputs?.filter((item) => fallthroughCallTypes.includes(item.type));
421
+ if (fallthroughCalls.length > 0)
422
+ input.push(...fallthroughCalls);
423
+ }
424
+ return input;
425
+ }
426
+ if (role === "user" || role === "system" || role === "developer") {
427
+ if (typeof lcMsg.content === "string") {
428
+ return { type: "message", role, content: lcMsg.content };
429
+ }
430
+ const messages = [];
431
+ const content = lcMsg.content.flatMap((item) => {
432
+ if (item.type === "mcp_approval_response") {
433
+ messages.push({
434
+ type: "mcp_approval_response",
435
+ approval_request_id: item.approval_request_id,
436
+ approve: item.approve,
437
+ });
438
+ }
439
+ if ((0, messages_1.isDataContentBlock)(item)) {
440
+ return (0, messages_1.convertToProviderContentBlock)(item, completionsApiContentBlockConverter);
441
+ }
442
+ if (item.type === "text") {
443
+ return {
444
+ type: "input_text",
445
+ text: item.text,
446
+ };
447
+ }
448
+ if (item.type === "image_url") {
449
+ return {
450
+ type: "input_image",
451
+ image_url: typeof item.image_url === "string"
452
+ ? item.image_url
453
+ : item.image_url.url,
454
+ detail: typeof item.image_url === "string"
455
+ ? "auto"
456
+ : item.image_url.detail,
457
+ };
458
+ }
459
+ if (item.type === "input_text" ||
460
+ item.type === "input_image" ||
461
+ item.type === "input_file") {
462
+ return item;
463
+ }
464
+ return [];
465
+ });
466
+ if (content.length > 0) {
467
+ messages.push({ type: "message", role, content });
468
+ }
469
+ return messages;
470
+ }
471
+ console.warn(`Unsupported role found when converting to OpenAI Responses API: ${role}`);
472
+ return [];
473
+ });
474
+ }
475
+ function _convertOpenAIResponsesMessageToBaseMessage(response) {
476
+ if (response.error) {
477
+ // TODO: add support for `addLangChainErrorFields`
478
+ const error = new Error(response.error.message);
479
+ error.name = response.error.code;
480
+ throw error;
263
481
  }
264
- static lc_name() {
265
- return "ChatOpenAI";
482
+ let messageId;
483
+ const content = [];
484
+ const tool_calls = [];
485
+ const invalid_tool_calls = [];
486
+ const response_metadata = {
487
+ model: response.model,
488
+ created_at: response.created_at,
489
+ id: response.id,
490
+ incomplete_details: response.incomplete_details,
491
+ metadata: response.metadata,
492
+ object: response.object,
493
+ status: response.status,
494
+ user: response.user,
495
+ service_tier: response.service_tier,
496
+ // for compatibility with chat completion calls.
497
+ model_name: response.model,
498
+ };
499
+ const additional_kwargs = {};
500
+ for (const item of response.output) {
501
+ if (item.type === "message") {
502
+ messageId = item.id;
503
+ content.push(...item.content.flatMap((part) => {
504
+ if (part.type === "output_text") {
505
+ if ("parsed" in part && part.parsed != null) {
506
+ additional_kwargs.parsed = part.parsed;
507
+ }
508
+ return {
509
+ type: "text",
510
+ text: part.text,
511
+ annotations: part.annotations,
512
+ };
513
+ }
514
+ if (part.type === "refusal") {
515
+ additional_kwargs.refusal = part.refusal;
516
+ return [];
517
+ }
518
+ return part;
519
+ }));
520
+ }
521
+ else if (item.type === "function_call") {
522
+ const fnAdapter = {
523
+ function: { name: item.name, arguments: item.arguments },
524
+ id: item.call_id,
525
+ };
526
+ try {
527
+ tool_calls.push((0, openai_tools_1.parseToolCall)(fnAdapter, { returnId: true }));
528
+ }
529
+ catch (e) {
530
+ let errMessage;
531
+ if (typeof e === "object" &&
532
+ e != null &&
533
+ "message" in e &&
534
+ typeof e.message === "string") {
535
+ errMessage = e.message;
536
+ }
537
+ invalid_tool_calls.push((0, openai_tools_1.makeInvalidToolCall)(fnAdapter, errMessage));
538
+ }
539
+ additional_kwargs[_FUNCTION_CALL_IDS_MAP_KEY] ??= {};
540
+ if (item.id) {
541
+ additional_kwargs[_FUNCTION_CALL_IDS_MAP_KEY][item.call_id] = item.id;
542
+ }
543
+ }
544
+ else if (item.type === "reasoning") {
545
+ additional_kwargs.reasoning = item;
546
+ }
547
+ else {
548
+ additional_kwargs.tool_outputs ??= [];
549
+ additional_kwargs.tool_outputs.push(item);
550
+ }
266
551
  }
267
- get callKeys() {
268
- return [
269
- ...super.callKeys,
270
- "options",
271
- "function_call",
272
- "functions",
273
- "tools",
274
- "tool_choice",
275
- "promptIndex",
276
- "response_format",
277
- "seed",
278
- "reasoning",
279
- "service_tier",
280
- ];
552
+ return new messages_1.AIMessage({
553
+ id: messageId,
554
+ content,
555
+ tool_calls,
556
+ invalid_tool_calls,
557
+ usage_metadata: response.usage,
558
+ additional_kwargs,
559
+ response_metadata,
560
+ });
561
+ }
562
+ function _convertOpenAIResponsesDeltaToBaseMessageChunk(chunk) {
563
+ const content = [];
564
+ let generationInfo = {};
565
+ let usage_metadata;
566
+ const tool_call_chunks = [];
567
+ const response_metadata = {};
568
+ const additional_kwargs = {};
569
+ let id;
570
+ if (chunk.type === "response.output_text.delta") {
571
+ content.push({
572
+ type: "text",
573
+ text: chunk.delta,
574
+ index: chunk.content_index,
575
+ });
281
576
  }
282
- get lc_secrets() {
283
- return {
284
- apiKey: "OPENAI_API_KEY",
285
- organization: "OPENAI_ORGANIZATION",
577
+ else if (chunk.type === "response.output_text_annotation.added") {
578
+ content.push({
579
+ type: "text",
580
+ text: "",
581
+ annotations: [chunk.annotation],
582
+ index: chunk.content_index,
583
+ });
584
+ }
585
+ else if (chunk.type === "response.output_item.added" &&
586
+ chunk.item.type === "message") {
587
+ id = chunk.item.id;
588
+ }
589
+ else if (chunk.type === "response.output_item.added" &&
590
+ chunk.item.type === "function_call") {
591
+ tool_call_chunks.push({
592
+ type: "tool_call_chunk",
593
+ name: chunk.item.name,
594
+ args: chunk.item.arguments,
595
+ id: chunk.item.call_id,
596
+ index: chunk.output_index,
597
+ });
598
+ additional_kwargs[_FUNCTION_CALL_IDS_MAP_KEY] = {
599
+ [chunk.item.call_id]: chunk.item.id,
286
600
  };
287
601
  }
288
- get lc_aliases() {
289
- return {
290
- apiKey: "openai_api_key",
602
+ else if (chunk.type === "response.output_item.done" &&
603
+ [
604
+ "web_search_call",
605
+ "file_search_call",
606
+ "computer_call",
607
+ "code_interpreter_call",
608
+ "mcp_call",
609
+ "mcp_list_tools",
610
+ "mcp_approval_request",
611
+ "image_generation_call",
612
+ ].includes(chunk.item.type)) {
613
+ additional_kwargs.tool_outputs = [chunk.item];
614
+ }
615
+ else if (chunk.type === "response.created") {
616
+ response_metadata.id = chunk.response.id;
617
+ response_metadata.model_name = chunk.response.model;
618
+ response_metadata.model = chunk.response.model;
619
+ }
620
+ else if (chunk.type === "response.completed") {
621
+ const msg = _convertOpenAIResponsesMessageToBaseMessage(chunk.response);
622
+ usage_metadata = chunk.response.usage;
623
+ if (chunk.response.text?.format?.type === "json_schema") {
624
+ additional_kwargs.parsed ??= JSON.parse(msg.text);
625
+ }
626
+ for (const [key, value] of Object.entries(chunk.response)) {
627
+ if (key !== "id")
628
+ response_metadata[key] = value;
629
+ }
630
+ }
631
+ else if (chunk.type === "response.function_call_arguments.delta") {
632
+ tool_call_chunks.push({
633
+ type: "tool_call_chunk",
634
+ args: chunk.delta,
635
+ index: chunk.output_index,
636
+ });
637
+ }
638
+ else if (chunk.type === "response.web_search_call.completed" ||
639
+ chunk.type === "response.file_search_call.completed") {
640
+ generationInfo = {
641
+ tool_outputs: {
642
+ id: chunk.item_id,
643
+ type: chunk.type.replace("response.", "").replace(".completed", ""),
644
+ status: "completed",
645
+ },
291
646
  };
292
647
  }
293
- get lc_serializable_keys() {
294
- return [
295
- "configuration",
296
- "logprobs",
297
- "topLogprobs",
298
- "prefixMessages",
299
- "supportsStrictToolCalling",
300
- "modalities",
301
- "audio",
302
- "temperature",
303
- "maxTokens",
304
- "topP",
305
- "frequencyPenalty",
306
- "presencePenalty",
307
- "n",
308
- "logitBias",
309
- "user",
310
- "streaming",
311
- "streamUsage",
312
- "model",
313
- "modelKwargs",
314
- "stop",
315
- "stopSequences",
316
- "timeout",
317
- "apiKey",
318
- "cache",
319
- "maxConcurrency",
320
- "maxRetries",
321
- "verbose",
322
- "callbacks",
323
- "tags",
324
- "metadata",
325
- "disableStreaming",
326
- "zdrEnabled",
327
- "reasoning",
328
- ];
648
+ else if (chunk.type === "response.refusal.done") {
649
+ additional_kwargs.refusal = chunk.refusal;
329
650
  }
330
- getLsParams(options) {
331
- const params = this.invocationParams(options);
332
- return {
333
- ls_provider: "openai",
334
- ls_model_name: this.model,
335
- ls_model_type: "chat",
336
- ls_temperature: params.temperature ?? undefined,
337
- ls_max_tokens: params.max_tokens ?? undefined,
338
- ls_stop: options.stop,
651
+ else if (chunk.type === "response.output_item.added" &&
652
+ "item" in chunk &&
653
+ chunk.item.type === "reasoning") {
654
+ const summary = chunk
655
+ .item.summary
656
+ ? chunk.item.summary.map((s, index) => ({
657
+ ...s,
658
+ index,
659
+ }))
660
+ : undefined;
661
+ additional_kwargs.reasoning = {
662
+ // We only capture ID in the first chunk or else the concatenated result of all chunks will
663
+ // have an ID field that is repeated once per chunk. There is special handling for the `type`
664
+ // field that prevents this, however.
665
+ id: chunk.item.id,
666
+ type: chunk.item.type,
667
+ ...(summary ? { summary } : {}),
339
668
  };
340
669
  }
341
- /** @ignore */
342
- _identifyingParams() {
343
- return {
344
- model_name: this.model,
345
- ...this.invocationParams(),
346
- ...this.clientConfig,
670
+ else if (chunk.type === "response.reasoning_summary_part.added") {
671
+ additional_kwargs.reasoning = {
672
+ type: "reasoning",
673
+ summary: [{ ...chunk.part, index: chunk.summary_index }],
347
674
  };
348
675
  }
349
- /**
350
- * Get the identifying parameters for the model
351
- */
352
- identifyingParams() {
353
- return this._identifyingParams();
676
+ else if (chunk.type === "response.reasoning_summary_text.delta") {
677
+ additional_kwargs.reasoning = {
678
+ type: "reasoning",
679
+ summary: [
680
+ { text: chunk.delta, type: "summary_text", index: chunk.summary_index },
681
+ ],
682
+ };
354
683
  }
355
- constructor(fields) {
356
- super(fields ?? {});
357
- Object.defineProperty(this, "temperature", {
358
- enumerable: true,
359
- configurable: true,
360
- writable: true,
361
- value: void 0
362
- });
363
- Object.defineProperty(this, "topP", {
364
- enumerable: true,
365
- configurable: true,
366
- writable: true,
367
- value: void 0
368
- });
369
- Object.defineProperty(this, "frequencyPenalty", {
370
- enumerable: true,
371
- configurable: true,
372
- writable: true,
373
- value: void 0
374
- });
375
- Object.defineProperty(this, "presencePenalty", {
376
- enumerable: true,
377
- configurable: true,
378
- writable: true,
379
- value: void 0
380
- });
381
- Object.defineProperty(this, "n", {
382
- enumerable: true,
383
- configurable: true,
384
- writable: true,
385
- value: void 0
386
- });
387
- Object.defineProperty(this, "logitBias", {
388
- enumerable: true,
389
- configurable: true,
390
- writable: true,
391
- value: void 0
392
- });
393
- Object.defineProperty(this, "model", {
394
- enumerable: true,
395
- configurable: true,
396
- writable: true,
397
- value: "gpt-3.5-turbo"
398
- });
399
- Object.defineProperty(this, "modelKwargs", {
400
- enumerable: true,
401
- configurable: true,
402
- writable: true,
403
- value: void 0
404
- });
405
- Object.defineProperty(this, "stop", {
406
- enumerable: true,
407
- configurable: true,
408
- writable: true,
409
- value: void 0
410
- });
411
- Object.defineProperty(this, "stopSequences", {
412
- enumerable: true,
413
- configurable: true,
414
- writable: true,
415
- value: void 0
416
- });
417
- Object.defineProperty(this, "user", {
418
- enumerable: true,
419
- configurable: true,
420
- writable: true,
421
- value: void 0
422
- });
423
- Object.defineProperty(this, "timeout", {
424
- enumerable: true,
425
- configurable: true,
426
- writable: true,
427
- value: void 0
428
- });
429
- Object.defineProperty(this, "streaming", {
430
- enumerable: true,
431
- configurable: true,
432
- writable: true,
433
- value: false
434
- });
435
- Object.defineProperty(this, "streamUsage", {
436
- enumerable: true,
437
- configurable: true,
438
- writable: true,
439
- value: true
440
- });
441
- Object.defineProperty(this, "maxTokens", {
442
- enumerable: true,
443
- configurable: true,
444
- writable: true,
445
- value: void 0
446
- });
447
- Object.defineProperty(this, "logprobs", {
448
- enumerable: true,
449
- configurable: true,
450
- writable: true,
451
- value: void 0
452
- });
453
- Object.defineProperty(this, "topLogprobs", {
454
- enumerable: true,
455
- configurable: true,
456
- writable: true,
457
- value: void 0
458
- });
459
- Object.defineProperty(this, "apiKey", {
460
- enumerable: true,
461
- configurable: true,
462
- writable: true,
463
- value: void 0
464
- });
465
- Object.defineProperty(this, "organization", {
466
- enumerable: true,
467
- configurable: true,
468
- writable: true,
469
- value: void 0
470
- });
471
- Object.defineProperty(this, "__includeRawResponse", {
472
- enumerable: true,
473
- configurable: true,
474
- writable: true,
475
- value: void 0
476
- });
477
- Object.defineProperty(this, "client", {
478
- enumerable: true,
479
- configurable: true,
480
- writable: true,
481
- value: void 0
482
- });
483
- Object.defineProperty(this, "clientConfig", {
484
- enumerable: true,
485
- configurable: true,
486
- writable: true,
487
- value: void 0
488
- });
489
- /**
490
- * Whether the model supports the `strict` argument when passing in tools.
491
- * If `undefined` the `strict` argument will not be passed to OpenAI.
492
- */
493
- Object.defineProperty(this, "supportsStrictToolCalling", {
494
- enumerable: true,
495
- configurable: true,
496
- writable: true,
497
- value: void 0
498
- });
499
- Object.defineProperty(this, "audio", {
500
- enumerable: true,
501
- configurable: true,
502
- writable: true,
503
- value: void 0
504
- });
505
- Object.defineProperty(this, "modalities", {
506
- enumerable: true,
507
- configurable: true,
508
- writable: true,
509
- value: void 0
510
- });
511
- Object.defineProperty(this, "reasoning", {
512
- enumerable: true,
513
- configurable: true,
514
- writable: true,
515
- value: void 0
516
- });
517
- /**
518
- * Must be set to `true` in tenancies with Zero Data Retention. Setting to `true` will disable
519
- * output storage in the Responses API, but this DOES NOT enable Zero Data Retention in your
520
- * OpenAI organization or project. This must be configured directly with OpenAI.
521
- *
522
- * See:
523
- * https://help.openai.com/en/articles/10503543-data-residency-for-the-openai-api
524
- * https://platform.openai.com/docs/api-reference/responses/create#responses-create-store
525
- *
526
- * @default false
527
- */
528
- Object.defineProperty(this, "zdrEnabled", {
529
- enumerable: true,
530
- configurable: true,
531
- writable: true,
532
- value: void 0
533
- });
534
- /**
535
- * Service tier to use for this request. Can be "auto", "default", or "flex" or "priority".
536
- * Specifies the service tier for prioritization and latency optimization.
537
- */
538
- Object.defineProperty(this, "service_tier", {
539
- enumerable: true,
540
- configurable: true,
541
- writable: true,
542
- value: void 0
543
- });
544
- Object.defineProperty(this, "lc_serializable", {
545
- enumerable: true,
546
- configurable: true,
547
- writable: true,
548
- value: true
549
- });
550
- this.apiKey =
551
- fields?.apiKey ??
552
- fields?.configuration?.apiKey ??
553
- (0, env_1.getEnvironmentVariable)("OPENAI_API_KEY");
554
- this.organization =
555
- fields?.configuration?.organization ??
556
- (0, env_1.getEnvironmentVariable)("OPENAI_ORGANIZATION");
557
- this.model = fields?.model ?? this.model;
558
- this.modelKwargs = fields?.modelKwargs ?? {};
559
- this.timeout = fields?.timeout;
560
- this.temperature = fields?.temperature ?? this.temperature;
561
- this.topP = fields?.topP ?? this.topP;
562
- this.frequencyPenalty = fields?.frequencyPenalty ?? this.frequencyPenalty;
563
- this.presencePenalty = fields?.presencePenalty ?? this.presencePenalty;
564
- this.logprobs = fields?.logprobs;
565
- this.topLogprobs = fields?.topLogprobs;
566
- this.n = fields?.n ?? this.n;
567
- this.logitBias = fields?.logitBias;
568
- this.stop = fields?.stopSequences ?? fields?.stop;
569
- this.stopSequences = this.stop;
570
- this.user = fields?.user;
571
- this.__includeRawResponse = fields?.__includeRawResponse;
572
- this.audio = fields?.audio;
573
- this.modalities = fields?.modalities;
574
- this.reasoning = fields?.reasoning;
575
- this.maxTokens = fields?.maxCompletionTokens ?? fields?.maxTokens;
576
- this.disableStreaming = fields?.disableStreaming ?? this.disableStreaming;
577
- this.streaming = fields?.streaming ?? false;
578
- if (this.disableStreaming)
579
- this.streaming = false;
580
- this.streamUsage = fields?.streamUsage ?? this.streamUsage;
581
- if (this.disableStreaming)
582
- this.streamUsage = false;
583
- this.clientConfig = {
584
- apiKey: this.apiKey,
585
- organization: this.organization,
586
- dangerouslyAllowBrowser: true,
587
- ...fields?.configuration,
588
- };
589
- // If `supportsStrictToolCalling` is explicitly set, use that value.
590
- // Else leave undefined so it's not passed to OpenAI.
591
- if (fields?.supportsStrictToolCalling !== undefined) {
592
- this.supportsStrictToolCalling = fields.supportsStrictToolCalling;
593
- }
594
- if (fields?.service_tier !== undefined) {
595
- this.service_tier = fields.service_tier;
596
- }
597
- this.zdrEnabled = fields?.zdrEnabled ?? false;
684
+ else if (chunk.type === "response.image_generation_call.partial_image") {
685
+ // noop/fixme: retaining partial images in a message chunk means that _all_
686
+ // partial images get kept in history, so we don't do anything here.
687
+ return null;
598
688
  }
599
- /**
600
- * Returns backwards compatible reasoning parameters from constructor params and call options
601
- * @internal
602
- */
603
- _getReasoningParams(options) {
604
- if (!isReasoningModel(this.model)) {
605
- return;
606
- }
607
- // apply options in reverse order of importance -- newer options supersede older options
608
- let reasoning;
609
- if (this.reasoning !== undefined) {
610
- reasoning = {
611
- ...reasoning,
612
- ...this.reasoning,
613
- };
614
- }
615
- if (options?.reasoning !== undefined) {
616
- reasoning = {
617
- ...reasoning,
618
- ...options.reasoning,
619
- };
620
- }
621
- return reasoning;
689
+ else {
690
+ return null;
622
691
  }
623
- /**
624
- * Returns an openai compatible response format from a set of options
625
- * @internal
626
- */
627
- _getResponseFormat(resFormat) {
628
- if (resFormat &&
629
- resFormat.type === "json_schema" &&
630
- resFormat.json_schema.schema &&
631
- (0, types_1.isInteropZodSchema)(resFormat.json_schema.schema)) {
632
- return (0, openai_js_1.interopZodResponseFormat)(resFormat.json_schema.schema, resFormat.json_schema.name, {
633
- description: resFormat.json_schema.description,
692
+ return new outputs_1.ChatGenerationChunk({
693
+ // Legacy reasons, `onLLMNewToken` should pulls this out
694
+ text: content.map((part) => part.text).join(""),
695
+ message: new messages_1.AIMessageChunk({
696
+ id,
697
+ content,
698
+ tool_call_chunks,
699
+ usage_metadata,
700
+ additional_kwargs,
701
+ response_metadata,
702
+ }),
703
+ generationInfo,
704
+ });
705
+ }
706
+ function isBuiltInTool(tool) {
707
+ return "type" in tool && tool.type !== "function";
708
+ }
709
+ function isBuiltInToolChoice(tool_choice) {
710
+ return (tool_choice != null &&
711
+ typeof tool_choice === "object" &&
712
+ "type" in tool_choice &&
713
+ tool_choice.type !== "function");
714
+ }
715
+ function _reduceChatOpenAIToolsForResponses(tools, fields) {
716
+ const reducedTools = [];
717
+ for (const tool of tools) {
718
+ if (isBuiltInTool(tool)) {
719
+ if (tool.type === "image_generation" && fields?.stream) {
720
+ // OpenAI sends a 400 error if partial_images is not set and we want to stream.
721
+ // We also set it to 1 since we don't support partial images yet.
722
+ tool.partial_images = 1;
723
+ }
724
+ reducedTools.push(tool);
725
+ }
726
+ else if ((0, base_1.isOpenAITool)(tool)) {
727
+ reducedTools.push({
728
+ type: "function",
729
+ name: tool.function.name,
730
+ parameters: tool.function.parameters,
731
+ description: tool.function.description,
732
+ strict: fields?.strict ?? null,
634
733
  });
635
734
  }
636
- return resFormat;
637
735
  }
638
- _getClientOptions(options) {
639
- if (!this.client) {
640
- const openAIEndpointConfig = {
641
- baseURL: this.clientConfig.baseURL,
642
- };
643
- const endpoint = (0, azure_js_1.getEndpoint)(openAIEndpointConfig);
644
- const params = {
645
- ...this.clientConfig,
646
- baseURL: endpoint,
647
- timeout: this.timeout,
648
- maxRetries: 0,
736
+ return reducedTools;
737
+ }
738
+ function _convertChatOpenAIToolTypeToOpenAITool(tool, fields) {
739
+ if ((0, base_1.isOpenAITool)(tool)) {
740
+ if (fields?.strict !== undefined) {
741
+ return {
742
+ ...tool,
743
+ function: {
744
+ ...tool.function,
745
+ strict: fields.strict,
746
+ },
649
747
  };
650
- if (!params.baseURL) {
651
- delete params.baseURL;
652
- }
653
- this.client = new openai_1.OpenAI(params);
654
- }
655
- const requestOptions = {
656
- ...this.clientConfig,
657
- ...options,
658
- };
659
- return requestOptions;
660
- }
661
- // TODO: move to completions class
662
- _convertChatOpenAIToolToCompletionsTool(tool, fields) {
663
- if ((0, base_1.isOpenAITool)(tool)) {
664
- if (fields?.strict !== undefined) {
665
- return {
666
- ...tool,
667
- function: {
668
- ...tool.function,
669
- strict: fields.strict,
670
- },
671
- };
672
- }
673
- return tool;
674
- }
675
- return (0, tools_js_1._convertToOpenAITool)(tool, fields);
676
- }
677
- bindTools(tools, kwargs) {
678
- let strict;
679
- if (kwargs?.strict !== undefined) {
680
- strict = kwargs.strict;
681
- }
682
- else if (this.supportsStrictToolCalling !== undefined) {
683
- strict = this.supportsStrictToolCalling;
684
- }
685
- return this.withConfig({
686
- tools: tools.map((tool) => isBuiltInTool(tool)
687
- ? tool
688
- : this._convertChatOpenAIToolToCompletionsTool(tool, { strict })),
689
- ...kwargs,
690
- });
691
- }
692
- /** @ignore */
693
- _combineLLMOutput(...llmOutputs) {
694
- return llmOutputs.reduce((acc, llmOutput) => {
695
- if (llmOutput && llmOutput.tokenUsage) {
696
- acc.tokenUsage.completionTokens +=
697
- llmOutput.tokenUsage.completionTokens ?? 0;
698
- acc.tokenUsage.promptTokens += llmOutput.tokenUsage.promptTokens ?? 0;
699
- acc.tokenUsage.totalTokens += llmOutput.tokenUsage.totalTokens ?? 0;
700
- }
701
- return acc;
702
- }, {
703
- tokenUsage: {
704
- completionTokens: 0,
705
- promptTokens: 0,
706
- totalTokens: 0,
707
- },
708
- });
709
- }
710
- async getNumTokensFromMessages(messages) {
711
- let totalCount = 0;
712
- let tokensPerMessage = 0;
713
- let tokensPerName = 0;
714
- // From: https://github.com/openai/openai-cookbook/blob/main/examples/How_to_format_inputs_to_ChatGPT_models.ipynb
715
- if (this.model === "gpt-3.5-turbo-0301") {
716
- tokensPerMessage = 4;
717
- tokensPerName = -1;
718
- }
719
- else {
720
- tokensPerMessage = 3;
721
- tokensPerName = 1;
722
- }
723
- const countPerMessage = await Promise.all(messages.map(async (message) => {
724
- const textCount = await this.getNumTokens(message.content);
725
- const roleCount = await this.getNumTokens(messageToOpenAIRole(message));
726
- const nameCount = message.name !== undefined
727
- ? tokensPerName + (await this.getNumTokens(message.name))
728
- : 0;
729
- let count = textCount + tokensPerMessage + roleCount + nameCount;
730
- // From: https://github.com/hmarr/openai-chat-tokens/blob/main/src/index.ts messageTokenEstimate
731
- const openAIMessage = message;
732
- if (openAIMessage._getType() === "function") {
733
- count -= 2;
734
- }
735
- if (openAIMessage.additional_kwargs?.function_call) {
736
- count += 3;
737
- }
738
- if (openAIMessage?.additional_kwargs.function_call?.name) {
739
- count += await this.getNumTokens(openAIMessage.additional_kwargs.function_call?.name);
740
- }
741
- if (openAIMessage.additional_kwargs.function_call?.arguments) {
742
- try {
743
- count += await this.getNumTokens(
744
- // Remove newlines and spaces
745
- JSON.stringify(JSON.parse(openAIMessage.additional_kwargs.function_call?.arguments)));
746
- }
747
- catch (error) {
748
- console.error("Error parsing function arguments", error, JSON.stringify(openAIMessage.additional_kwargs.function_call));
749
- count += await this.getNumTokens(openAIMessage.additional_kwargs.function_call?.arguments);
750
- }
751
- }
752
- totalCount += count;
753
- return count;
754
- }));
755
- totalCount += 3; // every reply is primed with <|start|>assistant<|message|>
756
- return { totalCount, countPerMessage };
757
- }
758
- /** @internal */
759
- async _getNumTokensFromGenerations(generations) {
760
- const generationUsages = await Promise.all(generations.map(async (generation) => {
761
- if (generation.message.additional_kwargs?.function_call) {
762
- return (await this.getNumTokensFromMessages([generation.message]))
763
- .countPerMessage[0];
764
- }
765
- else {
766
- return await this.getNumTokens(generation.message.content);
767
- }
768
- }));
769
- return generationUsages.reduce((a, b) => a + b, 0);
770
- }
771
- /** @internal */
772
- async _getEstimatedTokenCountFromPrompt(messages, functions, function_call) {
773
- // It appears that if functions are present, the first system message is padded with a trailing newline. This
774
- // was inferred by trying lots of combinations of messages and functions and seeing what the token counts were.
775
- let tokens = (await this.getNumTokensFromMessages(messages)).totalCount;
776
- // If there are functions, add the function definitions as they count towards token usage
777
- if (functions && function_call !== "auto") {
778
- const promptDefinitions = (0, openai_format_fndef_js_1.formatFunctionDefinitions)(functions);
779
- tokens += await this.getNumTokens(promptDefinitions);
780
- tokens += 9; // Add nine per completion
781
- }
782
- // If there's a system message _and_ functions are present, subtract four tokens. I assume this is because
783
- // functions typically add a system message, but reuse the first one if it's already there. This offsets
784
- // the extra 9 tokens added by the function definitions.
785
- if (functions && messages.find((m) => m._getType() === "system")) {
786
- tokens -= 4;
787
- }
788
- // If function_call is 'none', add one token.
789
- // If it's a FunctionCall object, add 4 + the number of tokens in the function name.
790
- // If it's undefined or 'auto', don't add anything.
791
- if (function_call === "none") {
792
- tokens += 1;
793
- }
794
- else if (typeof function_call === "object") {
795
- tokens += (await this.getNumTokens(function_call.name)) + 4;
796
- }
797
- return tokens;
798
- }
799
- withStructuredOutput(outputSchema, config) {
800
- // ):
801
- // | Runnable<BaseLanguageModelInput, RunOutput>
802
- // | Runnable<
803
- // BaseLanguageModelInput,
804
- // { raw: BaseMessage; parsed: RunOutput }
805
- // > {
806
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
807
- let schema;
808
- let name;
809
- let method;
810
- let includeRaw;
811
- if (isStructuredOutputMethodParams(outputSchema)) {
812
- schema = outputSchema.schema;
813
- name = outputSchema.name;
814
- method = outputSchema.method;
815
- includeRaw = outputSchema.includeRaw;
816
- }
817
- else {
818
- schema = outputSchema;
819
- name = config?.name;
820
- method = config?.method;
821
- includeRaw = config?.includeRaw;
822
- }
823
- let llm;
824
- let outputParser;
825
- if (config?.strict !== undefined && method === "jsonMode") {
826
- throw new Error("Argument `strict` is only supported for `method` = 'function_calling'");
827
- }
828
- if (!this.model.startsWith("gpt-3") &&
829
- !this.model.startsWith("gpt-4-") &&
830
- this.model !== "gpt-4") {
831
- if (method === undefined) {
832
- method = "jsonSchema";
833
- }
834
- }
835
- else if (method === "jsonSchema") {
836
- console.warn(`[WARNING]: JSON Schema is not supported for model "${this.model}". Falling back to tool calling.`);
837
- }
838
- if (method === "jsonMode") {
839
- let outputFormatSchema;
840
- if ((0, types_1.isInteropZodSchema)(schema)) {
841
- outputParser = output_parsers_1.StructuredOutputParser.fromZodSchema(schema);
842
- outputFormatSchema = (0, json_schema_1.toJsonSchema)(schema);
843
- }
844
- else {
845
- outputParser = new output_parsers_1.JsonOutputParser();
846
- }
847
- llm = this.withConfig({
848
- response_format: { type: "json_object" },
849
- ls_structured_output_format: {
850
- kwargs: { method: "jsonMode" },
851
- schema: outputFormatSchema,
852
- },
853
- });
854
- }
855
- else if (method === "jsonSchema") {
856
- llm = this.withConfig({
857
- response_format: {
858
- type: "json_schema",
859
- json_schema: {
860
- name: name ?? "extract",
861
- description: (0, types_1.getSchemaDescription)(schema),
862
- schema,
863
- strict: config?.strict,
864
- },
865
- },
866
- ls_structured_output_format: {
867
- kwargs: { method: "jsonSchema" },
868
- schema: (0, json_schema_1.toJsonSchema)(schema),
869
- },
870
- });
871
- if ((0, types_1.isInteropZodSchema)(schema)) {
872
- const altParser = output_parsers_1.StructuredOutputParser.fromZodSchema(schema);
873
- outputParser = runnables_1.RunnableLambda.from((aiMessage) => {
874
- if ("parsed" in aiMessage.additional_kwargs) {
875
- return aiMessage.additional_kwargs.parsed;
876
- }
877
- return altParser;
878
- });
879
- }
880
- else {
881
- outputParser = new output_parsers_1.JsonOutputParser();
882
- }
883
- }
884
- else {
885
- let functionName = name ?? "extract";
886
- // Is function calling
887
- if ((0, types_1.isInteropZodSchema)(schema)) {
888
- const asJsonSchema = (0, json_schema_1.toJsonSchema)(schema);
889
- llm = this.withConfig({
890
- tools: [
891
- {
892
- type: "function",
893
- function: {
894
- name: functionName,
895
- description: asJsonSchema.description,
896
- parameters: asJsonSchema,
897
- },
898
- },
899
- ],
900
- tool_choice: {
901
- type: "function",
902
- function: {
903
- name: functionName,
904
- },
905
- },
906
- ls_structured_output_format: {
907
- kwargs: { method: "functionCalling" },
908
- schema: asJsonSchema,
909
- },
910
- // Do not pass `strict` argument to OpenAI if `config.strict` is undefined
911
- ...(config?.strict !== undefined ? { strict: config.strict } : {}),
912
- });
913
- outputParser = new openai_tools_1.JsonOutputKeyToolsParser({
914
- returnSingle: true,
915
- keyName: functionName,
916
- zodSchema: schema,
917
- });
918
- }
919
- else {
920
- let openAIFunctionDefinition;
921
- if (typeof schema.name === "string" &&
922
- typeof schema.parameters === "object" &&
923
- schema.parameters != null) {
924
- openAIFunctionDefinition = schema;
925
- functionName = schema.name;
926
- }
927
- else {
928
- functionName = schema.title ?? functionName;
929
- openAIFunctionDefinition = {
930
- name: functionName,
931
- description: schema.description ?? "",
932
- parameters: schema,
933
- };
934
- }
935
- llm = this.withConfig({
936
- tools: [
937
- {
938
- type: "function",
939
- function: openAIFunctionDefinition,
940
- },
941
- ],
942
- tool_choice: {
943
- type: "function",
944
- function: {
945
- name: functionName,
946
- },
947
- },
948
- ls_structured_output_format: {
949
- kwargs: { method: "functionCalling" },
950
- schema: (0, json_schema_1.toJsonSchema)(schema),
951
- },
952
- // Do not pass `strict` argument to OpenAI if `config.strict` is undefined
953
- ...(config?.strict !== undefined ? { strict: config.strict } : {}),
954
- });
955
- outputParser = new openai_tools_1.JsonOutputKeyToolsParser({
956
- returnSingle: true,
957
- keyName: functionName,
958
- });
959
- }
960
- }
961
- if (!includeRaw) {
962
- return llm.pipe(outputParser);
963
- }
964
- const parserAssign = runnables_1.RunnablePassthrough.assign({
965
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
966
- parsed: (input, config) => outputParser.invoke(input.raw, config),
967
- });
968
- const parserNone = runnables_1.RunnablePassthrough.assign({
969
- parsed: () => null,
970
- });
971
- const parsedWithFallback = parserAssign.withFallbacks({
972
- fallbacks: [parserNone],
973
- });
974
- return runnables_1.RunnableSequence.from([{ raw: llm }, parsedWithFallback]);
975
- }
976
- }
977
- /**
978
- * OpenAI Responses API implementation.
979
- *
980
- * Will be exported in a later version of @langchain/openai.
981
- *
982
- * @internal
983
- */
984
- class ChatOpenAIResponses extends BaseChatOpenAI {
985
- invocationParams(options) {
986
- let strict;
987
- if (options?.strict !== undefined) {
988
- strict = options.strict;
989
- }
990
- else if (this.supportsStrictToolCalling !== undefined) {
991
- strict = this.supportsStrictToolCalling;
992
- }
993
- const params = {
994
- model: this.model,
995
- temperature: this.temperature,
996
- top_p: this.topP,
997
- user: this.user,
998
- // if include_usage is set or streamUsage then stream must be set to true.
999
- stream: this.streaming,
1000
- previous_response_id: options?.previous_response_id,
1001
- truncation: options?.truncation,
1002
- include: options?.include,
1003
- tools: options?.tools?.length
1004
- ? this._reduceChatOpenAITools(options.tools, {
1005
- stream: this.streaming,
1006
- strict,
1007
- })
1008
- : undefined,
1009
- tool_choice: isBuiltInToolChoice(options?.tool_choice)
1010
- ? options?.tool_choice
1011
- : (() => {
1012
- const formatted = (0, openai_js_1.formatToOpenAIToolChoice)(options?.tool_choice);
1013
- if (typeof formatted === "object" && "type" in formatted) {
1014
- return { type: "function", name: formatted.function.name };
1015
- }
1016
- else {
1017
- return undefined;
1018
- }
1019
- })(),
1020
- text: (() => {
1021
- if (options?.text)
1022
- return options.text;
1023
- const format = this._getResponseFormat(options?.response_format);
1024
- if (format?.type === "json_schema") {
1025
- if (format.json_schema.schema != null) {
1026
- return {
1027
- format: {
1028
- type: "json_schema",
1029
- schema: format.json_schema.schema,
1030
- description: format.json_schema.description,
1031
- name: format.json_schema.name,
1032
- strict: format.json_schema.strict,
1033
- },
1034
- };
1035
- }
1036
- return undefined;
1037
- }
1038
- return { format };
1039
- })(),
1040
- parallel_tool_calls: options?.parallel_tool_calls,
1041
- max_output_tokens: this.maxTokens === -1 ? undefined : this.maxTokens,
1042
- ...(this.zdrEnabled ? { store: false } : {}),
1043
- ...this.modelKwargs,
1044
- };
1045
- const reasoning = this._getReasoningParams(options);
1046
- if (reasoning !== undefined) {
1047
- params.reasoning = reasoning;
1048
- }
1049
- return params;
1050
- }
1051
- async _generate(messages, options) {
1052
- const invocationParams = this.invocationParams(options);
1053
- if (invocationParams.stream) {
1054
- const stream = this._streamResponseChunks(messages, options);
1055
- let finalChunk;
1056
- for await (const chunk of stream) {
1057
- chunk.message.response_metadata = {
1058
- ...chunk.generationInfo,
1059
- ...chunk.message.response_metadata,
1060
- };
1061
- finalChunk = finalChunk?.concat(chunk) ?? chunk;
1062
- }
1063
- return {
1064
- generations: finalChunk ? [finalChunk] : [],
1065
- llmOutput: {
1066
- estimatedTokenUsage: finalChunk?.message
1067
- ?.usage_metadata,
1068
- },
1069
- };
1070
- }
1071
- else {
1072
- const input = this._convertMessagesToResponsesParams(messages);
1073
- const data = await this.completionWithRetry({
1074
- input,
1075
- ...invocationParams,
1076
- stream: false,
1077
- }, { signal: options?.signal, ...options?.options });
1078
- return {
1079
- generations: [
1080
- {
1081
- text: data.output_text,
1082
- message: this._convertResponsesMessageToBaseMessage(data),
1083
- },
1084
- ],
1085
- llmOutput: {
1086
- id: data.id,
1087
- estimatedTokenUsage: data.usage
1088
- ? {
1089
- promptTokens: data.usage.input_tokens,
1090
- completionTokens: data.usage.output_tokens,
1091
- totalTokens: data.usage.total_tokens,
1092
- }
1093
- : undefined,
1094
- },
1095
- };
1096
- }
1097
- }
1098
- async *_streamResponseChunks(messages, options) {
1099
- const streamIterable = await this.completionWithRetry({
1100
- ...this.invocationParams(options),
1101
- input: this._convertMessagesToResponsesParams(messages),
1102
- stream: true,
1103
- }, options);
1104
- for await (const data of streamIterable) {
1105
- const chunk = this._convertResponsesDeltaToBaseMessageChunk(data);
1106
- if (chunk == null)
1107
- continue;
1108
- yield chunk;
1109
- }
1110
- }
1111
- async completionWithRetry(request, requestOptions) {
1112
- return this.caller.call(async () => {
1113
- const clientOptions = this._getClientOptions(requestOptions);
1114
- try {
1115
- // use parse if dealing with json_schema
1116
- if (request.text?.format?.type === "json_schema" && !request.stream) {
1117
- return await this.client.responses.parse(request, clientOptions);
1118
- }
1119
- return await this.client.responses.create(request, clientOptions);
1120
- }
1121
- catch (e) {
1122
- const error = (0, openai_js_1.wrapOpenAIClientError)(e);
1123
- throw error;
1124
- }
1125
- });
1126
- }
1127
- /** @internal */
1128
- _convertResponsesMessageToBaseMessage(response) {
1129
- if (response.error) {
1130
- // TODO: add support for `addLangChainErrorFields`
1131
- const error = new Error(response.error.message);
1132
- error.name = response.error.code;
1133
- throw error;
1134
- }
1135
- let messageId;
1136
- const content = [];
1137
- const tool_calls = [];
1138
- const invalid_tool_calls = [];
1139
- const response_metadata = {
1140
- model: response.model,
1141
- created_at: response.created_at,
1142
- id: response.id,
1143
- incomplete_details: response.incomplete_details,
1144
- metadata: response.metadata,
1145
- object: response.object,
1146
- status: response.status,
1147
- user: response.user,
1148
- service_tier: response.service_tier,
1149
- // for compatibility with chat completion calls.
1150
- model_name: response.model,
1151
- };
1152
- const additional_kwargs = {};
1153
- for (const item of response.output) {
1154
- if (item.type === "message") {
1155
- messageId = item.id;
1156
- content.push(...item.content.flatMap((part) => {
1157
- if (part.type === "output_text") {
1158
- if ("parsed" in part && part.parsed != null) {
1159
- additional_kwargs.parsed = part.parsed;
1160
- }
1161
- return {
1162
- type: "text",
1163
- text: part.text,
1164
- annotations: part.annotations,
1165
- };
1166
- }
1167
- if (part.type === "refusal") {
1168
- additional_kwargs.refusal = part.refusal;
1169
- return [];
1170
- }
1171
- return part;
1172
- }));
1173
- }
1174
- else if (item.type === "function_call") {
1175
- const fnAdapter = {
1176
- function: { name: item.name, arguments: item.arguments },
1177
- id: item.call_id,
1178
- };
1179
- try {
1180
- tool_calls.push((0, openai_tools_1.parseToolCall)(fnAdapter, { returnId: true }));
1181
- }
1182
- catch (e) {
1183
- let errMessage;
1184
- if (typeof e === "object" &&
1185
- e != null &&
1186
- "message" in e &&
1187
- typeof e.message === "string") {
1188
- errMessage = e.message;
1189
- }
1190
- invalid_tool_calls.push((0, openai_tools_1.makeInvalidToolCall)(fnAdapter, errMessage));
1191
- }
1192
- additional_kwargs[_FUNCTION_CALL_IDS_MAP_KEY] ??= {};
1193
- if (item.id) {
1194
- additional_kwargs[_FUNCTION_CALL_IDS_MAP_KEY][item.call_id] = item.id;
1195
- }
1196
- }
1197
- else if (item.type === "reasoning") {
1198
- additional_kwargs.reasoning = item;
1199
- }
1200
- else {
1201
- additional_kwargs.tool_outputs ??= [];
1202
- additional_kwargs.tool_outputs.push(item);
1203
- }
1204
- }
1205
- return new messages_1.AIMessage({
1206
- id: messageId,
1207
- content,
1208
- tool_calls,
1209
- invalid_tool_calls,
1210
- usage_metadata: response.usage,
1211
- additional_kwargs,
1212
- response_metadata,
1213
- });
1214
- }
1215
- /** @internal */
1216
- _convertResponsesDeltaToBaseMessageChunk(chunk) {
1217
- const content = [];
1218
- let generationInfo = {};
1219
- let usage_metadata;
1220
- const tool_call_chunks = [];
1221
- const response_metadata = {};
1222
- const additional_kwargs = {};
1223
- let id;
1224
- if (chunk.type === "response.output_text.delta") {
1225
- content.push({
1226
- type: "text",
1227
- text: chunk.delta,
1228
- index: chunk.content_index,
1229
- });
1230
- }
1231
- else if (chunk.type === "response.output_text_annotation.added") {
1232
- content.push({
1233
- type: "text",
1234
- text: "",
1235
- annotations: [chunk.annotation],
1236
- index: chunk.content_index,
1237
- });
1238
- }
1239
- else if (chunk.type === "response.output_item.added" &&
1240
- chunk.item.type === "message") {
1241
- id = chunk.item.id;
1242
- }
1243
- else if (chunk.type === "response.output_item.added" &&
1244
- chunk.item.type === "function_call") {
1245
- tool_call_chunks.push({
1246
- type: "tool_call_chunk",
1247
- name: chunk.item.name,
1248
- args: chunk.item.arguments,
1249
- id: chunk.item.call_id,
1250
- index: chunk.output_index,
1251
- });
1252
- additional_kwargs[_FUNCTION_CALL_IDS_MAP_KEY] = {
1253
- [chunk.item.call_id]: chunk.item.id,
1254
- };
1255
- }
1256
- else if (chunk.type === "response.output_item.done" &&
1257
- [
1258
- "web_search_call",
1259
- "file_search_call",
1260
- "computer_call",
1261
- "code_interpreter_call",
1262
- "mcp_call",
1263
- "mcp_list_tools",
1264
- "mcp_approval_request",
1265
- "image_generation_call",
1266
- ].includes(chunk.item.type)) {
1267
- additional_kwargs.tool_outputs = [chunk.item];
1268
- }
1269
- else if (chunk.type === "response.created") {
1270
- response_metadata.id = chunk.response.id;
1271
- response_metadata.model_name = chunk.response.model;
1272
- response_metadata.model = chunk.response.model;
1273
- }
1274
- else if (chunk.type === "response.completed") {
1275
- const msg = this._convertResponsesMessageToBaseMessage(chunk.response);
1276
- usage_metadata = chunk.response.usage;
1277
- if (chunk.response.text?.format?.type === "json_schema") {
1278
- additional_kwargs.parsed ??= JSON.parse(msg.text);
1279
- }
1280
- for (const [key, value] of Object.entries(chunk.response)) {
1281
- if (key !== "id")
1282
- response_metadata[key] = value;
1283
- }
1284
- }
1285
- else if (chunk.type === "response.function_call_arguments.delta") {
1286
- tool_call_chunks.push({
1287
- type: "tool_call_chunk",
1288
- args: chunk.delta,
1289
- index: chunk.output_index,
1290
- });
1291
- }
1292
- else if (chunk.type === "response.web_search_call.completed" ||
1293
- chunk.type === "response.file_search_call.completed") {
1294
- generationInfo = {
1295
- tool_outputs: {
1296
- id: chunk.item_id,
1297
- type: chunk.type.replace("response.", "").replace(".completed", ""),
1298
- status: "completed",
1299
- },
1300
- };
1301
- }
1302
- else if (chunk.type === "response.refusal.done") {
1303
- additional_kwargs.refusal = chunk.refusal;
1304
- }
1305
- else if (chunk.type === "response.output_item.added" &&
1306
- "item" in chunk &&
1307
- chunk.item.type === "reasoning") {
1308
- const summary = chunk
1309
- .item.summary
1310
- ? chunk.item.summary.map((s, index) => ({
1311
- ...s,
1312
- index,
1313
- }))
1314
- : undefined;
1315
- additional_kwargs.reasoning = {
1316
- // We only capture ID in the first chunk or else the concatenated result of all chunks will
1317
- // have an ID field that is repeated once per chunk. There is special handling for the `type`
1318
- // field that prevents this, however.
1319
- id: chunk.item.id,
1320
- type: chunk.item.type,
1321
- ...(summary ? { summary } : {}),
1322
- };
1323
- }
1324
- else if (chunk.type === "response.reasoning_summary_part.added") {
1325
- additional_kwargs.reasoning = {
1326
- type: "reasoning",
1327
- summary: [{ ...chunk.part, index: chunk.summary_index }],
1328
- };
1329
- }
1330
- else if (chunk.type === "response.reasoning_summary_text.delta") {
1331
- additional_kwargs.reasoning = {
1332
- type: "reasoning",
1333
- summary: [
1334
- {
1335
- text: chunk.delta,
1336
- type: "summary_text",
1337
- index: chunk.summary_index,
1338
- },
1339
- ],
1340
- };
1341
- }
1342
- else if (chunk.type === "response.image_generation_call.partial_image") {
1343
- // noop/fixme: retaining partial images in a message chunk means that _all_
1344
- // partial images get kept in history, so we don't do anything here.
1345
- return null;
1346
- }
1347
- else {
1348
- return null;
1349
- }
1350
- return new outputs_1.ChatGenerationChunk({
1351
- // Legacy reasons, `onLLMNewToken` should pulls this out
1352
- text: content.map((part) => part.text).join(""),
1353
- message: new messages_1.AIMessageChunk({
1354
- id,
1355
- content,
1356
- tool_call_chunks,
1357
- usage_metadata,
1358
- additional_kwargs,
1359
- response_metadata,
1360
- }),
1361
- generationInfo,
1362
- });
1363
- }
1364
- /** @internal */
1365
- _convertMessagesToResponsesParams(messages) {
1366
- return messages.flatMap((lcMsg) => {
1367
- const additional_kwargs = lcMsg.additional_kwargs;
1368
- let role = messageToOpenAIRole(lcMsg);
1369
- if (role === "system" && isReasoningModel(this.model))
1370
- role = "developer";
1371
- if (role === "function") {
1372
- throw new Error("Function messages are not supported in Responses API");
1373
- }
1374
- if (role === "tool") {
1375
- const toolMessage = lcMsg;
1376
- // Handle computer call output
1377
- if (additional_kwargs?.type === "computer_call_output") {
1378
- const output = (() => {
1379
- if (typeof toolMessage.content === "string") {
1380
- return {
1381
- type: "computer_screenshot",
1382
- image_url: toolMessage.content,
1383
- };
1384
- }
1385
- if (Array.isArray(toolMessage.content)) {
1386
- const oaiScreenshot = toolMessage.content.find((i) => i.type === "computer_screenshot");
1387
- if (oaiScreenshot)
1388
- return oaiScreenshot;
1389
- const lcImage = toolMessage.content.find((i) => i.type === "image_url");
1390
- if (lcImage) {
1391
- return {
1392
- type: "computer_screenshot",
1393
- image_url: typeof lcImage.image_url === "string"
1394
- ? lcImage.image_url
1395
- : lcImage.image_url.url,
1396
- };
1397
- }
1398
- }
1399
- throw new Error("Invalid computer call output");
1400
- })();
1401
- return {
1402
- type: "computer_call_output",
1403
- output,
1404
- call_id: toolMessage.tool_call_id,
1405
- };
1406
- }
1407
- return {
1408
- type: "function_call_output",
1409
- call_id: toolMessage.tool_call_id,
1410
- id: toolMessage.id?.startsWith("fc_") ? toolMessage.id : undefined,
1411
- output: typeof toolMessage.content !== "string"
1412
- ? JSON.stringify(toolMessage.content)
1413
- : toolMessage.content,
1414
- };
1415
- }
1416
- if (role === "assistant") {
1417
- // if we have the original response items, just reuse them
1418
- if (!this.zdrEnabled &&
1419
- lcMsg.response_metadata.output != null &&
1420
- Array.isArray(lcMsg.response_metadata.output) &&
1421
- lcMsg.response_metadata.output.length > 0 &&
1422
- lcMsg.response_metadata.output.every((item) => "type" in item)) {
1423
- return lcMsg.response_metadata.output;
1424
- }
1425
- // otherwise, try to reconstruct the response from what we have
1426
- const input = [];
1427
- // reasoning items
1428
- if (additional_kwargs?.reasoning && !this.zdrEnabled) {
1429
- const reasoningItem = this._convertReasoningSummary(additional_kwargs.reasoning);
1430
- input.push(reasoningItem);
1431
- }
1432
- // ai content
1433
- let { content } = lcMsg;
1434
- if (additional_kwargs?.refusal) {
1435
- if (typeof content === "string") {
1436
- content = [
1437
- { type: "output_text", text: content, annotations: [] },
1438
- ];
1439
- }
1440
- content = [
1441
- ...content,
1442
- { type: "refusal", refusal: additional_kwargs.refusal },
1443
- ];
1444
- }
1445
- input.push({
1446
- type: "message",
1447
- role: "assistant",
1448
- ...(lcMsg.id && !this.zdrEnabled && lcMsg.id.startsWith("msg_")
1449
- ? { id: lcMsg.id }
1450
- : {}),
1451
- content: typeof content === "string"
1452
- ? content
1453
- : content.flatMap((item) => {
1454
- if (item.type === "text") {
1455
- return {
1456
- type: "output_text",
1457
- text: item.text,
1458
- // @ts-expect-error TODO: add types for `annotations`
1459
- annotations: item.annotations ?? [],
1460
- };
1461
- }
1462
- if (item.type === "output_text" ||
1463
- item.type === "refusal") {
1464
- return item;
1465
- }
1466
- return [];
1467
- }),
1468
- });
1469
- const functionCallIds = additional_kwargs?.[_FUNCTION_CALL_IDS_MAP_KEY];
1470
- if ((0, messages_1.isAIMessage)(lcMsg) && !!lcMsg.tool_calls?.length) {
1471
- input.push(...lcMsg.tool_calls.map((toolCall) => ({
1472
- type: "function_call",
1473
- name: toolCall.name,
1474
- arguments: JSON.stringify(toolCall.args),
1475
- call_id: toolCall.id,
1476
- ...(this.zdrEnabled
1477
- ? { id: functionCallIds?.[toolCall.id] }
1478
- : {}),
1479
- })));
1480
- }
1481
- else if (additional_kwargs?.tool_calls) {
1482
- input.push(...additional_kwargs.tool_calls.map((toolCall) => ({
1483
- type: "function_call",
1484
- name: toolCall.function.name,
1485
- call_id: toolCall.id,
1486
- arguments: toolCall.function.arguments,
1487
- ...(this.zdrEnabled
1488
- ? { id: functionCallIds?.[toolCall.id] }
1489
- : {}),
1490
- })));
1491
- }
1492
- const toolOutputs = lcMsg.response_metadata.output?.length
1493
- ? lcMsg.response_metadata.output
1494
- : additional_kwargs.tool_outputs;
1495
- const fallthroughCallTypes = [
1496
- "computer_call",
1497
- "mcp_call",
1498
- "code_interpreter_call",
1499
- "image_generation_call",
1500
- ];
1501
- if (toolOutputs != null) {
1502
- const castToolOutputs = toolOutputs;
1503
- const fallthroughCalls = castToolOutputs?.filter((item) => fallthroughCallTypes.includes(item.type));
1504
- if (fallthroughCalls.length > 0)
1505
- input.push(...fallthroughCalls);
1506
- }
1507
- return input;
1508
- }
1509
- if (role === "user" || role === "system" || role === "developer") {
1510
- if (typeof lcMsg.content === "string") {
1511
- return { type: "message", role, content: lcMsg.content };
1512
- }
1513
- const messages = [];
1514
- const content = lcMsg.content.flatMap((item) => {
1515
- if (item.type === "mcp_approval_response") {
1516
- messages.push({
1517
- type: "mcp_approval_response",
1518
- approval_request_id: item.approval_request_id,
1519
- approve: item.approve,
1520
- });
1521
- }
1522
- if ((0, messages_1.isDataContentBlock)(item)) {
1523
- return (0, messages_1.convertToProviderContentBlock)(item, completionsApiContentBlockConverter);
1524
- }
1525
- if (item.type === "text") {
1526
- return {
1527
- type: "input_text",
1528
- text: item.text,
1529
- };
1530
- }
1531
- if (item.type === "image_url") {
1532
- return {
1533
- type: "input_image",
1534
- image_url: typeof item.image_url === "string"
1535
- ? item.image_url
1536
- : item.image_url.url,
1537
- detail: typeof item.image_url === "string"
1538
- ? "auto"
1539
- : item.image_url.detail,
1540
- };
1541
- }
1542
- if (item.type === "input_text" ||
1543
- item.type === "input_image" ||
1544
- item.type === "input_file") {
1545
- return item;
1546
- }
1547
- return [];
1548
- });
1549
- if (content.length > 0) {
1550
- messages.push({ type: "message", role, content });
1551
- }
1552
- return messages;
1553
- }
1554
- console.warn(`Unsupported role found when converting to OpenAI Responses API: ${role}`);
1555
- return [];
1556
- });
1557
- }
1558
- /** @internal */
1559
- _convertReasoningSummary(reasoning) {
1560
- // combine summary parts that have the the same index and then remove the indexes
1561
- const summary = (reasoning.summary.length > 1
1562
- ? reasoning.summary.reduce((acc, curr) => {
1563
- const last = acc.at(-1);
1564
- if (last.index === curr.index) {
1565
- last.text += curr.text;
1566
- }
1567
- else {
1568
- acc.push(curr);
1569
- }
1570
- return acc;
1571
- }, [{ ...reasoning.summary[0] }])
1572
- : reasoning.summary).map((s) => Object.fromEntries(Object.entries(s).filter(([k]) => k !== "index")));
1573
- return {
1574
- ...reasoning,
1575
- summary,
1576
- };
1577
- }
1578
- /** @internal */
1579
- _reduceChatOpenAITools(tools, fields) {
1580
- const reducedTools = [];
1581
- for (const tool of tools) {
1582
- if (isBuiltInTool(tool)) {
1583
- if (tool.type === "image_generation" && fields?.stream) {
1584
- // OpenAI sends a 400 error if partial_images is not set and we want to stream.
1585
- // We also set it to 1 since we don't support partial images yet.
1586
- tool.partial_images = 1;
1587
- }
1588
- reducedTools.push(tool);
1589
- }
1590
- else if ((0, base_1.isOpenAITool)(tool)) {
1591
- reducedTools.push({
1592
- type: "function",
1593
- name: tool.function.name,
1594
- parameters: tool.function.parameters,
1595
- description: tool.function.description,
1596
- strict: fields?.strict ?? null,
1597
- });
1598
- }
1599
- }
1600
- return reducedTools;
1601
- }
1602
- }
1603
- exports.ChatOpenAIResponses = ChatOpenAIResponses;
1604
- /**
1605
- * OpenAI Completions API implementation.
1606
- * @internal
1607
- */
1608
- class ChatOpenAICompletions extends BaseChatOpenAI {
1609
- /** @internal */
1610
- invocationParams(options, extra) {
1611
- let strict;
1612
- if (options?.strict !== undefined) {
1613
- strict = options.strict;
1614
- }
1615
- else if (this.supportsStrictToolCalling !== undefined) {
1616
- strict = this.supportsStrictToolCalling;
1617
- }
1618
- let streamOptionsConfig = {};
1619
- if (options?.stream_options !== undefined) {
1620
- streamOptionsConfig = { stream_options: options.stream_options };
1621
- }
1622
- else if (this.streamUsage && (this.streaming || extra?.streaming)) {
1623
- streamOptionsConfig = { stream_options: { include_usage: true } };
1624
- }
1625
- const params = {
1626
- model: this.model,
1627
- temperature: this.temperature,
1628
- top_p: this.topP,
1629
- frequency_penalty: this.frequencyPenalty,
1630
- presence_penalty: this.presencePenalty,
1631
- logprobs: this.logprobs,
1632
- top_logprobs: this.topLogprobs,
1633
- n: this.n,
1634
- logit_bias: this.logitBias,
1635
- stop: options?.stop ?? this.stopSequences,
1636
- user: this.user,
1637
- // if include_usage is set or streamUsage then stream must be set to true.
1638
- stream: this.streaming,
1639
- functions: options?.functions,
1640
- function_call: options?.function_call,
1641
- tools: options?.tools?.length
1642
- ? options.tools.map((tool) => this._convertChatOpenAIToolToCompletionsTool(tool, { strict }))
1643
- : undefined,
1644
- tool_choice: (0, openai_js_1.formatToOpenAIToolChoice)(options?.tool_choice),
1645
- response_format: this._getResponseFormat(options?.response_format),
1646
- seed: options?.seed,
1647
- ...streamOptionsConfig,
1648
- parallel_tool_calls: options?.parallel_tool_calls,
1649
- ...(this.audio || options?.audio
1650
- ? { audio: this.audio || options?.audio }
1651
- : {}),
1652
- ...(this.modalities || options?.modalities
1653
- ? { modalities: this.modalities || options?.modalities }
1654
- : {}),
1655
- ...this.modelKwargs,
1656
- };
1657
- if (options?.prediction !== undefined) {
1658
- params.prediction = options.prediction;
1659
- }
1660
- if (this.service_tier !== undefined) {
1661
- params.service_tier = this.service_tier;
1662
- }
1663
- if (options?.service_tier !== undefined) {
1664
- params.service_tier = options.service_tier;
1665
- }
1666
- const reasoning = this._getReasoningParams(options);
1667
- if (reasoning !== undefined && reasoning.effort !== undefined) {
1668
- params.reasoning_effort = reasoning.effort;
1669
- }
1670
- if (isReasoningModel(params.model)) {
1671
- params.max_completion_tokens =
1672
- this.maxTokens === -1 ? undefined : this.maxTokens;
1673
- }
1674
- else {
1675
- params.max_tokens = this.maxTokens === -1 ? undefined : this.maxTokens;
1676
- }
1677
- return params;
1678
- }
1679
- async _generate(messages, options, runManager) {
1680
- const usageMetadata = {};
1681
- const params = this.invocationParams(options);
1682
- const messagesMapped = _convertMessagesToOpenAIParams(messages, this.model);
1683
- if (params.stream) {
1684
- const stream = this._streamResponseChunks(messages, options, runManager);
1685
- const finalChunks = {};
1686
- for await (const chunk of stream) {
1687
- chunk.message.response_metadata = {
1688
- ...chunk.generationInfo,
1689
- ...chunk.message.response_metadata,
1690
- };
1691
- const index = chunk.generationInfo?.completion ?? 0;
1692
- if (finalChunks[index] === undefined) {
1693
- finalChunks[index] = chunk;
1694
- }
1695
- else {
1696
- finalChunks[index] = finalChunks[index].concat(chunk);
1697
- }
1698
- }
1699
- const generations = Object.entries(finalChunks)
1700
- .sort(([aKey], [bKey]) => parseInt(aKey, 10) - parseInt(bKey, 10))
1701
- .map(([_, value]) => value);
1702
- const { functions, function_call } = this.invocationParams(options);
1703
- // OpenAI does not support token usage report under stream mode,
1704
- // fallback to estimation.
1705
- const promptTokenUsage = await this._getEstimatedTokenCountFromPrompt(messages, functions, function_call);
1706
- const completionTokenUsage = await this._getNumTokensFromGenerations(generations);
1707
- usageMetadata.input_tokens = promptTokenUsage;
1708
- usageMetadata.output_tokens = completionTokenUsage;
1709
- usageMetadata.total_tokens = promptTokenUsage + completionTokenUsage;
1710
- return {
1711
- generations,
1712
- llmOutput: {
1713
- estimatedTokenUsage: {
1714
- promptTokens: usageMetadata.input_tokens,
1715
- completionTokens: usageMetadata.output_tokens,
1716
- totalTokens: usageMetadata.total_tokens,
1717
- },
1718
- },
1719
- };
1720
- }
1721
- else {
1722
- const data = await this.completionWithRetry({
1723
- ...params,
1724
- stream: false,
1725
- messages: messagesMapped,
1726
- }, {
1727
- signal: options?.signal,
1728
- ...options?.options,
1729
- });
1730
- const { completion_tokens: completionTokens, prompt_tokens: promptTokens, total_tokens: totalTokens, prompt_tokens_details: promptTokensDetails, completion_tokens_details: completionTokensDetails, } = data?.usage ?? {};
1731
- if (completionTokens) {
1732
- usageMetadata.output_tokens =
1733
- (usageMetadata.output_tokens ?? 0) + completionTokens;
1734
- }
1735
- if (promptTokens) {
1736
- usageMetadata.input_tokens =
1737
- (usageMetadata.input_tokens ?? 0) + promptTokens;
1738
- }
1739
- if (totalTokens) {
1740
- usageMetadata.total_tokens =
1741
- (usageMetadata.total_tokens ?? 0) + totalTokens;
1742
- }
1743
- if (promptTokensDetails?.audio_tokens !== null ||
1744
- promptTokensDetails?.cached_tokens !== null) {
1745
- usageMetadata.input_token_details = {
1746
- ...(promptTokensDetails?.audio_tokens !== null && {
1747
- audio: promptTokensDetails?.audio_tokens,
1748
- }),
1749
- ...(promptTokensDetails?.cached_tokens !== null && {
1750
- cache_read: promptTokensDetails?.cached_tokens,
1751
- }),
1752
- };
1753
- }
1754
- if (completionTokensDetails?.audio_tokens !== null ||
1755
- completionTokensDetails?.reasoning_tokens !== null) {
1756
- usageMetadata.output_token_details = {
1757
- ...(completionTokensDetails?.audio_tokens !== null && {
1758
- audio: completionTokensDetails?.audio_tokens,
1759
- }),
1760
- ...(completionTokensDetails?.reasoning_tokens !== null && {
1761
- reasoning: completionTokensDetails?.reasoning_tokens,
1762
- }),
1763
- };
1764
- }
1765
- const generations = [];
1766
- for (const part of data?.choices ?? []) {
1767
- const text = part.message?.content ?? "";
1768
- const generation = {
1769
- text,
1770
- message: this._convertCompletionsMessageToBaseMessage(part.message ?? { role: "assistant" }, data),
1771
- };
1772
- generation.generationInfo = {
1773
- ...(part.finish_reason ? { finish_reason: part.finish_reason } : {}),
1774
- ...(part.logprobs ? { logprobs: part.logprobs } : {}),
1775
- };
1776
- if ((0, messages_1.isAIMessage)(generation.message)) {
1777
- generation.message.usage_metadata = usageMetadata;
1778
- }
1779
- // Fields are not serialized unless passed to the constructor
1780
- // Doing this ensures all fields on the message are serialized
1781
- generation.message = new messages_1.AIMessage(Object.fromEntries(Object.entries(generation.message).filter(([key]) => !key.startsWith("lc_"))));
1782
- generations.push(generation);
1783
- }
1784
- return {
1785
- generations,
1786
- llmOutput: {
1787
- tokenUsage: {
1788
- promptTokens: usageMetadata.input_tokens,
1789
- completionTokens: usageMetadata.output_tokens,
1790
- totalTokens: usageMetadata.total_tokens,
1791
- },
1792
- },
1793
- };
1794
- }
1795
- }
1796
- async *_streamResponseChunks(messages, options, runManager) {
1797
- const messagesMapped = _convertMessagesToOpenAIParams(messages, this.model);
1798
- const params = {
1799
- ...this.invocationParams(options, {
1800
- streaming: true,
1801
- }),
1802
- messages: messagesMapped,
1803
- stream: true,
1804
- };
1805
- let defaultRole;
1806
- const streamIterable = await this.completionWithRetry(params, options);
1807
- let usage;
1808
- for await (const data of streamIterable) {
1809
- const choice = data?.choices?.[0];
1810
- if (data.usage) {
1811
- usage = data.usage;
1812
- }
1813
- if (!choice) {
1814
- continue;
1815
- }
1816
- const { delta } = choice;
1817
- if (!delta) {
1818
- continue;
1819
- }
1820
- const chunk = this._convertCompletionsDeltaToBaseMessageChunk(delta, data, defaultRole);
1821
- defaultRole = delta.role ?? defaultRole;
1822
- const newTokenIndices = {
1823
- prompt: options.promptIndex ?? 0,
1824
- completion: choice.index ?? 0,
1825
- };
1826
- if (typeof chunk.content !== "string") {
1827
- console.log("[WARNING]: Received non-string content from OpenAI. This is currently not supported.");
1828
- continue;
1829
- }
1830
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
1831
- const generationInfo = { ...newTokenIndices };
1832
- if (choice.finish_reason != null) {
1833
- generationInfo.finish_reason = choice.finish_reason;
1834
- // Only include system fingerprint in the last chunk for now
1835
- // to avoid concatenation issues
1836
- generationInfo.system_fingerprint = data.system_fingerprint;
1837
- generationInfo.model_name = data.model;
1838
- generationInfo.service_tier = data.service_tier;
1839
- }
1840
- if (this.logprobs) {
1841
- generationInfo.logprobs = choice.logprobs;
1842
- }
1843
- const generationChunk = new outputs_1.ChatGenerationChunk({
1844
- message: chunk,
1845
- text: chunk.content,
1846
- generationInfo,
1847
- });
1848
- yield generationChunk;
1849
- await runManager?.handleLLMNewToken(generationChunk.text ?? "", newTokenIndices, undefined, undefined, undefined, { chunk: generationChunk });
1850
- }
1851
- if (usage) {
1852
- const inputTokenDetails = {
1853
- ...(usage.prompt_tokens_details?.audio_tokens !== null && {
1854
- audio: usage.prompt_tokens_details?.audio_tokens,
1855
- }),
1856
- ...(usage.prompt_tokens_details?.cached_tokens !== null && {
1857
- cache_read: usage.prompt_tokens_details?.cached_tokens,
1858
- }),
1859
- };
1860
- const outputTokenDetails = {
1861
- ...(usage.completion_tokens_details?.audio_tokens !== null && {
1862
- audio: usage.completion_tokens_details?.audio_tokens,
1863
- }),
1864
- ...(usage.completion_tokens_details?.reasoning_tokens !== null && {
1865
- reasoning: usage.completion_tokens_details?.reasoning_tokens,
1866
- }),
1867
- };
1868
- const generationChunk = new outputs_1.ChatGenerationChunk({
1869
- message: new messages_1.AIMessageChunk({
1870
- content: "",
1871
- response_metadata: {
1872
- usage: { ...usage },
1873
- },
1874
- usage_metadata: {
1875
- input_tokens: usage.prompt_tokens,
1876
- output_tokens: usage.completion_tokens,
1877
- total_tokens: usage.total_tokens,
1878
- ...(Object.keys(inputTokenDetails).length > 0 && {
1879
- input_token_details: inputTokenDetails,
1880
- }),
1881
- ...(Object.keys(outputTokenDetails).length > 0 && {
1882
- output_token_details: outputTokenDetails,
1883
- }),
1884
- },
1885
- }),
1886
- text: "",
1887
- });
1888
- yield generationChunk;
1889
- }
1890
- if (options.signal?.aborted) {
1891
- throw new Error("AbortError");
1892
- }
1893
- }
1894
- async completionWithRetry(request, requestOptions) {
1895
- const clientOptions = this._getClientOptions(requestOptions);
1896
- const isParseableFormat = request.response_format && request.response_format.type === "json_schema";
1897
- return this.caller.call(async () => {
1898
- try {
1899
- if (isParseableFormat && !request.stream) {
1900
- return await this.client.chat.completions.parse(request, clientOptions);
1901
- }
1902
- else {
1903
- return await this.client.chat.completions.create(request, clientOptions);
1904
- }
1905
- }
1906
- catch (e) {
1907
- const error = (0, openai_js_1.wrapOpenAIClientError)(e);
1908
- throw error;
1909
- }
1910
- });
1911
- }
1912
- /** @internal */
1913
- _convertCompletionsMessageToBaseMessage(message, rawResponse) {
1914
- const rawToolCalls = message.tool_calls;
1915
- switch (message.role) {
1916
- case "assistant": {
1917
- const toolCalls = [];
1918
- const invalidToolCalls = [];
1919
- for (const rawToolCall of rawToolCalls ?? []) {
1920
- try {
1921
- toolCalls.push((0, openai_tools_1.parseToolCall)(rawToolCall, { returnId: true }));
1922
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
1923
- }
1924
- catch (e) {
1925
- invalidToolCalls.push((0, openai_tools_1.makeInvalidToolCall)(rawToolCall, e.message));
1926
- }
1927
- }
1928
- const additional_kwargs = {
1929
- function_call: message.function_call,
1930
- tool_calls: rawToolCalls,
1931
- };
1932
- if (this.__includeRawResponse !== undefined) {
1933
- additional_kwargs.__raw_response = rawResponse;
1934
- }
1935
- const response_metadata = {
1936
- model_name: rawResponse.model,
1937
- ...(rawResponse.system_fingerprint
1938
- ? {
1939
- usage: { ...rawResponse.usage },
1940
- system_fingerprint: rawResponse.system_fingerprint,
1941
- }
1942
- : {}),
1943
- };
1944
- if (message.audio) {
1945
- additional_kwargs.audio = message.audio;
1946
- }
1947
- return new messages_1.AIMessage({
1948
- content: message.content || "",
1949
- tool_calls: toolCalls,
1950
- invalid_tool_calls: invalidToolCalls,
1951
- additional_kwargs,
1952
- response_metadata,
1953
- id: rawResponse.id,
1954
- });
1955
- }
1956
- default:
1957
- return new messages_1.ChatMessage(message.content || "", message.role ?? "unknown");
1958
- }
1959
- }
1960
- /** @internal */
1961
- _convertCompletionsDeltaToBaseMessageChunk(
1962
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
1963
- delta, rawResponse, defaultRole) {
1964
- const role = delta.role ?? defaultRole;
1965
- const content = delta.content ?? "";
1966
- let additional_kwargs;
1967
- if (delta.function_call) {
1968
- additional_kwargs = {
1969
- function_call: delta.function_call,
1970
- };
1971
- }
1972
- else if (delta.tool_calls) {
1973
- additional_kwargs = {
1974
- tool_calls: delta.tool_calls,
1975
- };
1976
- }
1977
- else {
1978
- additional_kwargs = {};
1979
- }
1980
- if (this.__includeRawResponse) {
1981
- additional_kwargs.__raw_response = rawResponse;
1982
- }
1983
- if (delta.audio) {
1984
- additional_kwargs.audio = {
1985
- ...delta.audio,
1986
- index: rawResponse.choices[0].index,
1987
- };
1988
- }
1989
- const response_metadata = { usage: { ...rawResponse.usage } };
1990
- if (role === "user") {
1991
- return new messages_1.HumanMessageChunk({ content, response_metadata });
1992
- }
1993
- else if (role === "assistant") {
1994
- const toolCallChunks = [];
1995
- if (Array.isArray(delta.tool_calls)) {
1996
- for (const rawToolCall of delta.tool_calls) {
1997
- toolCallChunks.push({
1998
- name: rawToolCall.function?.name,
1999
- args: rawToolCall.function?.arguments,
2000
- id: rawToolCall.id,
2001
- index: rawToolCall.index,
2002
- type: "tool_call_chunk",
2003
- });
2004
- }
2005
- }
2006
- return new messages_1.AIMessageChunk({
2007
- content,
2008
- tool_call_chunks: toolCallChunks,
2009
- additional_kwargs,
2010
- id: rawResponse.id,
2011
- response_metadata,
2012
- });
2013
- }
2014
- else if (role === "system") {
2015
- return new messages_1.SystemMessageChunk({ content, response_metadata });
2016
- }
2017
- else if (role === "developer") {
2018
- return new messages_1.SystemMessageChunk({
2019
- content,
2020
- response_metadata,
2021
- additional_kwargs: {
2022
- __openai_role__: "developer",
2023
- },
2024
- });
2025
- }
2026
- else if (role === "function") {
2027
- return new messages_1.FunctionMessageChunk({
2028
- content,
2029
- additional_kwargs,
2030
- name: delta.name,
2031
- response_metadata,
2032
- });
2033
- }
2034
- else if (role === "tool") {
2035
- return new messages_1.ToolMessageChunk({
2036
- content,
2037
- additional_kwargs,
2038
- tool_call_id: delta.tool_call_id,
2039
- response_metadata,
2040
- });
2041
- }
2042
- else {
2043
- return new messages_1.ChatMessageChunk({ content, role, response_metadata });
2044
748
  }
749
+ return tool;
2045
750
  }
751
+ return (0, tools_js_1._convertToOpenAITool)(tool, fields);
752
+ }
753
+ function isReasoningModel(model) {
754
+ return model && /^o\d/.test(model);
2046
755
  }
2047
- exports.ChatOpenAICompletions = ChatOpenAICompletions;
2048
756
  /**
2049
757
  * OpenAI chat model integration.
2050
758
  *
@@ -2090,7 +798,7 @@ exports.ChatOpenAICompletions = ChatOpenAICompletions;
2090
798
  * import { ChatOpenAI } from '@langchain/openai';
2091
799
  *
2092
800
  * const llm = new ChatOpenAI({
2093
- * model: "gpt-4o-mini",
801
+ * model: "gpt-4o",
2094
802
  * temperature: 0,
2095
803
  * maxTokens: undefined,
2096
804
  * timeout: undefined,
@@ -2391,7 +1099,7 @@ exports.ChatOpenAICompletions = ChatOpenAICompletions;
2391
1099
  * <summary><strong>Logprobs</strong></summary>
2392
1100
  *
2393
1101
  * ```typescript
2394
- * const logprobsLlm = new ChatOpenAI({ model: "gpt-4o-mini", logprobs: true });
1102
+ * const logprobsLlm = new ChatOpenAI({ logprobs: true });
2395
1103
  * const aiMsgForLogprobs = await logprobsLlm.invoke(input);
2396
1104
  * console.log(aiMsgForLogprobs.response_metadata.logprobs);
2397
1105
  * ```
@@ -2583,84 +1291,1437 @@ exports.ChatOpenAICompletions = ChatOpenAICompletions;
2583
1291
  *
2584
1292
  * <br />
2585
1293
  */
2586
- class ChatOpenAI extends BaseChatOpenAI {
1294
+ class ChatOpenAI extends chat_models_1.BaseChatModel {
1295
+ static lc_name() {
1296
+ return "ChatOpenAI";
1297
+ }
1298
+ get callKeys() {
1299
+ return [
1300
+ ...super.callKeys,
1301
+ "options",
1302
+ "function_call",
1303
+ "functions",
1304
+ "tools",
1305
+ "tool_choice",
1306
+ "promptIndex",
1307
+ "response_format",
1308
+ "seed",
1309
+ "reasoning_effort",
1310
+ "service_tier",
1311
+ ];
1312
+ }
1313
+ get lc_secrets() {
1314
+ return {
1315
+ openAIApiKey: "OPENAI_API_KEY",
1316
+ apiKey: "OPENAI_API_KEY",
1317
+ organization: "OPENAI_ORGANIZATION",
1318
+ };
1319
+ }
1320
+ get lc_aliases() {
1321
+ return {
1322
+ modelName: "model",
1323
+ openAIApiKey: "openai_api_key",
1324
+ apiKey: "openai_api_key",
1325
+ };
1326
+ }
2587
1327
  get lc_serializable_keys() {
2588
- return [...super.lc_serializable_keys, "useResponsesApi"];
1328
+ return [
1329
+ "configuration",
1330
+ "logprobs",
1331
+ "topLogprobs",
1332
+ "prefixMessages",
1333
+ "supportsStrictToolCalling",
1334
+ "modalities",
1335
+ "audio",
1336
+ "reasoningEffort",
1337
+ "temperature",
1338
+ "maxTokens",
1339
+ "topP",
1340
+ "frequencyPenalty",
1341
+ "presencePenalty",
1342
+ "n",
1343
+ "logitBias",
1344
+ "user",
1345
+ "streaming",
1346
+ "streamUsage",
1347
+ "modelName",
1348
+ "model",
1349
+ "modelKwargs",
1350
+ "stop",
1351
+ "stopSequences",
1352
+ "timeout",
1353
+ "openAIApiKey",
1354
+ "apiKey",
1355
+ "cache",
1356
+ "maxConcurrency",
1357
+ "maxRetries",
1358
+ "verbose",
1359
+ "callbacks",
1360
+ "tags",
1361
+ "metadata",
1362
+ "disableStreaming",
1363
+ "useResponsesApi",
1364
+ "zdrEnabled",
1365
+ "reasoning",
1366
+ ];
1367
+ }
1368
+ constructor(fields) {
1369
+ super(fields ?? {});
1370
+ Object.defineProperty(this, "lc_serializable", {
1371
+ enumerable: true,
1372
+ configurable: true,
1373
+ writable: true,
1374
+ value: true
1375
+ });
1376
+ Object.defineProperty(this, "temperature", {
1377
+ enumerable: true,
1378
+ configurable: true,
1379
+ writable: true,
1380
+ value: void 0
1381
+ });
1382
+ Object.defineProperty(this, "topP", {
1383
+ enumerable: true,
1384
+ configurable: true,
1385
+ writable: true,
1386
+ value: void 0
1387
+ });
1388
+ Object.defineProperty(this, "frequencyPenalty", {
1389
+ enumerable: true,
1390
+ configurable: true,
1391
+ writable: true,
1392
+ value: void 0
1393
+ });
1394
+ Object.defineProperty(this, "presencePenalty", {
1395
+ enumerable: true,
1396
+ configurable: true,
1397
+ writable: true,
1398
+ value: void 0
1399
+ });
1400
+ Object.defineProperty(this, "n", {
1401
+ enumerable: true,
1402
+ configurable: true,
1403
+ writable: true,
1404
+ value: void 0
1405
+ });
1406
+ Object.defineProperty(this, "logitBias", {
1407
+ enumerable: true,
1408
+ configurable: true,
1409
+ writable: true,
1410
+ value: void 0
1411
+ });
1412
+ /** @deprecated Use "model" instead */
1413
+ Object.defineProperty(this, "modelName", {
1414
+ enumerable: true,
1415
+ configurable: true,
1416
+ writable: true,
1417
+ value: void 0
1418
+ });
1419
+ Object.defineProperty(this, "model", {
1420
+ enumerable: true,
1421
+ configurable: true,
1422
+ writable: true,
1423
+ value: "gpt-3.5-turbo"
1424
+ });
1425
+ Object.defineProperty(this, "modelKwargs", {
1426
+ enumerable: true,
1427
+ configurable: true,
1428
+ writable: true,
1429
+ value: void 0
1430
+ });
1431
+ Object.defineProperty(this, "stop", {
1432
+ enumerable: true,
1433
+ configurable: true,
1434
+ writable: true,
1435
+ value: void 0
1436
+ });
1437
+ Object.defineProperty(this, "stopSequences", {
1438
+ enumerable: true,
1439
+ configurable: true,
1440
+ writable: true,
1441
+ value: void 0
1442
+ });
1443
+ Object.defineProperty(this, "user", {
1444
+ enumerable: true,
1445
+ configurable: true,
1446
+ writable: true,
1447
+ value: void 0
1448
+ });
1449
+ Object.defineProperty(this, "timeout", {
1450
+ enumerable: true,
1451
+ configurable: true,
1452
+ writable: true,
1453
+ value: void 0
1454
+ });
1455
+ Object.defineProperty(this, "streaming", {
1456
+ enumerable: true,
1457
+ configurable: true,
1458
+ writable: true,
1459
+ value: false
1460
+ });
1461
+ Object.defineProperty(this, "streamUsage", {
1462
+ enumerable: true,
1463
+ configurable: true,
1464
+ writable: true,
1465
+ value: true
1466
+ });
1467
+ Object.defineProperty(this, "maxTokens", {
1468
+ enumerable: true,
1469
+ configurable: true,
1470
+ writable: true,
1471
+ value: void 0
1472
+ });
1473
+ Object.defineProperty(this, "logprobs", {
1474
+ enumerable: true,
1475
+ configurable: true,
1476
+ writable: true,
1477
+ value: void 0
1478
+ });
1479
+ Object.defineProperty(this, "topLogprobs", {
1480
+ enumerable: true,
1481
+ configurable: true,
1482
+ writable: true,
1483
+ value: void 0
1484
+ });
1485
+ Object.defineProperty(this, "openAIApiKey", {
1486
+ enumerable: true,
1487
+ configurable: true,
1488
+ writable: true,
1489
+ value: void 0
1490
+ });
1491
+ Object.defineProperty(this, "apiKey", {
1492
+ enumerable: true,
1493
+ configurable: true,
1494
+ writable: true,
1495
+ value: void 0
1496
+ });
1497
+ Object.defineProperty(this, "organization", {
1498
+ enumerable: true,
1499
+ configurable: true,
1500
+ writable: true,
1501
+ value: void 0
1502
+ });
1503
+ Object.defineProperty(this, "__includeRawResponse", {
1504
+ enumerable: true,
1505
+ configurable: true,
1506
+ writable: true,
1507
+ value: void 0
1508
+ });
1509
+ Object.defineProperty(this, "client", {
1510
+ enumerable: true,
1511
+ configurable: true,
1512
+ writable: true,
1513
+ value: void 0
1514
+ });
1515
+ Object.defineProperty(this, "clientConfig", {
1516
+ enumerable: true,
1517
+ configurable: true,
1518
+ writable: true,
1519
+ value: void 0
1520
+ });
1521
+ /**
1522
+ * Whether the model supports the `strict` argument when passing in tools.
1523
+ * If `undefined` the `strict` argument will not be passed to OpenAI.
1524
+ */
1525
+ Object.defineProperty(this, "supportsStrictToolCalling", {
1526
+ enumerable: true,
1527
+ configurable: true,
1528
+ writable: true,
1529
+ value: void 0
1530
+ });
1531
+ Object.defineProperty(this, "audio", {
1532
+ enumerable: true,
1533
+ configurable: true,
1534
+ writable: true,
1535
+ value: void 0
1536
+ });
1537
+ Object.defineProperty(this, "modalities", {
1538
+ enumerable: true,
1539
+ configurable: true,
1540
+ writable: true,
1541
+ value: void 0
1542
+ });
1543
+ /**
1544
+ * @deprecated Use {@link reasoning} object instead.
1545
+ */
1546
+ Object.defineProperty(this, "reasoningEffort", {
1547
+ enumerable: true,
1548
+ configurable: true,
1549
+ writable: true,
1550
+ value: void 0
1551
+ });
1552
+ /**
1553
+ * Options for reasoning models.
1554
+ */
1555
+ Object.defineProperty(this, "reasoning", {
1556
+ enumerable: true,
1557
+ configurable: true,
1558
+ writable: true,
1559
+ value: void 0
1560
+ });
1561
+ /**
1562
+ * Whether to use the responses API for all requests. If `false` the responses API will be used
1563
+ * only when required in order to fulfill the request.
1564
+ */
1565
+ Object.defineProperty(this, "useResponsesApi", {
1566
+ enumerable: true,
1567
+ configurable: true,
1568
+ writable: true,
1569
+ value: false
1570
+ });
1571
+ /**
1572
+ * Must be set to `true` in tenancies with Zero Data Retention. Setting to `true` will disable
1573
+ * output storage in the Responses API, but this DOES NOT enable Zero Data Retention in your
1574
+ * OpenAI organization or project. This must be configured directly with OpenAI.
1575
+ *
1576
+ * See:
1577
+ * https://help.openai.com/en/articles/10503543-data-residency-for-the-openai-api
1578
+ * https://platform.openai.com/docs/api-reference/responses/create#responses-create-store
1579
+ *
1580
+ * @default false
1581
+ */
1582
+ Object.defineProperty(this, "zdrEnabled", {
1583
+ enumerable: true,
1584
+ configurable: true,
1585
+ writable: true,
1586
+ value: void 0
1587
+ });
1588
+ /**
1589
+ * Service tier to use for this request. Can be "auto", "default", or "flex" or "priority".
1590
+ * Specifies the service tier for prioritization and latency optimization.
1591
+ */
1592
+ Object.defineProperty(this, "service_tier", {
1593
+ enumerable: true,
1594
+ configurable: true,
1595
+ writable: true,
1596
+ value: void 0
1597
+ });
1598
+ this.openAIApiKey =
1599
+ fields?.apiKey ??
1600
+ fields?.openAIApiKey ??
1601
+ fields?.configuration?.apiKey ??
1602
+ (0, env_1.getEnvironmentVariable)("OPENAI_API_KEY");
1603
+ this.apiKey = this.openAIApiKey;
1604
+ this.organization =
1605
+ fields?.configuration?.organization ??
1606
+ (0, env_1.getEnvironmentVariable)("OPENAI_ORGANIZATION");
1607
+ this.model = fields?.model ?? fields?.modelName ?? this.model;
1608
+ this.modelName = this.model;
1609
+ this.modelKwargs = fields?.modelKwargs ?? {};
1610
+ this.timeout = fields?.timeout;
1611
+ this.temperature = fields?.temperature ?? this.temperature;
1612
+ this.topP = fields?.topP ?? this.topP;
1613
+ this.frequencyPenalty = fields?.frequencyPenalty ?? this.frequencyPenalty;
1614
+ this.presencePenalty = fields?.presencePenalty ?? this.presencePenalty;
1615
+ this.logprobs = fields?.logprobs;
1616
+ this.topLogprobs = fields?.topLogprobs;
1617
+ this.n = fields?.n ?? this.n;
1618
+ this.logitBias = fields?.logitBias;
1619
+ this.stop = fields?.stopSequences ?? fields?.stop;
1620
+ this.stopSequences = this.stop;
1621
+ this.user = fields?.user;
1622
+ this.__includeRawResponse = fields?.__includeRawResponse;
1623
+ this.audio = fields?.audio;
1624
+ this.modalities = fields?.modalities;
1625
+ this.reasoningEffort = fields?.reasoningEffort ?? fields?.reasoning?.effort;
1626
+ this.reasoning =
1627
+ fields?.reasoning ??
1628
+ (fields?.reasoningEffort
1629
+ ? { effort: fields.reasoningEffort }
1630
+ : undefined);
1631
+ this.maxTokens = fields?.maxCompletionTokens ?? fields?.maxTokens;
1632
+ this.useResponsesApi = fields?.useResponsesApi ?? this.useResponsesApi;
1633
+ this.disableStreaming = fields?.disableStreaming ?? this.disableStreaming;
1634
+ this.streaming = fields?.streaming ?? false;
1635
+ if (this.disableStreaming)
1636
+ this.streaming = false;
1637
+ this.streamUsage = fields?.streamUsage ?? this.streamUsage;
1638
+ if (this.disableStreaming)
1639
+ this.streamUsage = false;
1640
+ this.clientConfig = {
1641
+ apiKey: this.apiKey,
1642
+ organization: this.organization,
1643
+ dangerouslyAllowBrowser: true,
1644
+ ...fields?.configuration,
1645
+ };
1646
+ // If `supportsStrictToolCalling` is explicitly set, use that value.
1647
+ // Else leave undefined so it's not passed to OpenAI.
1648
+ if (fields?.supportsStrictToolCalling !== undefined) {
1649
+ this.supportsStrictToolCalling = fields.supportsStrictToolCalling;
1650
+ }
1651
+ if (fields?.service_tier !== undefined) {
1652
+ this.service_tier = fields.service_tier;
1653
+ }
1654
+ this.zdrEnabled = fields?.zdrEnabled ?? false;
1655
+ }
1656
+ getLsParams(options) {
1657
+ const params = this.invocationParams(options);
1658
+ return {
1659
+ ls_provider: "openai",
1660
+ ls_model_name: this.model,
1661
+ ls_model_type: "chat",
1662
+ ls_temperature: params.temperature ?? undefined,
1663
+ ls_max_tokens: params.max_tokens ?? undefined,
1664
+ ls_stop: options.stop,
1665
+ };
1666
+ }
1667
+ bindTools(tools, kwargs) {
1668
+ let strict;
1669
+ if (kwargs?.strict !== undefined) {
1670
+ strict = kwargs.strict;
1671
+ }
1672
+ else if (this.supportsStrictToolCalling !== undefined) {
1673
+ strict = this.supportsStrictToolCalling;
1674
+ }
1675
+ return this.withConfig({
1676
+ tools: tools.map((tool) => isBuiltInTool(tool)
1677
+ ? tool
1678
+ : _convertChatOpenAIToolTypeToOpenAITool(tool, { strict })),
1679
+ ...kwargs,
1680
+ });
1681
+ }
1682
+ createResponseFormat(resFormat) {
1683
+ if (resFormat &&
1684
+ resFormat.type === "json_schema" &&
1685
+ resFormat.json_schema.schema &&
1686
+ (0, types_1.isInteropZodSchema)(resFormat.json_schema.schema)) {
1687
+ return interopZodResponseFormat(resFormat.json_schema.schema, resFormat.json_schema.name, {
1688
+ description: resFormat.json_schema.description,
1689
+ });
1690
+ }
1691
+ return resFormat;
1692
+ }
1693
+ getReasoningParams(options) {
1694
+ if (!isReasoningModel(this.model)) {
1695
+ return;
1696
+ }
1697
+ let reasoning;
1698
+ // apply reasoning options in reverse order of priority without else blocks so that highest priority overrides lowest priority
1699
+ if (this.reasoningEffort !== undefined) {
1700
+ reasoning = { effort: this.reasoningEffort };
1701
+ }
1702
+ if (this.reasoning !== undefined) {
1703
+ reasoning = {
1704
+ ...reasoning,
1705
+ ...this.reasoning,
1706
+ };
1707
+ }
1708
+ if (options?.reasoning_effort !== undefined) {
1709
+ reasoning = {
1710
+ ...reasoning,
1711
+ effort: options.reasoning_effort,
1712
+ };
1713
+ }
1714
+ if (options?.reasoning !== undefined) {
1715
+ reasoning = {
1716
+ ...reasoning,
1717
+ ...options.reasoning,
1718
+ };
1719
+ }
1720
+ return reasoning;
1721
+ }
1722
+ /**
1723
+ * Get the parameters used to invoke the model
1724
+ */
1725
+ invocationParams(options, extra) {
1726
+ let strict;
1727
+ if (options?.strict !== undefined) {
1728
+ strict = options.strict;
1729
+ }
1730
+ else if (this.supportsStrictToolCalling !== undefined) {
1731
+ strict = this.supportsStrictToolCalling;
1732
+ }
1733
+ if (this._useResponseApi(options)) {
1734
+ const params = {
1735
+ model: this.model,
1736
+ temperature: this.temperature,
1737
+ top_p: this.topP,
1738
+ user: this.user,
1739
+ // if include_usage is set or streamUsage then stream must be set to true.
1740
+ stream: this.streaming,
1741
+ previous_response_id: options?.previous_response_id,
1742
+ truncation: options?.truncation,
1743
+ include: options?.include,
1744
+ tools: options?.tools?.length
1745
+ ? _reduceChatOpenAIToolsForResponses(options.tools, {
1746
+ stream: this.streaming,
1747
+ strict,
1748
+ })
1749
+ : undefined,
1750
+ tool_choice: isBuiltInToolChoice(options?.tool_choice)
1751
+ ? options?.tool_choice
1752
+ : (() => {
1753
+ const formatted = (0, openai_js_1.formatToOpenAIToolChoice)(options?.tool_choice);
1754
+ if (typeof formatted === "object" && "type" in formatted) {
1755
+ return { type: "function", name: formatted.function.name };
1756
+ }
1757
+ else {
1758
+ return undefined;
1759
+ }
1760
+ })(),
1761
+ text: (() => {
1762
+ if (options?.text)
1763
+ return options.text;
1764
+ const format = this.createResponseFormat(options?.response_format);
1765
+ if (format?.type === "json_schema") {
1766
+ if (format.json_schema.schema != null) {
1767
+ return {
1768
+ format: {
1769
+ type: "json_schema",
1770
+ schema: format.json_schema.schema,
1771
+ description: format.json_schema.description,
1772
+ name: format.json_schema.name,
1773
+ strict: format.json_schema.strict,
1774
+ },
1775
+ };
1776
+ }
1777
+ return undefined;
1778
+ }
1779
+ return { format };
1780
+ })(),
1781
+ parallel_tool_calls: options?.parallel_tool_calls,
1782
+ max_output_tokens: this.maxTokens === -1 ? undefined : this.maxTokens,
1783
+ ...(this.zdrEnabled ? { store: false } : {}),
1784
+ ...this.modelKwargs,
1785
+ };
1786
+ const reasoning = this.getReasoningParams(options);
1787
+ if (reasoning !== undefined) {
1788
+ params.reasoning = reasoning;
1789
+ }
1790
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
1791
+ return params;
1792
+ }
1793
+ let streamOptionsConfig = {};
1794
+ if (options?.stream_options !== undefined) {
1795
+ streamOptionsConfig = { stream_options: options.stream_options };
1796
+ }
1797
+ else if (this.streamUsage && (this.streaming || extra?.streaming)) {
1798
+ streamOptionsConfig = { stream_options: { include_usage: true } };
1799
+ }
1800
+ const params = {
1801
+ model: this.model,
1802
+ temperature: this.temperature,
1803
+ top_p: this.topP,
1804
+ frequency_penalty: this.frequencyPenalty,
1805
+ presence_penalty: this.presencePenalty,
1806
+ logprobs: this.logprobs,
1807
+ top_logprobs: this.topLogprobs,
1808
+ n: this.n,
1809
+ logit_bias: this.logitBias,
1810
+ stop: options?.stop ?? this.stopSequences,
1811
+ user: this.user,
1812
+ // if include_usage is set or streamUsage then stream must be set to true.
1813
+ stream: this.streaming,
1814
+ functions: options?.functions,
1815
+ function_call: options?.function_call,
1816
+ tools: options?.tools?.length
1817
+ ? options.tools.map((tool) => _convertChatOpenAIToolTypeToOpenAITool(tool, { strict }))
1818
+ : undefined,
1819
+ tool_choice: (0, openai_js_1.formatToOpenAIToolChoice)(options?.tool_choice),
1820
+ response_format: this.createResponseFormat(options?.response_format),
1821
+ seed: options?.seed,
1822
+ ...streamOptionsConfig,
1823
+ parallel_tool_calls: options?.parallel_tool_calls,
1824
+ ...(this.audio || options?.audio
1825
+ ? { audio: this.audio || options?.audio }
1826
+ : {}),
1827
+ ...(this.modalities || options?.modalities
1828
+ ? { modalities: this.modalities || options?.modalities }
1829
+ : {}),
1830
+ ...this.modelKwargs,
1831
+ };
1832
+ if (options?.prediction !== undefined) {
1833
+ params.prediction = options.prediction;
1834
+ }
1835
+ if (this.service_tier !== undefined) {
1836
+ params.service_tier = this.service_tier;
1837
+ }
1838
+ if (options?.service_tier !== undefined) {
1839
+ params.service_tier = options.service_tier;
1840
+ }
1841
+ const reasoning = this.getReasoningParams(options);
1842
+ if (reasoning !== undefined && reasoning.effort !== undefined) {
1843
+ params.reasoning_effort = reasoning.effort;
1844
+ }
1845
+ if (isReasoningModel(params.model)) {
1846
+ params.max_completion_tokens =
1847
+ this.maxTokens === -1 ? undefined : this.maxTokens;
1848
+ }
1849
+ else {
1850
+ params.max_tokens = this.maxTokens === -1 ? undefined : this.maxTokens;
1851
+ }
1852
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
1853
+ return params;
1854
+ }
1855
+ _convertOpenAIChatCompletionMessageToBaseMessage(message, rawResponse) {
1856
+ const rawToolCalls = message.tool_calls;
1857
+ switch (message.role) {
1858
+ case "assistant": {
1859
+ const toolCalls = [];
1860
+ const invalidToolCalls = [];
1861
+ for (const rawToolCall of rawToolCalls ?? []) {
1862
+ try {
1863
+ toolCalls.push((0, openai_tools_1.parseToolCall)(rawToolCall, { returnId: true }));
1864
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
1865
+ }
1866
+ catch (e) {
1867
+ invalidToolCalls.push((0, openai_tools_1.makeInvalidToolCall)(rawToolCall, e.message));
1868
+ }
1869
+ }
1870
+ const additional_kwargs = {
1871
+ function_call: message.function_call,
1872
+ tool_calls: rawToolCalls,
1873
+ };
1874
+ if (this.__includeRawResponse !== undefined) {
1875
+ additional_kwargs.__raw_response = rawResponse;
1876
+ }
1877
+ const response_metadata = {
1878
+ model_name: rawResponse.model,
1879
+ ...(rawResponse.system_fingerprint
1880
+ ? {
1881
+ usage: { ...rawResponse.usage },
1882
+ system_fingerprint: rawResponse.system_fingerprint,
1883
+ }
1884
+ : {}),
1885
+ };
1886
+ if (message.audio) {
1887
+ additional_kwargs.audio = message.audio;
1888
+ }
1889
+ return new messages_1.AIMessage({
1890
+ content: message.content || "",
1891
+ tool_calls: toolCalls,
1892
+ invalid_tool_calls: invalidToolCalls,
1893
+ additional_kwargs,
1894
+ response_metadata,
1895
+ id: rawResponse.id,
1896
+ });
1897
+ }
1898
+ default:
1899
+ return new messages_1.ChatMessage(message.content || "", message.role ?? "unknown");
1900
+ }
1901
+ }
1902
+ _convertOpenAIDeltaToBaseMessageChunk(
1903
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
1904
+ delta, rawResponse, defaultRole) {
1905
+ const role = delta.role ?? defaultRole;
1906
+ const content = delta.content ?? "";
1907
+ let additional_kwargs;
1908
+ if (delta.function_call) {
1909
+ additional_kwargs = {
1910
+ function_call: delta.function_call,
1911
+ };
1912
+ }
1913
+ else if (delta.tool_calls) {
1914
+ additional_kwargs = {
1915
+ tool_calls: delta.tool_calls,
1916
+ };
1917
+ }
1918
+ else {
1919
+ additional_kwargs = {};
1920
+ }
1921
+ if (this.__includeRawResponse) {
1922
+ additional_kwargs.__raw_response = rawResponse;
1923
+ }
1924
+ if (delta.audio) {
1925
+ additional_kwargs.audio = {
1926
+ ...delta.audio,
1927
+ index: rawResponse.choices[0].index,
1928
+ };
1929
+ }
1930
+ const response_metadata = { usage: { ...rawResponse.usage } };
1931
+ if (role === "user") {
1932
+ return new messages_1.HumanMessageChunk({ content, response_metadata });
1933
+ }
1934
+ else if (role === "assistant") {
1935
+ const toolCallChunks = [];
1936
+ if (Array.isArray(delta.tool_calls)) {
1937
+ for (const rawToolCall of delta.tool_calls) {
1938
+ toolCallChunks.push({
1939
+ name: rawToolCall.function?.name,
1940
+ args: rawToolCall.function?.arguments,
1941
+ id: rawToolCall.id,
1942
+ index: rawToolCall.index,
1943
+ type: "tool_call_chunk",
1944
+ });
1945
+ }
1946
+ }
1947
+ return new messages_1.AIMessageChunk({
1948
+ content,
1949
+ tool_call_chunks: toolCallChunks,
1950
+ additional_kwargs,
1951
+ id: rawResponse.id,
1952
+ response_metadata,
1953
+ });
1954
+ }
1955
+ else if (role === "system") {
1956
+ return new messages_1.SystemMessageChunk({ content, response_metadata });
1957
+ }
1958
+ else if (role === "developer") {
1959
+ return new messages_1.SystemMessageChunk({
1960
+ content,
1961
+ response_metadata,
1962
+ additional_kwargs: {
1963
+ __openai_role__: "developer",
1964
+ },
1965
+ });
1966
+ }
1967
+ else if (role === "function") {
1968
+ return new messages_1.FunctionMessageChunk({
1969
+ content,
1970
+ additional_kwargs,
1971
+ name: delta.name,
1972
+ response_metadata,
1973
+ });
1974
+ }
1975
+ else if (role === "tool") {
1976
+ return new messages_1.ToolMessageChunk({
1977
+ content,
1978
+ additional_kwargs,
1979
+ tool_call_id: delta.tool_call_id,
1980
+ response_metadata,
1981
+ });
1982
+ }
1983
+ else {
1984
+ return new messages_1.ChatMessageChunk({ content, role, response_metadata });
1985
+ }
1986
+ }
1987
+ /** @ignore */
1988
+ _identifyingParams() {
1989
+ return {
1990
+ model_name: this.model,
1991
+ ...this.invocationParams(),
1992
+ ...this.clientConfig,
1993
+ };
1994
+ }
1995
+ async *_streamResponseChunks(messages, options, runManager) {
1996
+ if (this._useResponseApi(options)) {
1997
+ const streamIterable = await this.responseApiWithRetry({
1998
+ ...this.invocationParams(options, { streaming: true }),
1999
+ input: _convertMessagesToOpenAIResponsesParams(messages, this.model, this.zdrEnabled),
2000
+ stream: true,
2001
+ }, options);
2002
+ for await (const data of streamIterable) {
2003
+ const chunk = _convertOpenAIResponsesDeltaToBaseMessageChunk(data);
2004
+ if (chunk == null)
2005
+ continue;
2006
+ yield chunk;
2007
+ }
2008
+ return;
2009
+ }
2010
+ const messagesMapped = _convertMessagesToOpenAIParams(messages, this.model);
2011
+ const params = {
2012
+ ...this.invocationParams(options, {
2013
+ streaming: true,
2014
+ }),
2015
+ messages: messagesMapped,
2016
+ stream: true,
2017
+ };
2018
+ let defaultRole;
2019
+ const streamIterable = await this.completionWithRetry(params, options);
2020
+ let usage;
2021
+ for await (const data of streamIterable) {
2022
+ const choice = data?.choices?.[0];
2023
+ if (data.usage) {
2024
+ usage = data.usage;
2025
+ }
2026
+ if (!choice) {
2027
+ continue;
2028
+ }
2029
+ const { delta } = choice;
2030
+ if (!delta) {
2031
+ continue;
2032
+ }
2033
+ const chunk = this._convertOpenAIDeltaToBaseMessageChunk(delta, data, defaultRole);
2034
+ defaultRole = delta.role ?? defaultRole;
2035
+ const newTokenIndices = {
2036
+ prompt: options.promptIndex ?? 0,
2037
+ completion: choice.index ?? 0,
2038
+ };
2039
+ if (typeof chunk.content !== "string") {
2040
+ console.log("[WARNING]: Received non-string content from OpenAI. This is currently not supported.");
2041
+ continue;
2042
+ }
2043
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
2044
+ const generationInfo = { ...newTokenIndices };
2045
+ if (choice.finish_reason != null) {
2046
+ generationInfo.finish_reason = choice.finish_reason;
2047
+ // Only include system fingerprint in the last chunk for now
2048
+ // to avoid concatenation issues
2049
+ generationInfo.system_fingerprint = data.system_fingerprint;
2050
+ generationInfo.model_name = data.model;
2051
+ generationInfo.service_tier = data.service_tier;
2052
+ }
2053
+ if (this.logprobs) {
2054
+ generationInfo.logprobs = choice.logprobs;
2055
+ }
2056
+ const generationChunk = new outputs_1.ChatGenerationChunk({
2057
+ message: chunk,
2058
+ text: chunk.content,
2059
+ generationInfo,
2060
+ });
2061
+ yield generationChunk;
2062
+ await runManager?.handleLLMNewToken(generationChunk.text ?? "", newTokenIndices, undefined, undefined, undefined, { chunk: generationChunk });
2063
+ }
2064
+ if (usage) {
2065
+ const inputTokenDetails = {
2066
+ ...(usage.prompt_tokens_details?.audio_tokens !== null && {
2067
+ audio: usage.prompt_tokens_details?.audio_tokens,
2068
+ }),
2069
+ ...(usage.prompt_tokens_details?.cached_tokens !== null && {
2070
+ cache_read: usage.prompt_tokens_details?.cached_tokens,
2071
+ }),
2072
+ };
2073
+ const outputTokenDetails = {
2074
+ ...(usage.completion_tokens_details?.audio_tokens !== null && {
2075
+ audio: usage.completion_tokens_details?.audio_tokens,
2076
+ }),
2077
+ ...(usage.completion_tokens_details?.reasoning_tokens !== null && {
2078
+ reasoning: usage.completion_tokens_details?.reasoning_tokens,
2079
+ }),
2080
+ };
2081
+ const generationChunk = new outputs_1.ChatGenerationChunk({
2082
+ message: new messages_1.AIMessageChunk({
2083
+ content: "",
2084
+ response_metadata: {
2085
+ usage: { ...usage },
2086
+ },
2087
+ usage_metadata: {
2088
+ input_tokens: usage.prompt_tokens,
2089
+ output_tokens: usage.completion_tokens,
2090
+ total_tokens: usage.total_tokens,
2091
+ ...(Object.keys(inputTokenDetails).length > 0 && {
2092
+ input_token_details: inputTokenDetails,
2093
+ }),
2094
+ ...(Object.keys(outputTokenDetails).length > 0 && {
2095
+ output_token_details: outputTokenDetails,
2096
+ }),
2097
+ },
2098
+ }),
2099
+ text: "",
2100
+ });
2101
+ yield generationChunk;
2102
+ }
2103
+ if (options.signal?.aborted) {
2104
+ throw new Error("AbortError");
2105
+ }
2106
+ }
2107
+ /**
2108
+ * Get the identifying parameters for the model
2109
+ *
2110
+ */
2111
+ identifyingParams() {
2112
+ return this._identifyingParams();
2113
+ }
2114
+ /** @ignore */
2115
+ async _responseApiGenerate(messages, options, runManager) {
2116
+ const invocationParams = this.invocationParams(options);
2117
+ if (invocationParams.stream) {
2118
+ const stream = this._streamResponseChunks(messages, options, runManager);
2119
+ let finalChunk;
2120
+ for await (const chunk of stream) {
2121
+ chunk.message.response_metadata = {
2122
+ ...chunk.generationInfo,
2123
+ ...chunk.message.response_metadata,
2124
+ };
2125
+ finalChunk = finalChunk?.concat(chunk) ?? chunk;
2126
+ }
2127
+ return {
2128
+ generations: finalChunk ? [finalChunk] : [],
2129
+ llmOutput: {
2130
+ estimatedTokenUsage: finalChunk?.message
2131
+ ?.usage_metadata,
2132
+ },
2133
+ };
2134
+ }
2135
+ const input = _convertMessagesToOpenAIResponsesParams(messages, this.model, this.zdrEnabled);
2136
+ const data = await this.responseApiWithRetry({
2137
+ input,
2138
+ ...invocationParams,
2139
+ }, { signal: options?.signal, ...options?.options });
2140
+ return {
2141
+ generations: [
2142
+ {
2143
+ text: data.output_text,
2144
+ message: _convertOpenAIResponsesMessageToBaseMessage(data),
2145
+ },
2146
+ ],
2147
+ llmOutput: {
2148
+ id: data.id,
2149
+ estimatedTokenUsage: data.usage
2150
+ ? {
2151
+ promptTokens: data.usage.input_tokens,
2152
+ completionTokens: data.usage.output_tokens,
2153
+ totalTokens: data.usage.total_tokens,
2154
+ }
2155
+ : undefined,
2156
+ },
2157
+ };
2589
2158
  }
2590
- constructor(fields) {
2591
- super(fields ?? {});
2592
- /**
2593
- * Whether to use the responses API for all requests. If `false` the responses API will be used
2594
- * only when required in order to fulfill the request.
2595
- */
2596
- Object.defineProperty(this, "useResponsesApi", {
2597
- enumerable: true,
2598
- configurable: true,
2599
- writable: true,
2600
- value: false
2159
+ /**
2160
+ * Determines whether the responses API should be used for the given request.
2161
+ *
2162
+ * @internal
2163
+ *
2164
+ * @param options The parsed call options for the request.
2165
+ * @returns `true` if the responses API should be used, either because it is explicitly enabled,
2166
+ * or because the request requires it.
2167
+ */
2168
+ _useResponseApi(options) {
2169
+ const usesBuiltInTools = options?.tools?.some(isBuiltInTool);
2170
+ const hasResponsesOnlyKwargs = options?.previous_response_id != null ||
2171
+ options?.text != null ||
2172
+ options?.truncation != null ||
2173
+ options?.include != null ||
2174
+ options?.reasoning?.summary != null ||
2175
+ this.reasoning?.summary != null;
2176
+ return this.useResponsesApi || usesBuiltInTools || hasResponsesOnlyKwargs;
2177
+ }
2178
+ /** @ignore */
2179
+ async _generate(messages, options, runManager) {
2180
+ if (this._useResponseApi(options)) {
2181
+ return this._responseApiGenerate(messages, options, runManager);
2182
+ }
2183
+ const usageMetadata = {};
2184
+ const params = this.invocationParams(options);
2185
+ const messagesMapped = _convertMessagesToOpenAIParams(messages, this.model);
2186
+ if (params.stream) {
2187
+ const stream = this._streamResponseChunks(messages, options, runManager);
2188
+ const finalChunks = {};
2189
+ for await (const chunk of stream) {
2190
+ chunk.message.response_metadata = {
2191
+ ...chunk.generationInfo,
2192
+ ...chunk.message.response_metadata,
2193
+ };
2194
+ const index = chunk.generationInfo?.completion ?? 0;
2195
+ if (finalChunks[index] === undefined) {
2196
+ finalChunks[index] = chunk;
2197
+ }
2198
+ else {
2199
+ finalChunks[index] = finalChunks[index].concat(chunk);
2200
+ }
2201
+ }
2202
+ const generations = Object.entries(finalChunks)
2203
+ .sort(([aKey], [bKey]) => parseInt(aKey, 10) - parseInt(bKey, 10))
2204
+ .map(([_, value]) => value);
2205
+ const { functions, function_call } = this.invocationParams(options);
2206
+ // OpenAI does not support token usage report under stream mode,
2207
+ // fallback to estimation.
2208
+ const promptTokenUsage = await this.getEstimatedTokenCountFromPrompt(messages, functions, function_call);
2209
+ const completionTokenUsage = await this.getNumTokensFromGenerations(generations);
2210
+ usageMetadata.input_tokens = promptTokenUsage;
2211
+ usageMetadata.output_tokens = completionTokenUsage;
2212
+ usageMetadata.total_tokens = promptTokenUsage + completionTokenUsage;
2213
+ return {
2214
+ generations,
2215
+ llmOutput: {
2216
+ estimatedTokenUsage: {
2217
+ promptTokens: usageMetadata.input_tokens,
2218
+ completionTokens: usageMetadata.output_tokens,
2219
+ totalTokens: usageMetadata.total_tokens,
2220
+ },
2221
+ },
2222
+ };
2223
+ }
2224
+ else {
2225
+ let data;
2226
+ if (options.response_format &&
2227
+ options.response_format.type === "json_schema") {
2228
+ data = await this.betaParsedCompletionWithRetry({
2229
+ ...params,
2230
+ stream: false,
2231
+ messages: messagesMapped,
2232
+ }, {
2233
+ signal: options?.signal,
2234
+ ...options?.options,
2235
+ });
2236
+ }
2237
+ else {
2238
+ data = await this.completionWithRetry({
2239
+ ...params,
2240
+ stream: false,
2241
+ messages: messagesMapped,
2242
+ }, {
2243
+ signal: options?.signal,
2244
+ ...options?.options,
2245
+ });
2246
+ }
2247
+ const { completion_tokens: completionTokens, prompt_tokens: promptTokens, total_tokens: totalTokens, prompt_tokens_details: promptTokensDetails, completion_tokens_details: completionTokensDetails, } = data?.usage ?? {};
2248
+ if (completionTokens) {
2249
+ usageMetadata.output_tokens =
2250
+ (usageMetadata.output_tokens ?? 0) + completionTokens;
2251
+ }
2252
+ if (promptTokens) {
2253
+ usageMetadata.input_tokens =
2254
+ (usageMetadata.input_tokens ?? 0) + promptTokens;
2255
+ }
2256
+ if (totalTokens) {
2257
+ usageMetadata.total_tokens =
2258
+ (usageMetadata.total_tokens ?? 0) + totalTokens;
2259
+ }
2260
+ if (promptTokensDetails?.audio_tokens !== null ||
2261
+ promptTokensDetails?.cached_tokens !== null) {
2262
+ usageMetadata.input_token_details = {
2263
+ ...(promptTokensDetails?.audio_tokens !== null && {
2264
+ audio: promptTokensDetails?.audio_tokens,
2265
+ }),
2266
+ ...(promptTokensDetails?.cached_tokens !== null && {
2267
+ cache_read: promptTokensDetails?.cached_tokens,
2268
+ }),
2269
+ };
2270
+ }
2271
+ if (completionTokensDetails?.audio_tokens !== null ||
2272
+ completionTokensDetails?.reasoning_tokens !== null) {
2273
+ usageMetadata.output_token_details = {
2274
+ ...(completionTokensDetails?.audio_tokens !== null && {
2275
+ audio: completionTokensDetails?.audio_tokens,
2276
+ }),
2277
+ ...(completionTokensDetails?.reasoning_tokens !== null && {
2278
+ reasoning: completionTokensDetails?.reasoning_tokens,
2279
+ }),
2280
+ };
2281
+ }
2282
+ const generations = [];
2283
+ for (const part of data?.choices ?? []) {
2284
+ const text = part.message?.content ?? "";
2285
+ const generation = {
2286
+ text,
2287
+ message: this._convertOpenAIChatCompletionMessageToBaseMessage(part.message ?? { role: "assistant" }, data),
2288
+ };
2289
+ generation.generationInfo = {
2290
+ ...(part.finish_reason ? { finish_reason: part.finish_reason } : {}),
2291
+ ...(part.logprobs ? { logprobs: part.logprobs } : {}),
2292
+ };
2293
+ if ((0, messages_1.isAIMessage)(generation.message)) {
2294
+ generation.message.usage_metadata = usageMetadata;
2295
+ }
2296
+ // Fields are not serialized unless passed to the constructor
2297
+ // Doing this ensures all fields on the message are serialized
2298
+ generation.message = new messages_1.AIMessage(Object.fromEntries(Object.entries(generation.message).filter(([key]) => !key.startsWith("lc_"))));
2299
+ generations.push(generation);
2300
+ }
2301
+ return {
2302
+ generations,
2303
+ llmOutput: {
2304
+ tokenUsage: {
2305
+ promptTokens: usageMetadata.input_tokens,
2306
+ completionTokens: usageMetadata.output_tokens,
2307
+ totalTokens: usageMetadata.total_tokens,
2308
+ },
2309
+ },
2310
+ };
2311
+ }
2312
+ }
2313
+ /**
2314
+ * Estimate the number of tokens a prompt will use.
2315
+ * Modified from: https://github.com/hmarr/openai-chat-tokens/blob/main/src/index.ts
2316
+ */
2317
+ async getEstimatedTokenCountFromPrompt(messages, functions, function_call) {
2318
+ // It appears that if functions are present, the first system message is padded with a trailing newline. This
2319
+ // was inferred by trying lots of combinations of messages and functions and seeing what the token counts were.
2320
+ let tokens = (await this.getNumTokensFromMessages(messages)).totalCount;
2321
+ // If there are functions, add the function definitions as they count towards token usage
2322
+ if (functions && function_call !== "auto") {
2323
+ const promptDefinitions = (0, openai_format_fndef_js_1.formatFunctionDefinitions)(functions);
2324
+ tokens += await this.getNumTokens(promptDefinitions);
2325
+ tokens += 9; // Add nine per completion
2326
+ }
2327
+ // If there's a system message _and_ functions are present, subtract four tokens. I assume this is because
2328
+ // functions typically add a system message, but reuse the first one if it's already there. This offsets
2329
+ // the extra 9 tokens added by the function definitions.
2330
+ if (functions && messages.find((m) => m._getType() === "system")) {
2331
+ tokens -= 4;
2332
+ }
2333
+ // If function_call is 'none', add one token.
2334
+ // If it's a FunctionCall object, add 4 + the number of tokens in the function name.
2335
+ // If it's undefined or 'auto', don't add anything.
2336
+ if (function_call === "none") {
2337
+ tokens += 1;
2338
+ }
2339
+ else if (typeof function_call === "object") {
2340
+ tokens += (await this.getNumTokens(function_call.name)) + 4;
2341
+ }
2342
+ return tokens;
2343
+ }
2344
+ /**
2345
+ * Estimate the number of tokens an array of generations have used.
2346
+ */
2347
+ async getNumTokensFromGenerations(generations) {
2348
+ const generationUsages = await Promise.all(generations.map(async (generation) => {
2349
+ if (generation.message.additional_kwargs?.function_call) {
2350
+ return (await this.getNumTokensFromMessages([generation.message]))
2351
+ .countPerMessage[0];
2352
+ }
2353
+ else {
2354
+ return await this.getNumTokens(generation.message.content);
2355
+ }
2356
+ }));
2357
+ return generationUsages.reduce((a, b) => a + b, 0);
2358
+ }
2359
+ async getNumTokensFromMessages(messages) {
2360
+ let totalCount = 0;
2361
+ let tokensPerMessage = 0;
2362
+ let tokensPerName = 0;
2363
+ // From: https://github.com/openai/openai-cookbook/blob/main/examples/How_to_format_inputs_to_ChatGPT_models.ipynb
2364
+ if (this.model === "gpt-3.5-turbo-0301") {
2365
+ tokensPerMessage = 4;
2366
+ tokensPerName = -1;
2367
+ }
2368
+ else {
2369
+ tokensPerMessage = 3;
2370
+ tokensPerName = 1;
2371
+ }
2372
+ const countPerMessage = await Promise.all(messages.map(async (message) => {
2373
+ const textCount = await this.getNumTokens(message.content);
2374
+ const roleCount = await this.getNumTokens(messageToOpenAIRole(message));
2375
+ const nameCount = message.name !== undefined
2376
+ ? tokensPerName + (await this.getNumTokens(message.name))
2377
+ : 0;
2378
+ let count = textCount + tokensPerMessage + roleCount + nameCount;
2379
+ // From: https://github.com/hmarr/openai-chat-tokens/blob/main/src/index.ts messageTokenEstimate
2380
+ const openAIMessage = message;
2381
+ if (openAIMessage._getType() === "function") {
2382
+ count -= 2;
2383
+ }
2384
+ if (openAIMessage.additional_kwargs?.function_call) {
2385
+ count += 3;
2386
+ }
2387
+ if (openAIMessage?.additional_kwargs.function_call?.name) {
2388
+ count += await this.getNumTokens(openAIMessage.additional_kwargs.function_call?.name);
2389
+ }
2390
+ if (openAIMessage.additional_kwargs.function_call?.arguments) {
2391
+ try {
2392
+ count += await this.getNumTokens(
2393
+ // Remove newlines and spaces
2394
+ JSON.stringify(JSON.parse(openAIMessage.additional_kwargs.function_call?.arguments)));
2395
+ }
2396
+ catch (error) {
2397
+ console.error("Error parsing function arguments", error, JSON.stringify(openAIMessage.additional_kwargs.function_call));
2398
+ count += await this.getNumTokens(openAIMessage.additional_kwargs.function_call?.arguments);
2399
+ }
2400
+ }
2401
+ totalCount += count;
2402
+ return count;
2403
+ }));
2404
+ totalCount += 3; // every reply is primed with <|start|>assistant<|message|>
2405
+ return { totalCount, countPerMessage };
2406
+ }
2407
+ async completionWithRetry(request, options) {
2408
+ const requestOptions = this._getClientOptions(options);
2409
+ return this.caller.call(async () => {
2410
+ try {
2411
+ const res = await this.client.chat.completions.create(request, requestOptions);
2412
+ return res;
2413
+ }
2414
+ catch (e) {
2415
+ const error = (0, openai_js_1.wrapOpenAIClientError)(e);
2416
+ throw error;
2417
+ }
2601
2418
  });
2602
- Object.defineProperty(this, "responses", {
2603
- enumerable: true,
2604
- configurable: true,
2605
- writable: true,
2606
- value: void 0
2419
+ }
2420
+ async responseApiWithRetry(request, options) {
2421
+ return this.caller.call(async () => {
2422
+ const requestOptions = this._getClientOptions(options);
2423
+ try {
2424
+ // use parse if dealing with json_schema
2425
+ if (request.text?.format?.type === "json_schema" && !request.stream) {
2426
+ return await this.client.responses.parse(request, requestOptions);
2427
+ }
2428
+ return await this.client.responses.create(request, requestOptions);
2429
+ }
2430
+ catch (e) {
2431
+ const error = (0, openai_js_1.wrapOpenAIClientError)(e);
2432
+ throw error;
2433
+ }
2607
2434
  });
2608
- Object.defineProperty(this, "completions", {
2609
- enumerable: true,
2610
- configurable: true,
2611
- writable: true,
2612
- value: void 0
2435
+ }
2436
+ /**
2437
+ * Call the beta chat completions parse endpoint. This should only be called if
2438
+ * response_format is set to "json_object".
2439
+ * @param {OpenAIClient.Chat.ChatCompletionCreateParamsNonStreaming} request
2440
+ * @param {OpenAICoreRequestOptions | undefined} options
2441
+ */
2442
+ async betaParsedCompletionWithRetry(request, options
2443
+ // Avoid relying importing a beta type with no official entrypoint
2444
+ ) {
2445
+ const requestOptions = this._getClientOptions(options);
2446
+ return this.caller.call(async () => {
2447
+ try {
2448
+ const res = await this.client.chat.completions.parse(request, requestOptions);
2449
+ return res;
2450
+ }
2451
+ catch (e) {
2452
+ const error = (0, openai_js_1.wrapOpenAIClientError)(e);
2453
+ throw error;
2454
+ }
2613
2455
  });
2614
- this.useResponsesApi = fields?.useResponsesApi ?? false;
2615
- this.responses = new ChatOpenAIResponses(fields);
2616
- this.completions = new ChatOpenAICompletions(fields);
2617
2456
  }
2618
- _useResponsesApi(options) {
2619
- const usesBuiltInTools = options?.tools?.some(isBuiltInTool);
2620
- const hasResponsesOnlyKwargs = options?.previous_response_id != null ||
2621
- options?.text != null ||
2622
- options?.truncation != null ||
2623
- options?.include != null ||
2624
- options?.reasoning?.summary != null ||
2625
- this.reasoning?.summary != null;
2626
- return this.useResponsesApi || usesBuiltInTools || hasResponsesOnlyKwargs;
2457
+ _getClientOptions(options) {
2458
+ if (!this.client) {
2459
+ const openAIEndpointConfig = {
2460
+ baseURL: this.clientConfig.baseURL,
2461
+ };
2462
+ const endpoint = (0, azure_js_1.getEndpoint)(openAIEndpointConfig);
2463
+ const params = {
2464
+ ...this.clientConfig,
2465
+ baseURL: endpoint,
2466
+ timeout: this.timeout,
2467
+ maxRetries: 0,
2468
+ };
2469
+ if (!params.baseURL) {
2470
+ delete params.baseURL;
2471
+ }
2472
+ this.client = new openai_1.OpenAI(params);
2473
+ }
2474
+ const requestOptions = {
2475
+ ...this.clientConfig,
2476
+ ...options,
2477
+ };
2478
+ return requestOptions;
2479
+ }
2480
+ _llmType() {
2481
+ return "openai";
2627
2482
  }
2628
2483
  /** @ignore */
2629
- async _generate(messages, options, runManager) {
2630
- if (this._useResponsesApi(options)) {
2631
- return this.responses._generate(messages, options);
2632
- }
2633
- return this.completions._generate(messages, options, runManager);
2484
+ _combineLLMOutput(...llmOutputs) {
2485
+ return llmOutputs.reduce((acc, llmOutput) => {
2486
+ if (llmOutput && llmOutput.tokenUsage) {
2487
+ acc.tokenUsage.completionTokens +=
2488
+ llmOutput.tokenUsage.completionTokens ?? 0;
2489
+ acc.tokenUsage.promptTokens += llmOutput.tokenUsage.promptTokens ?? 0;
2490
+ acc.tokenUsage.totalTokens += llmOutput.tokenUsage.totalTokens ?? 0;
2491
+ }
2492
+ return acc;
2493
+ }, {
2494
+ tokenUsage: {
2495
+ completionTokens: 0,
2496
+ promptTokens: 0,
2497
+ totalTokens: 0,
2498
+ },
2499
+ });
2634
2500
  }
2635
- async *_streamResponseChunks(messages, options, runManager) {
2636
- if (this._useResponsesApi(options)) {
2637
- yield* this.responses._streamResponseChunks(messages, options);
2638
- return;
2501
+ withStructuredOutput(outputSchema, config) {
2502
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
2503
+ let schema;
2504
+ let name;
2505
+ let method;
2506
+ let includeRaw;
2507
+ if (isStructuredOutputMethodParams(outputSchema)) {
2508
+ schema = outputSchema.schema;
2509
+ name = outputSchema.name;
2510
+ method = outputSchema.method;
2511
+ includeRaw = outputSchema.includeRaw;
2639
2512
  }
2640
- yield* this.completions._streamResponseChunks(messages, options, runManager);
2641
- }
2642
- withConfig(config) {
2643
- // FIXME: assigning additional config options to the inner chat classes this way
2644
- // is awkward, but it's the only way to preserve the original object identity
2645
- // and still thread config options, which is important since this class is a "proxy"
2646
- // for the inner chat classes. This will be fixed in a later version of langchain
2647
- // when the core runnable interface is improved (0.4) to support this out of the box.
2648
- const bindChatOpenAIConfig = (cls, config) => {
2649
- const oldGenerate = cls._generate;
2650
- const oldStreamResponseChunks = cls._streamResponseChunks;
2651
- return Object.assign(cls, {
2652
- _generate(messages, options, runManager) {
2653
- return oldGenerate.call(cls, messages, { ...options, ...config }, runManager);
2513
+ else {
2514
+ schema = outputSchema;
2515
+ name = config?.name;
2516
+ method = config?.method;
2517
+ includeRaw = config?.includeRaw;
2518
+ }
2519
+ let llm;
2520
+ let outputParser;
2521
+ if (config?.strict !== undefined && method === "jsonMode") {
2522
+ throw new Error("Argument `strict` is only supported for `method` = 'function_calling'");
2523
+ }
2524
+ if (!this.model.startsWith("gpt-3") &&
2525
+ !this.model.startsWith("gpt-4-") &&
2526
+ this.model !== "gpt-4") {
2527
+ if (method === undefined) {
2528
+ method = "jsonSchema";
2529
+ }
2530
+ }
2531
+ else if (method === "jsonSchema") {
2532
+ console.warn(`[WARNING]: JSON Schema is not supported for model "${this.model}". Falling back to tool calling.`);
2533
+ }
2534
+ if (method === "jsonMode") {
2535
+ let outputFormatSchema;
2536
+ if ((0, types_1.isInteropZodSchema)(schema)) {
2537
+ outputParser = output_parsers_1.StructuredOutputParser.fromZodSchema(schema);
2538
+ outputFormatSchema = (0, json_schema_1.toJsonSchema)(schema);
2539
+ }
2540
+ else {
2541
+ outputParser = new output_parsers_1.JsonOutputParser();
2542
+ }
2543
+ llm = this.withConfig({
2544
+ response_format: { type: "json_object" },
2545
+ ls_structured_output_format: {
2546
+ kwargs: { method: "jsonMode" },
2547
+ schema: outputFormatSchema,
2548
+ },
2549
+ });
2550
+ }
2551
+ else if (method === "jsonSchema") {
2552
+ llm = this.withConfig({
2553
+ response_format: {
2554
+ type: "json_schema",
2555
+ json_schema: {
2556
+ name: name ?? "extract",
2557
+ description: (0, types_1.getSchemaDescription)(schema),
2558
+ schema,
2559
+ strict: config?.strict,
2560
+ },
2654
2561
  },
2655
- _streamResponseChunks(messages, options, runManager) {
2656
- return oldStreamResponseChunks.call(cls, messages, { ...options, ...config }, runManager);
2562
+ ls_structured_output_format: {
2563
+ kwargs: { method: "jsonSchema" },
2564
+ schema: (0, json_schema_1.toJsonSchema)(schema),
2657
2565
  },
2658
2566
  });
2659
- };
2660
- this.responses = bindChatOpenAIConfig(this.responses, config);
2661
- this.completions = bindChatOpenAIConfig(this.completions, config);
2662
- // Proxy chat class is also bound for `_useResponsesApi`,
2663
- return bindChatOpenAIConfig(this, config);
2567
+ if ((0, types_1.isInteropZodSchema)(schema)) {
2568
+ const altParser = output_parsers_1.StructuredOutputParser.fromZodSchema(schema);
2569
+ outputParser = runnables_1.RunnableLambda.from((aiMessage) => {
2570
+ if ("parsed" in aiMessage.additional_kwargs) {
2571
+ return aiMessage.additional_kwargs.parsed;
2572
+ }
2573
+ return altParser;
2574
+ });
2575
+ }
2576
+ else {
2577
+ outputParser = new output_parsers_1.JsonOutputParser();
2578
+ }
2579
+ }
2580
+ else {
2581
+ let functionName = name ?? "extract";
2582
+ // Is function calling
2583
+ if ((0, types_1.isInteropZodSchema)(schema)) {
2584
+ const asJsonSchema = (0, json_schema_1.toJsonSchema)(schema);
2585
+ llm = this.withConfig({
2586
+ tools: [
2587
+ {
2588
+ type: "function",
2589
+ function: {
2590
+ name: functionName,
2591
+ description: asJsonSchema.description,
2592
+ parameters: asJsonSchema,
2593
+ },
2594
+ },
2595
+ ],
2596
+ tool_choice: {
2597
+ type: "function",
2598
+ function: {
2599
+ name: functionName,
2600
+ },
2601
+ },
2602
+ ls_structured_output_format: {
2603
+ kwargs: { method: "functionCalling" },
2604
+ schema: asJsonSchema,
2605
+ },
2606
+ // Do not pass `strict` argument to OpenAI if `config.strict` is undefined
2607
+ ...(config?.strict !== undefined ? { strict: config.strict } : {}),
2608
+ });
2609
+ outputParser = new openai_tools_1.JsonOutputKeyToolsParser({
2610
+ returnSingle: true,
2611
+ keyName: functionName,
2612
+ zodSchema: schema,
2613
+ });
2614
+ }
2615
+ else {
2616
+ let openAIFunctionDefinition;
2617
+ if (typeof schema.name === "string" &&
2618
+ typeof schema.parameters === "object" &&
2619
+ schema.parameters != null) {
2620
+ openAIFunctionDefinition = schema;
2621
+ functionName = schema.name;
2622
+ }
2623
+ else {
2624
+ functionName = schema.title ?? functionName;
2625
+ openAIFunctionDefinition = {
2626
+ name: functionName,
2627
+ description: schema.description ?? "",
2628
+ parameters: schema,
2629
+ };
2630
+ }
2631
+ llm = this.withConfig({
2632
+ tools: [
2633
+ {
2634
+ type: "function",
2635
+ function: openAIFunctionDefinition,
2636
+ },
2637
+ ],
2638
+ tool_choice: {
2639
+ type: "function",
2640
+ function: {
2641
+ name: functionName,
2642
+ },
2643
+ },
2644
+ ls_structured_output_format: {
2645
+ kwargs: { method: "functionCalling" },
2646
+ schema: (0, json_schema_1.toJsonSchema)(schema),
2647
+ },
2648
+ // Do not pass `strict` argument to OpenAI if `config.strict` is undefined
2649
+ ...(config?.strict !== undefined ? { strict: config.strict } : {}),
2650
+ });
2651
+ outputParser = new openai_tools_1.JsonOutputKeyToolsParser({
2652
+ returnSingle: true,
2653
+ keyName: functionName,
2654
+ });
2655
+ }
2656
+ }
2657
+ if (!includeRaw) {
2658
+ return llm.pipe(outputParser);
2659
+ }
2660
+ const parserAssign = runnables_1.RunnablePassthrough.assign({
2661
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
2662
+ parsed: (input, config) => outputParser.invoke(input.raw, config),
2663
+ });
2664
+ const parserNone = runnables_1.RunnablePassthrough.assign({
2665
+ parsed: () => null,
2666
+ });
2667
+ const parsedWithFallback = parserAssign.withFallbacks({
2668
+ fallbacks: [parserNone],
2669
+ });
2670
+ return runnables_1.RunnableSequence.from([{ raw: llm }, parsedWithFallback]);
2664
2671
  }
2665
2672
  }
2666
2673
  exports.ChatOpenAI = ChatOpenAI;
2674
+ function isStructuredOutputMethodParams(x
2675
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
2676
+ ) {
2677
+ return (x !== undefined &&
2678
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
2679
+ typeof x.schema ===
2680
+ "object");
2681
+ }
2682
+ // inlined from openai/lib/parser.ts
2683
+ function makeParseableResponseFormat(response_format, parser) {
2684
+ const obj = { ...response_format };
2685
+ Object.defineProperties(obj, {
2686
+ $brand: {
2687
+ value: "auto-parseable-response-format",
2688
+ enumerable: false,
2689
+ },
2690
+ $parseRaw: {
2691
+ value: parser,
2692
+ enumerable: false,
2693
+ },
2694
+ });
2695
+ return obj;
2696
+ }
2697
+ function interopZodResponseFormat(zodSchema, name, props) {
2698
+ if ((0, types_1.isZodSchemaV3)(zodSchema)) {
2699
+ return (0, zod_1.zodResponseFormat)(zodSchema, name, props);
2700
+ }
2701
+ if ((0, types_1.isZodSchemaV4)(zodSchema)) {
2702
+ return makeParseableResponseFormat({
2703
+ type: "json_schema",
2704
+ json_schema: {
2705
+ ...props,
2706
+ name,
2707
+ strict: true,
2708
+ schema: z4.toJSONSchema(zodSchema, {
2709
+ cycles: "ref", // equivalent to nameStrategy: 'duplicate-ref'
2710
+ reused: "ref", // equivalent to $refStrategy: 'extract-to-root'
2711
+ override(ctx) {
2712
+ ctx.jsonSchema.title = name; // equivalent to `name` property
2713
+ // TODO: implement `nullableStrategy` patch-fix (zod doesn't support openApi3 json schema target)
2714
+ // TODO: implement `openaiStrictMode` patch-fix (where optional properties without `nullable` are not supported)
2715
+ },
2716
+ /// property equivalents from native `zodResponseFormat` fn
2717
+ // openaiStrictMode: true,
2718
+ // name,
2719
+ // nameStrategy: 'duplicate-ref',
2720
+ // $refStrategy: 'extract-to-root',
2721
+ // nullableStrategy: 'property',
2722
+ }),
2723
+ },
2724
+ }, (content) => z4.parse(zodSchema, JSON.parse(content)));
2725
+ }
2726
+ throw new Error("Unsupported schema response format");
2727
+ }