@langchain/openai 0.5.18 → 0.6.1

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