@langchain/openai 0.3.3 → 0.3.5

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.
@@ -74,6 +74,7 @@ function openAIResponseToChatMessage(message, rawResponse, includeRawResponse) {
74
74
  let response_metadata;
75
75
  if (rawResponse.system_fingerprint) {
76
76
  response_metadata = {
77
+ usage: { ...rawResponse.usage },
77
78
  system_fingerprint: rawResponse.system_fingerprint,
78
79
  };
79
80
  }
@@ -112,8 +113,9 @@ delta, rawResponse, defaultRole, includeRawResponse) {
112
113
  if (includeRawResponse) {
113
114
  additional_kwargs.__raw_response = rawResponse;
114
115
  }
116
+ const response_metadata = { usage: { ...rawResponse.usage } };
115
117
  if (role === "user") {
116
- return new messages_1.HumanMessageChunk({ content });
118
+ return new messages_1.HumanMessageChunk({ content, response_metadata });
117
119
  }
118
120
  else if (role === "assistant") {
119
121
  const toolCallChunks = [];
@@ -133,16 +135,18 @@ delta, rawResponse, defaultRole, includeRawResponse) {
133
135
  tool_call_chunks: toolCallChunks,
134
136
  additional_kwargs,
135
137
  id: rawResponse.id,
138
+ response_metadata,
136
139
  });
137
140
  }
138
141
  else if (role === "system") {
139
- return new messages_1.SystemMessageChunk({ content });
142
+ return new messages_1.SystemMessageChunk({ content, response_metadata });
140
143
  }
141
144
  else if (role === "function") {
142
145
  return new messages_1.FunctionMessageChunk({
143
146
  content,
144
147
  additional_kwargs,
145
148
  name: delta.name,
149
+ response_metadata,
146
150
  });
147
151
  }
148
152
  else if (role === "tool") {
@@ -150,10 +154,11 @@ delta, rawResponse, defaultRole, includeRawResponse) {
150
154
  content,
151
155
  additional_kwargs,
152
156
  tool_call_id: delta.tool_call_id,
157
+ response_metadata,
153
158
  });
154
159
  }
155
160
  else {
156
- return new messages_1.ChatMessageChunk({ content, role });
161
+ return new messages_1.ChatMessageChunk({ content, role, response_metadata });
157
162
  }
158
163
  }
159
164
  // Used in LangSmith, export is important here
@@ -458,7 +463,10 @@ function _convertChatOpenAIToolTypeToOpenAITool(tool, fields) {
458
463
  * rating: z.number().optional().describe("How funny the joke is, from 1 to 10")
459
464
  * }).describe('Joke to tell user.');
460
465
  *
461
- * const structuredLlm = llm.withStructuredOutput(Joke, { name: "Joke" });
466
+ * const structuredLlm = llm.withStructuredOutput(Joke, {
467
+ * name: "Joke",
468
+ * strict: true, // Optionally enable OpenAI structured outputs
469
+ * });
462
470
  * const jokeResult = await structuredLlm.invoke("Tell me a joke about cats");
463
471
  * console.log(jokeResult);
464
472
  * ```
@@ -1105,20 +1113,6 @@ class ChatOpenAI extends chat_models_1.BaseChatModel {
1105
1113
  stream: true,
1106
1114
  };
1107
1115
  let defaultRole;
1108
- if (params.response_format &&
1109
- params.response_format.type === "json_schema") {
1110
- console.warn(`OpenAI does not yet support streaming with "response_format" set to "json_schema". Falling back to non-streaming mode.`);
1111
- const res = await this._generate(messages, options, runManager);
1112
- const chunk = new outputs_1.ChatGenerationChunk({
1113
- message: new messages_1.AIMessageChunk({
1114
- ...res.generations[0].message,
1115
- }),
1116
- text: res.generations[0].text,
1117
- generationInfo: res.generations[0].generationInfo,
1118
- });
1119
- yield chunk;
1120
- return runManager?.handleLLMNewToken(res.generations[0].text ?? "", undefined, undefined, undefined, undefined, { chunk });
1121
- }
1122
1116
  const streamIterable = await this.completionWithRetry(params, options);
1123
1117
  let usage;
1124
1118
  for await (const data of streamIterable) {
@@ -1166,6 +1160,9 @@ class ChatOpenAI extends chat_models_1.BaseChatModel {
1166
1160
  const generationChunk = new outputs_1.ChatGenerationChunk({
1167
1161
  message: new messages_1.AIMessageChunk({
1168
1162
  content: "",
1163
+ response_metadata: {
1164
+ usage: { ...usage },
1165
+ },
1169
1166
  usage_metadata: {
1170
1167
  input_tokens: usage.prompt_tokens,
1171
1168
  output_tokens: usage.completion_tokens,
@@ -334,7 +334,10 @@ export interface ChatOpenAIFields extends Partial<OpenAIChatInput>, Partial<Azur
334
334
  * rating: z.number().optional().describe("How funny the joke is, from 1 to 10")
335
335
  * }).describe('Joke to tell user.');
336
336
  *
337
- * const structuredLlm = llm.withStructuredOutput(Joke, { name: "Joke" });
337
+ * const structuredLlm = llm.withStructuredOutput(Joke, {
338
+ * name: "Joke",
339
+ * strict: true, // Optionally enable OpenAI structured outputs
340
+ * });
338
341
  * const jokeResult = await structuredLlm.invoke("Tell me a joke about cats");
339
342
  * console.log(jokeResult);
340
343
  * ```
@@ -70,6 +70,7 @@ function openAIResponseToChatMessage(message, rawResponse, includeRawResponse) {
70
70
  let response_metadata;
71
71
  if (rawResponse.system_fingerprint) {
72
72
  response_metadata = {
73
+ usage: { ...rawResponse.usage },
73
74
  system_fingerprint: rawResponse.system_fingerprint,
74
75
  };
75
76
  }
@@ -108,8 +109,9 @@ delta, rawResponse, defaultRole, includeRawResponse) {
108
109
  if (includeRawResponse) {
109
110
  additional_kwargs.__raw_response = rawResponse;
110
111
  }
112
+ const response_metadata = { usage: { ...rawResponse.usage } };
111
113
  if (role === "user") {
112
- return new HumanMessageChunk({ content });
114
+ return new HumanMessageChunk({ content, response_metadata });
113
115
  }
114
116
  else if (role === "assistant") {
115
117
  const toolCallChunks = [];
@@ -129,16 +131,18 @@ delta, rawResponse, defaultRole, includeRawResponse) {
129
131
  tool_call_chunks: toolCallChunks,
130
132
  additional_kwargs,
131
133
  id: rawResponse.id,
134
+ response_metadata,
132
135
  });
133
136
  }
134
137
  else if (role === "system") {
135
- return new SystemMessageChunk({ content });
138
+ return new SystemMessageChunk({ content, response_metadata });
136
139
  }
137
140
  else if (role === "function") {
138
141
  return new FunctionMessageChunk({
139
142
  content,
140
143
  additional_kwargs,
141
144
  name: delta.name,
145
+ response_metadata,
142
146
  });
143
147
  }
144
148
  else if (role === "tool") {
@@ -146,10 +150,11 @@ delta, rawResponse, defaultRole, includeRawResponse) {
146
150
  content,
147
151
  additional_kwargs,
148
152
  tool_call_id: delta.tool_call_id,
153
+ response_metadata,
149
154
  });
150
155
  }
151
156
  else {
152
- return new ChatMessageChunk({ content, role });
157
+ return new ChatMessageChunk({ content, role, response_metadata });
153
158
  }
154
159
  }
155
160
  // Used in LangSmith, export is important here
@@ -453,7 +458,10 @@ function _convertChatOpenAIToolTypeToOpenAITool(tool, fields) {
453
458
  * rating: z.number().optional().describe("How funny the joke is, from 1 to 10")
454
459
  * }).describe('Joke to tell user.');
455
460
  *
456
- * const structuredLlm = llm.withStructuredOutput(Joke, { name: "Joke" });
461
+ * const structuredLlm = llm.withStructuredOutput(Joke, {
462
+ * name: "Joke",
463
+ * strict: true, // Optionally enable OpenAI structured outputs
464
+ * });
457
465
  * const jokeResult = await structuredLlm.invoke("Tell me a joke about cats");
458
466
  * console.log(jokeResult);
459
467
  * ```
@@ -1100,20 +1108,6 @@ export class ChatOpenAI extends BaseChatModel {
1100
1108
  stream: true,
1101
1109
  };
1102
1110
  let defaultRole;
1103
- if (params.response_format &&
1104
- params.response_format.type === "json_schema") {
1105
- console.warn(`OpenAI does not yet support streaming with "response_format" set to "json_schema". Falling back to non-streaming mode.`);
1106
- const res = await this._generate(messages, options, runManager);
1107
- const chunk = new ChatGenerationChunk({
1108
- message: new AIMessageChunk({
1109
- ...res.generations[0].message,
1110
- }),
1111
- text: res.generations[0].text,
1112
- generationInfo: res.generations[0].generationInfo,
1113
- });
1114
- yield chunk;
1115
- return runManager?.handleLLMNewToken(res.generations[0].text ?? "", undefined, undefined, undefined, undefined, { chunk });
1116
- }
1117
1111
  const streamIterable = await this.completionWithRetry(params, options);
1118
1112
  let usage;
1119
1113
  for await (const data of streamIterable) {
@@ -1161,6 +1155,9 @@ export class ChatOpenAI extends BaseChatModel {
1161
1155
  const generationChunk = new ChatGenerationChunk({
1162
1156
  message: new AIMessageChunk({
1163
1157
  content: "",
1158
+ response_metadata: {
1159
+ usage: { ...usage },
1160
+ },
1164
1161
  usage_metadata: {
1165
1162
  input_tokens: usage.prompt_tokens,
1166
1163
  output_tokens: usage.completion_tokens,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@langchain/openai",
3
- "version": "0.3.3",
3
+ "version": "0.3.5",
4
4
  "description": "OpenAI integrations for LangChain.js",
5
5
  "type": "module",
6
6
  "engines": {