@langchain/openai 0.4.1 → 0.4.3

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.
@@ -440,6 +440,20 @@ class AzureChatOpenAI extends chat_models_js_1.ChatOpenAI {
440
440
  azureOpenAIApiKey: "AZURE_OPENAI_API_KEY",
441
441
  };
442
442
  }
443
+ get lc_serializable_keys() {
444
+ return [
445
+ ...super.lc_serializable_keys,
446
+ "azureOpenAIApiKey",
447
+ "azureOpenAIApiVersion",
448
+ "azureOpenAIBasePath",
449
+ "azureOpenAIEndpoint",
450
+ "azureOpenAIApiInstanceName",
451
+ "azureOpenAIApiDeploymentName",
452
+ "deploymentName",
453
+ "openAIApiKey",
454
+ "openAIApiVersion",
455
+ ];
456
+ }
443
457
  constructor(fields) {
444
458
  super(fields);
445
459
  Object.defineProperty(this, "azureOpenAIApiVersion", {
@@ -432,6 +432,7 @@ export declare class AzureChatOpenAI extends ChatOpenAI {
432
432
  get lc_secrets(): {
433
433
  [key: string]: string;
434
434
  } | undefined;
435
+ get lc_serializable_keys(): string[];
435
436
  constructor(fields?: Partial<OpenAIChatInput> & Partial<AzureOpenAIInput> & {
436
437
  openAIApiKey?: string;
437
438
  openAIApiVersion?: string;
@@ -437,6 +437,20 @@ export class AzureChatOpenAI extends ChatOpenAI {
437
437
  azureOpenAIApiKey: "AZURE_OPENAI_API_KEY",
438
438
  };
439
439
  }
440
+ get lc_serializable_keys() {
441
+ return [
442
+ ...super.lc_serializable_keys,
443
+ "azureOpenAIApiKey",
444
+ "azureOpenAIApiVersion",
445
+ "azureOpenAIBasePath",
446
+ "azureOpenAIEndpoint",
447
+ "azureOpenAIApiInstanceName",
448
+ "azureOpenAIApiDeploymentName",
449
+ "deploymentName",
450
+ "openAIApiKey",
451
+ "openAIApiVersion",
452
+ ];
453
+ }
440
454
  constructor(fields) {
441
455
  super(fields);
442
456
  Object.defineProperty(this, "azureOpenAIApiVersion", {
@@ -50,138 +50,6 @@ function messageToOpenAIRole(message) {
50
50
  }
51
51
  }
52
52
  exports.messageToOpenAIRole = messageToOpenAIRole;
53
- function openAIResponseToChatMessage(message, rawResponse, includeRawResponse) {
54
- const rawToolCalls = message.tool_calls;
55
- switch (message.role) {
56
- case "assistant": {
57
- const toolCalls = [];
58
- const invalidToolCalls = [];
59
- for (const rawToolCall of rawToolCalls ?? []) {
60
- try {
61
- toolCalls.push((0, openai_tools_1.parseToolCall)(rawToolCall, { returnId: true }));
62
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
63
- }
64
- catch (e) {
65
- invalidToolCalls.push((0, openai_tools_1.makeInvalidToolCall)(rawToolCall, e.message));
66
- }
67
- }
68
- const additional_kwargs = {
69
- function_call: message.function_call,
70
- tool_calls: rawToolCalls,
71
- };
72
- if (includeRawResponse !== undefined) {
73
- additional_kwargs.__raw_response = rawResponse;
74
- }
75
- const response_metadata = {
76
- model_name: rawResponse.model,
77
- ...(rawResponse.system_fingerprint
78
- ? {
79
- usage: { ...rawResponse.usage },
80
- system_fingerprint: rawResponse.system_fingerprint,
81
- }
82
- : {}),
83
- };
84
- if (message.audio) {
85
- additional_kwargs.audio = message.audio;
86
- }
87
- return new messages_1.AIMessage({
88
- content: message.content || "",
89
- tool_calls: toolCalls,
90
- invalid_tool_calls: invalidToolCalls,
91
- additional_kwargs,
92
- response_metadata,
93
- id: rawResponse.id,
94
- });
95
- }
96
- default:
97
- return new messages_1.ChatMessage(message.content || "", message.role ?? "unknown");
98
- }
99
- }
100
- function _convertDeltaToMessageChunk(
101
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
102
- delta, rawResponse, defaultRole, includeRawResponse) {
103
- const role = delta.role ?? defaultRole;
104
- const content = delta.content ?? "";
105
- let additional_kwargs;
106
- if (delta.function_call) {
107
- additional_kwargs = {
108
- function_call: delta.function_call,
109
- };
110
- }
111
- else if (delta.tool_calls) {
112
- additional_kwargs = {
113
- tool_calls: delta.tool_calls,
114
- };
115
- }
116
- else {
117
- additional_kwargs = {};
118
- }
119
- if (includeRawResponse) {
120
- additional_kwargs.__raw_response = rawResponse;
121
- }
122
- if (delta.audio) {
123
- additional_kwargs.audio = {
124
- ...delta.audio,
125
- index: rawResponse.choices[0].index,
126
- };
127
- }
128
- const response_metadata = { usage: { ...rawResponse.usage } };
129
- if (role === "user") {
130
- return new messages_1.HumanMessageChunk({ content, response_metadata });
131
- }
132
- else if (role === "assistant") {
133
- const toolCallChunks = [];
134
- if (Array.isArray(delta.tool_calls)) {
135
- for (const rawToolCall of delta.tool_calls) {
136
- toolCallChunks.push({
137
- name: rawToolCall.function?.name,
138
- args: rawToolCall.function?.arguments,
139
- id: rawToolCall.id,
140
- index: rawToolCall.index,
141
- type: "tool_call_chunk",
142
- });
143
- }
144
- }
145
- return new messages_1.AIMessageChunk({
146
- content,
147
- tool_call_chunks: toolCallChunks,
148
- additional_kwargs,
149
- id: rawResponse.id,
150
- response_metadata,
151
- });
152
- }
153
- else if (role === "system") {
154
- return new messages_1.SystemMessageChunk({ content, response_metadata });
155
- }
156
- else if (role === "developer") {
157
- return new messages_1.SystemMessageChunk({
158
- content,
159
- response_metadata,
160
- additional_kwargs: {
161
- __openai_role__: "developer",
162
- },
163
- });
164
- }
165
- else if (role === "function") {
166
- return new messages_1.FunctionMessageChunk({
167
- content,
168
- additional_kwargs,
169
- name: delta.name,
170
- response_metadata,
171
- });
172
- }
173
- else if (role === "tool") {
174
- return new messages_1.ToolMessageChunk({
175
- content,
176
- additional_kwargs,
177
- tool_call_id: delta.tool_call_id,
178
- response_metadata,
179
- });
180
- }
181
- else {
182
- return new messages_1.ChatMessageChunk({ content, role, response_metadata });
183
- }
184
- }
185
53
  // Used in LangSmith, export is important here
186
54
  function _convertMessagesToOpenAIParams(messages, model) {
187
55
  // TODO: Function messages do not support array content, fix cast
@@ -814,6 +682,44 @@ class ChatOpenAI extends chat_models_1.BaseChatModel {
814
682
  apiKey: "openai_api_key",
815
683
  };
816
684
  }
685
+ get lc_serializable_keys() {
686
+ return [
687
+ "configuration",
688
+ "logprobs",
689
+ "topLogprobs",
690
+ "prefixMessages",
691
+ "supportsStrictToolCalling",
692
+ "modalities",
693
+ "audio",
694
+ "reasoningEffort",
695
+ "temperature",
696
+ "maxTokens",
697
+ "topP",
698
+ "frequencyPenalty",
699
+ "presencePenalty",
700
+ "n",
701
+ "logitBias",
702
+ "user",
703
+ "streaming",
704
+ "streamUsage",
705
+ "modelName",
706
+ "model",
707
+ "modelKwargs",
708
+ "stop",
709
+ "stopSequences",
710
+ "timeout",
711
+ "openAIApiKey",
712
+ "apiKey",
713
+ "cache",
714
+ "maxConcurrency",
715
+ "maxRetries",
716
+ "verbose",
717
+ "callbacks",
718
+ "tags",
719
+ "metadata",
720
+ "disableStreaming",
721
+ ];
722
+ }
817
723
  constructor(fields) {
818
724
  super(fields ?? {});
819
725
  Object.defineProperty(this, "lc_serializable", {
@@ -1136,6 +1042,138 @@ class ChatOpenAI extends chat_models_1.BaseChatModel {
1136
1042
  }
1137
1043
  return params;
1138
1044
  }
1045
+ _convertOpenAIChatCompletionMessageToBaseMessage(message, rawResponse) {
1046
+ const rawToolCalls = message.tool_calls;
1047
+ switch (message.role) {
1048
+ case "assistant": {
1049
+ const toolCalls = [];
1050
+ const invalidToolCalls = [];
1051
+ for (const rawToolCall of rawToolCalls ?? []) {
1052
+ try {
1053
+ toolCalls.push((0, openai_tools_1.parseToolCall)(rawToolCall, { returnId: true }));
1054
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
1055
+ }
1056
+ catch (e) {
1057
+ invalidToolCalls.push((0, openai_tools_1.makeInvalidToolCall)(rawToolCall, e.message));
1058
+ }
1059
+ }
1060
+ const additional_kwargs = {
1061
+ function_call: message.function_call,
1062
+ tool_calls: rawToolCalls,
1063
+ };
1064
+ if (this.__includeRawResponse !== undefined) {
1065
+ additional_kwargs.__raw_response = rawResponse;
1066
+ }
1067
+ const response_metadata = {
1068
+ model_name: rawResponse.model,
1069
+ ...(rawResponse.system_fingerprint
1070
+ ? {
1071
+ usage: { ...rawResponse.usage },
1072
+ system_fingerprint: rawResponse.system_fingerprint,
1073
+ }
1074
+ : {}),
1075
+ };
1076
+ if (message.audio) {
1077
+ additional_kwargs.audio = message.audio;
1078
+ }
1079
+ return new messages_1.AIMessage({
1080
+ content: message.content || "",
1081
+ tool_calls: toolCalls,
1082
+ invalid_tool_calls: invalidToolCalls,
1083
+ additional_kwargs,
1084
+ response_metadata,
1085
+ id: rawResponse.id,
1086
+ });
1087
+ }
1088
+ default:
1089
+ return new messages_1.ChatMessage(message.content || "", message.role ?? "unknown");
1090
+ }
1091
+ }
1092
+ _convertOpenAIDeltaToBaseMessageChunk(
1093
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
1094
+ delta, rawResponse, defaultRole) {
1095
+ const role = delta.role ?? defaultRole;
1096
+ const content = delta.content ?? "";
1097
+ let additional_kwargs;
1098
+ if (delta.function_call) {
1099
+ additional_kwargs = {
1100
+ function_call: delta.function_call,
1101
+ };
1102
+ }
1103
+ else if (delta.tool_calls) {
1104
+ additional_kwargs = {
1105
+ tool_calls: delta.tool_calls,
1106
+ };
1107
+ }
1108
+ else {
1109
+ additional_kwargs = {};
1110
+ }
1111
+ if (this.__includeRawResponse) {
1112
+ additional_kwargs.__raw_response = rawResponse;
1113
+ }
1114
+ if (delta.audio) {
1115
+ additional_kwargs.audio = {
1116
+ ...delta.audio,
1117
+ index: rawResponse.choices[0].index,
1118
+ };
1119
+ }
1120
+ const response_metadata = { usage: { ...rawResponse.usage } };
1121
+ if (role === "user") {
1122
+ return new messages_1.HumanMessageChunk({ content, response_metadata });
1123
+ }
1124
+ else if (role === "assistant") {
1125
+ const toolCallChunks = [];
1126
+ if (Array.isArray(delta.tool_calls)) {
1127
+ for (const rawToolCall of delta.tool_calls) {
1128
+ toolCallChunks.push({
1129
+ name: rawToolCall.function?.name,
1130
+ args: rawToolCall.function?.arguments,
1131
+ id: rawToolCall.id,
1132
+ index: rawToolCall.index,
1133
+ type: "tool_call_chunk",
1134
+ });
1135
+ }
1136
+ }
1137
+ return new messages_1.AIMessageChunk({
1138
+ content,
1139
+ tool_call_chunks: toolCallChunks,
1140
+ additional_kwargs,
1141
+ id: rawResponse.id,
1142
+ response_metadata,
1143
+ });
1144
+ }
1145
+ else if (role === "system") {
1146
+ return new messages_1.SystemMessageChunk({ content, response_metadata });
1147
+ }
1148
+ else if (role === "developer") {
1149
+ return new messages_1.SystemMessageChunk({
1150
+ content,
1151
+ response_metadata,
1152
+ additional_kwargs: {
1153
+ __openai_role__: "developer",
1154
+ },
1155
+ });
1156
+ }
1157
+ else if (role === "function") {
1158
+ return new messages_1.FunctionMessageChunk({
1159
+ content,
1160
+ additional_kwargs,
1161
+ name: delta.name,
1162
+ response_metadata,
1163
+ });
1164
+ }
1165
+ else if (role === "tool") {
1166
+ return new messages_1.ToolMessageChunk({
1167
+ content,
1168
+ additional_kwargs,
1169
+ tool_call_id: delta.tool_call_id,
1170
+ response_metadata,
1171
+ });
1172
+ }
1173
+ else {
1174
+ return new messages_1.ChatMessageChunk({ content, role, response_metadata });
1175
+ }
1176
+ }
1139
1177
  /** @ignore */
1140
1178
  _identifyingParams() {
1141
1179
  return {
@@ -1168,7 +1206,7 @@ class ChatOpenAI extends chat_models_1.BaseChatModel {
1168
1206
  if (!delta) {
1169
1207
  continue;
1170
1208
  }
1171
- const chunk = _convertDeltaToMessageChunk(delta, data, defaultRole, this.__includeRawResponse);
1209
+ const chunk = this._convertOpenAIDeltaToBaseMessageChunk(delta, data, defaultRole);
1172
1210
  defaultRole = delta.role ?? defaultRole;
1173
1211
  const newTokenIndices = {
1174
1212
  prompt: options.promptIndex ?? 0,
@@ -1354,7 +1392,7 @@ class ChatOpenAI extends chat_models_1.BaseChatModel {
1354
1392
  const text = part.message?.content ?? "";
1355
1393
  const generation = {
1356
1394
  text,
1357
- message: openAIResponseToChatMessage(part.message ?? { role: "assistant" }, data, this.__includeRawResponse),
1395
+ message: this._convertOpenAIChatCompletionMessageToBaseMessage(part.message ?? { role: "assistant" }, data),
1358
1396
  };
1359
1397
  generation.generationInfo = {
1360
1398
  ...(part.finish_reason ? { finish_reason: part.finish_reason } : {}),
@@ -1,6 +1,6 @@
1
1
  import { type ClientOptions, OpenAI as OpenAIClient } from "openai";
2
2
  import { CallbackManagerForLLMRun } from "@langchain/core/callbacks/manager";
3
- import { AIMessageChunk, type BaseMessage } from "@langchain/core/messages";
3
+ import { AIMessageChunk, type BaseMessage, ChatMessageChunk, FunctionMessageChunk, HumanMessageChunk, SystemMessageChunk, ToolMessageChunk } from "@langchain/core/messages";
4
4
  import { ChatGenerationChunk, type ChatResult } from "@langchain/core/outputs";
5
5
  import { BaseChatModel, BindToolsInput, LangSmithParams, type BaseChatModelParams } from "@langchain/core/language_models/chat_models";
6
6
  import { type BaseFunctionCallOptions, type BaseLanguageModelInput, type StructuredOutputMethodOptions } from "@langchain/core/language_models/base";
@@ -655,6 +655,7 @@ export declare class ChatOpenAI<CallOptions extends ChatOpenAICallOptions = Chat
655
655
  [key: string]: string;
656
656
  } | undefined;
657
657
  get lc_aliases(): Record<string, string>;
658
+ get lc_serializable_keys(): string[];
658
659
  temperature?: number;
659
660
  topP?: number;
660
661
  frequencyPenalty?: number;
@@ -698,6 +699,8 @@ export declare class ChatOpenAI<CallOptions extends ChatOpenAICallOptions = Chat
698
699
  invocationParams(options?: this["ParsedCallOptions"], extra?: {
699
700
  streaming?: boolean;
700
701
  }): Omit<OpenAIClient.Chat.ChatCompletionCreateParams, "messages">;
702
+ protected _convertOpenAIChatCompletionMessageToBaseMessage(message: OpenAIClient.Chat.Completions.ChatCompletionMessage, rawResponse: OpenAIClient.Chat.Completions.ChatCompletion): BaseMessage;
703
+ protected _convertOpenAIDeltaToBaseMessageChunk(delta: Record<string, any>, rawResponse: OpenAIClient.Chat.Completions.ChatCompletionChunk, defaultRole?: OpenAIRoleEnum): AIMessageChunk | HumanMessageChunk | SystemMessageChunk | FunctionMessageChunk | ToolMessageChunk | ChatMessageChunk;
701
704
  /** @ignore */
702
705
  _identifyingParams(): Omit<OpenAIClient.Chat.ChatCompletionCreateParams, "messages"> & {
703
706
  model_name: string;
@@ -46,138 +46,6 @@ export function messageToOpenAIRole(message) {
46
46
  throw new Error(`Unknown message type: ${type}`);
47
47
  }
48
48
  }
49
- function openAIResponseToChatMessage(message, rawResponse, includeRawResponse) {
50
- const rawToolCalls = message.tool_calls;
51
- switch (message.role) {
52
- case "assistant": {
53
- const toolCalls = [];
54
- const invalidToolCalls = [];
55
- for (const rawToolCall of rawToolCalls ?? []) {
56
- try {
57
- toolCalls.push(parseToolCall(rawToolCall, { returnId: true }));
58
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
59
- }
60
- catch (e) {
61
- invalidToolCalls.push(makeInvalidToolCall(rawToolCall, e.message));
62
- }
63
- }
64
- const additional_kwargs = {
65
- function_call: message.function_call,
66
- tool_calls: rawToolCalls,
67
- };
68
- if (includeRawResponse !== undefined) {
69
- additional_kwargs.__raw_response = rawResponse;
70
- }
71
- const response_metadata = {
72
- model_name: rawResponse.model,
73
- ...(rawResponse.system_fingerprint
74
- ? {
75
- usage: { ...rawResponse.usage },
76
- system_fingerprint: rawResponse.system_fingerprint,
77
- }
78
- : {}),
79
- };
80
- if (message.audio) {
81
- additional_kwargs.audio = message.audio;
82
- }
83
- return new AIMessage({
84
- content: message.content || "",
85
- tool_calls: toolCalls,
86
- invalid_tool_calls: invalidToolCalls,
87
- additional_kwargs,
88
- response_metadata,
89
- id: rawResponse.id,
90
- });
91
- }
92
- default:
93
- return new ChatMessage(message.content || "", message.role ?? "unknown");
94
- }
95
- }
96
- function _convertDeltaToMessageChunk(
97
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
98
- delta, rawResponse, defaultRole, includeRawResponse) {
99
- const role = delta.role ?? defaultRole;
100
- const content = delta.content ?? "";
101
- let additional_kwargs;
102
- if (delta.function_call) {
103
- additional_kwargs = {
104
- function_call: delta.function_call,
105
- };
106
- }
107
- else if (delta.tool_calls) {
108
- additional_kwargs = {
109
- tool_calls: delta.tool_calls,
110
- };
111
- }
112
- else {
113
- additional_kwargs = {};
114
- }
115
- if (includeRawResponse) {
116
- additional_kwargs.__raw_response = rawResponse;
117
- }
118
- if (delta.audio) {
119
- additional_kwargs.audio = {
120
- ...delta.audio,
121
- index: rawResponse.choices[0].index,
122
- };
123
- }
124
- const response_metadata = { usage: { ...rawResponse.usage } };
125
- if (role === "user") {
126
- return new HumanMessageChunk({ content, response_metadata });
127
- }
128
- else if (role === "assistant") {
129
- const toolCallChunks = [];
130
- if (Array.isArray(delta.tool_calls)) {
131
- for (const rawToolCall of delta.tool_calls) {
132
- toolCallChunks.push({
133
- name: rawToolCall.function?.name,
134
- args: rawToolCall.function?.arguments,
135
- id: rawToolCall.id,
136
- index: rawToolCall.index,
137
- type: "tool_call_chunk",
138
- });
139
- }
140
- }
141
- return new AIMessageChunk({
142
- content,
143
- tool_call_chunks: toolCallChunks,
144
- additional_kwargs,
145
- id: rawResponse.id,
146
- response_metadata,
147
- });
148
- }
149
- else if (role === "system") {
150
- return new SystemMessageChunk({ content, response_metadata });
151
- }
152
- else if (role === "developer") {
153
- return new SystemMessageChunk({
154
- content,
155
- response_metadata,
156
- additional_kwargs: {
157
- __openai_role__: "developer",
158
- },
159
- });
160
- }
161
- else if (role === "function") {
162
- return new FunctionMessageChunk({
163
- content,
164
- additional_kwargs,
165
- name: delta.name,
166
- response_metadata,
167
- });
168
- }
169
- else if (role === "tool") {
170
- return new ToolMessageChunk({
171
- content,
172
- additional_kwargs,
173
- tool_call_id: delta.tool_call_id,
174
- response_metadata,
175
- });
176
- }
177
- else {
178
- return new ChatMessageChunk({ content, role, response_metadata });
179
- }
180
- }
181
49
  // Used in LangSmith, export is important here
182
50
  export function _convertMessagesToOpenAIParams(messages, model) {
183
51
  // TODO: Function messages do not support array content, fix cast
@@ -809,6 +677,44 @@ export class ChatOpenAI extends BaseChatModel {
809
677
  apiKey: "openai_api_key",
810
678
  };
811
679
  }
680
+ get lc_serializable_keys() {
681
+ return [
682
+ "configuration",
683
+ "logprobs",
684
+ "topLogprobs",
685
+ "prefixMessages",
686
+ "supportsStrictToolCalling",
687
+ "modalities",
688
+ "audio",
689
+ "reasoningEffort",
690
+ "temperature",
691
+ "maxTokens",
692
+ "topP",
693
+ "frequencyPenalty",
694
+ "presencePenalty",
695
+ "n",
696
+ "logitBias",
697
+ "user",
698
+ "streaming",
699
+ "streamUsage",
700
+ "modelName",
701
+ "model",
702
+ "modelKwargs",
703
+ "stop",
704
+ "stopSequences",
705
+ "timeout",
706
+ "openAIApiKey",
707
+ "apiKey",
708
+ "cache",
709
+ "maxConcurrency",
710
+ "maxRetries",
711
+ "verbose",
712
+ "callbacks",
713
+ "tags",
714
+ "metadata",
715
+ "disableStreaming",
716
+ ];
717
+ }
812
718
  constructor(fields) {
813
719
  super(fields ?? {});
814
720
  Object.defineProperty(this, "lc_serializable", {
@@ -1131,6 +1037,138 @@ export class ChatOpenAI extends BaseChatModel {
1131
1037
  }
1132
1038
  return params;
1133
1039
  }
1040
+ _convertOpenAIChatCompletionMessageToBaseMessage(message, rawResponse) {
1041
+ const rawToolCalls = message.tool_calls;
1042
+ switch (message.role) {
1043
+ case "assistant": {
1044
+ const toolCalls = [];
1045
+ const invalidToolCalls = [];
1046
+ for (const rawToolCall of rawToolCalls ?? []) {
1047
+ try {
1048
+ toolCalls.push(parseToolCall(rawToolCall, { returnId: true }));
1049
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
1050
+ }
1051
+ catch (e) {
1052
+ invalidToolCalls.push(makeInvalidToolCall(rawToolCall, e.message));
1053
+ }
1054
+ }
1055
+ const additional_kwargs = {
1056
+ function_call: message.function_call,
1057
+ tool_calls: rawToolCalls,
1058
+ };
1059
+ if (this.__includeRawResponse !== undefined) {
1060
+ additional_kwargs.__raw_response = rawResponse;
1061
+ }
1062
+ const response_metadata = {
1063
+ model_name: rawResponse.model,
1064
+ ...(rawResponse.system_fingerprint
1065
+ ? {
1066
+ usage: { ...rawResponse.usage },
1067
+ system_fingerprint: rawResponse.system_fingerprint,
1068
+ }
1069
+ : {}),
1070
+ };
1071
+ if (message.audio) {
1072
+ additional_kwargs.audio = message.audio;
1073
+ }
1074
+ return new AIMessage({
1075
+ content: message.content || "",
1076
+ tool_calls: toolCalls,
1077
+ invalid_tool_calls: invalidToolCalls,
1078
+ additional_kwargs,
1079
+ response_metadata,
1080
+ id: rawResponse.id,
1081
+ });
1082
+ }
1083
+ default:
1084
+ return new ChatMessage(message.content || "", message.role ?? "unknown");
1085
+ }
1086
+ }
1087
+ _convertOpenAIDeltaToBaseMessageChunk(
1088
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
1089
+ delta, rawResponse, defaultRole) {
1090
+ const role = delta.role ?? defaultRole;
1091
+ const content = delta.content ?? "";
1092
+ let additional_kwargs;
1093
+ if (delta.function_call) {
1094
+ additional_kwargs = {
1095
+ function_call: delta.function_call,
1096
+ };
1097
+ }
1098
+ else if (delta.tool_calls) {
1099
+ additional_kwargs = {
1100
+ tool_calls: delta.tool_calls,
1101
+ };
1102
+ }
1103
+ else {
1104
+ additional_kwargs = {};
1105
+ }
1106
+ if (this.__includeRawResponse) {
1107
+ additional_kwargs.__raw_response = rawResponse;
1108
+ }
1109
+ if (delta.audio) {
1110
+ additional_kwargs.audio = {
1111
+ ...delta.audio,
1112
+ index: rawResponse.choices[0].index,
1113
+ };
1114
+ }
1115
+ const response_metadata = { usage: { ...rawResponse.usage } };
1116
+ if (role === "user") {
1117
+ return new HumanMessageChunk({ content, response_metadata });
1118
+ }
1119
+ else if (role === "assistant") {
1120
+ const toolCallChunks = [];
1121
+ if (Array.isArray(delta.tool_calls)) {
1122
+ for (const rawToolCall of delta.tool_calls) {
1123
+ toolCallChunks.push({
1124
+ name: rawToolCall.function?.name,
1125
+ args: rawToolCall.function?.arguments,
1126
+ id: rawToolCall.id,
1127
+ index: rawToolCall.index,
1128
+ type: "tool_call_chunk",
1129
+ });
1130
+ }
1131
+ }
1132
+ return new AIMessageChunk({
1133
+ content,
1134
+ tool_call_chunks: toolCallChunks,
1135
+ additional_kwargs,
1136
+ id: rawResponse.id,
1137
+ response_metadata,
1138
+ });
1139
+ }
1140
+ else if (role === "system") {
1141
+ return new SystemMessageChunk({ content, response_metadata });
1142
+ }
1143
+ else if (role === "developer") {
1144
+ return new SystemMessageChunk({
1145
+ content,
1146
+ response_metadata,
1147
+ additional_kwargs: {
1148
+ __openai_role__: "developer",
1149
+ },
1150
+ });
1151
+ }
1152
+ else if (role === "function") {
1153
+ return new FunctionMessageChunk({
1154
+ content,
1155
+ additional_kwargs,
1156
+ name: delta.name,
1157
+ response_metadata,
1158
+ });
1159
+ }
1160
+ else if (role === "tool") {
1161
+ return new ToolMessageChunk({
1162
+ content,
1163
+ additional_kwargs,
1164
+ tool_call_id: delta.tool_call_id,
1165
+ response_metadata,
1166
+ });
1167
+ }
1168
+ else {
1169
+ return new ChatMessageChunk({ content, role, response_metadata });
1170
+ }
1171
+ }
1134
1172
  /** @ignore */
1135
1173
  _identifyingParams() {
1136
1174
  return {
@@ -1163,7 +1201,7 @@ export class ChatOpenAI extends BaseChatModel {
1163
1201
  if (!delta) {
1164
1202
  continue;
1165
1203
  }
1166
- const chunk = _convertDeltaToMessageChunk(delta, data, defaultRole, this.__includeRawResponse);
1204
+ const chunk = this._convertOpenAIDeltaToBaseMessageChunk(delta, data, defaultRole);
1167
1205
  defaultRole = delta.role ?? defaultRole;
1168
1206
  const newTokenIndices = {
1169
1207
  prompt: options.promptIndex ?? 0,
@@ -1349,7 +1387,7 @@ export class ChatOpenAI extends BaseChatModel {
1349
1387
  const text = part.message?.content ?? "";
1350
1388
  const generation = {
1351
1389
  text,
1352
- message: openAIResponseToChatMessage(part.message ?? { role: "assistant" }, data, this.__includeRawResponse),
1390
+ message: this._convertOpenAIChatCompletionMessageToBaseMessage(part.message ?? { role: "assistant" }, data),
1353
1391
  };
1354
1392
  generation.generationInfo = {
1355
1393
  ...(part.finish_reason ? { finish_reason: part.finish_reason } : {}),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@langchain/openai",
3
- "version": "0.4.1",
3
+ "version": "0.4.3",
4
4
  "description": "OpenAI integrations for LangChain.js",
5
5
  "type": "module",
6
6
  "engines": {
@@ -41,7 +41,7 @@
41
41
  "zod-to-json-schema": "^3.22.3"
42
42
  },
43
43
  "peerDependencies": {
44
- "@langchain/core": ">=0.3.29 <0.4.0"
44
+ "@langchain/core": ">=0.3.39 <0.4.0"
45
45
  },
46
46
  "devDependencies": {
47
47
  "@azure/identity": "^4.2.1",