@langchain/openai 0.6.3 → 0.6.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.
@@ -1041,11 +1041,24 @@ class ChatOpenAIResponses extends BaseChatOpenAI {
1041
1041
  : (() => {
1042
1042
  const formatted = (0, openai_js_1.formatToOpenAIToolChoice)(options?.tool_choice);
1043
1043
  if (typeof formatted === "object" && "type" in formatted) {
1044
- return { type: "function", name: formatted.function.name };
1045
- }
1046
- else {
1047
- return undefined;
1044
+ if (formatted.type === "function") {
1045
+ return { type: "function", name: formatted.function.name };
1046
+ }
1047
+ else if (formatted.type === "allowed_tools") {
1048
+ return {
1049
+ type: "allowed_tools",
1050
+ mode: formatted.allowed_tools.mode,
1051
+ tools: formatted.allowed_tools.tools,
1052
+ };
1053
+ }
1054
+ else if (formatted.type === "custom") {
1055
+ return {
1056
+ type: "custom",
1057
+ name: formatted.custom.name,
1058
+ };
1059
+ }
1048
1060
  }
1061
+ return undefined;
1049
1062
  })(),
1050
1063
  text: (() => {
1051
1064
  if (options?.text)
@@ -1125,7 +1138,7 @@ class ChatOpenAIResponses extends BaseChatOpenAI {
1125
1138
  };
1126
1139
  }
1127
1140
  }
1128
- async *_streamResponseChunks(messages, options) {
1141
+ async *_streamResponseChunks(messages, options, runManager) {
1129
1142
  const streamIterable = await this.completionWithRetry({
1130
1143
  ...this.invocationParams(options),
1131
1144
  input: this._convertMessagesToResponsesParams(messages),
@@ -1136,6 +1149,10 @@ class ChatOpenAIResponses extends BaseChatOpenAI {
1136
1149
  if (chunk == null)
1137
1150
  continue;
1138
1151
  yield chunk;
1152
+ await runManager?.handleLLMNewToken(chunk.text || "", {
1153
+ prompt: options.promptIndex ?? 0,
1154
+ completion: 0,
1155
+ }, undefined, undefined, undefined, { chunk });
1139
1156
  }
1140
1157
  }
1141
1158
  async completionWithRetry(request, requestOptions) {
@@ -1258,7 +1275,7 @@ class ChatOpenAIResponses extends BaseChatOpenAI {
1258
1275
  index: chunk.content_index,
1259
1276
  });
1260
1277
  }
1261
- else if (chunk.type === "response.output_text_annotation.added") {
1278
+ else if (chunk.type === "response.output_text.annotation.added") {
1262
1279
  content.push({
1263
1280
  type: "text",
1264
1281
  text: "",
@@ -2619,6 +2636,12 @@ class ChatOpenAI extends BaseChatOpenAI {
2619
2636
  }
2620
2637
  constructor(fields) {
2621
2638
  super(fields);
2639
+ Object.defineProperty(this, "fields", {
2640
+ enumerable: true,
2641
+ configurable: true,
2642
+ writable: true,
2643
+ value: fields
2644
+ });
2622
2645
  /**
2623
2646
  * Whether to use the responses API for all requests. If `false` the responses API will be used
2624
2647
  * only when required in order to fulfill the request.
@@ -2678,14 +2701,15 @@ class ChatOpenAI extends BaseChatOpenAI {
2678
2701
  }
2679
2702
  async *_streamResponseChunks(messages, options, runManager) {
2680
2703
  if (this._useResponsesApi(options)) {
2681
- yield* this.responses._streamResponseChunks(messages, this._combineCallOptions(options));
2704
+ yield* this.responses._streamResponseChunks(messages, this._combineCallOptions(options), runManager);
2682
2705
  return;
2683
2706
  }
2684
2707
  yield* this.completions._streamResponseChunks(messages, this._combineCallOptions(options), runManager);
2685
2708
  }
2686
2709
  withConfig(config) {
2687
- this.defaultOptions = { ...this.defaultOptions, ...config };
2688
- return this;
2710
+ const newModel = new ChatOpenAI(this.fields);
2711
+ newModel.defaultOptions = { ...this.defaultOptions, ...config };
2712
+ return newModel;
2689
2713
  }
2690
2714
  }
2691
2715
  exports.ChatOpenAI = ChatOpenAI;
@@ -271,7 +271,7 @@ type ChatResponsesInvocationParams = Omit<OpenAIClient.Responses.ResponseCreateP
271
271
  export declare class ChatOpenAIResponses<CallOptions extends ChatOpenAIResponsesCallOptions = ChatOpenAIResponsesCallOptions> extends BaseChatOpenAI<CallOptions> {
272
272
  invocationParams(options?: this["ParsedCallOptions"]): ChatResponsesInvocationParams;
273
273
  _generate(messages: BaseMessage[], options: this["ParsedCallOptions"]): Promise<ChatResult>;
274
- _streamResponseChunks(messages: BaseMessage[], options: this["ParsedCallOptions"]): AsyncGenerator<ChatGenerationChunk>;
274
+ _streamResponseChunks(messages: BaseMessage[], options: this["ParsedCallOptions"], runManager?: CallbackManagerForLLMRun): AsyncGenerator<ChatGenerationChunk>;
275
275
  /**
276
276
  * Calls the Responses API with retry logic in case of failures.
277
277
  * @param request The request to send to the OpenAI API.
@@ -285,7 +285,7 @@ export declare class ChatOpenAIResponses<CallOptions extends ChatOpenAIResponses
285
285
  /** @internal */
286
286
  protected _convertResponsesDeltaToBaseMessageChunk(chunk: OpenAIClient.Responses.ResponseStreamEvent): ChatGenerationChunk | null;
287
287
  /** @internal */
288
- protected _convertMessagesToResponsesParams(messages: BaseMessage[]): (OpenAIClient.Responses.ResponseReasoningItem | OpenAIClient.Responses.EasyInputMessage | OpenAIClient.Responses.ResponseInputItem.Message | OpenAIClient.Responses.ResponseOutputMessage | OpenAIClient.Responses.ResponseFileSearchToolCall | OpenAIClient.Responses.ResponseComputerToolCall | OpenAIClient.Responses.ResponseInputItem.ComputerCallOutput | OpenAIClient.Responses.ResponseFunctionWebSearch | OpenAIClient.Responses.ResponseFunctionToolCall | OpenAIClient.Responses.ResponseInputItem.FunctionCallOutput | OpenAIClient.Responses.ResponseInputItem.ImageGenerationCall | OpenAIClient.Responses.ResponseCodeInterpreterToolCall | OpenAIClient.Responses.ResponseInputItem.LocalShellCall | OpenAIClient.Responses.ResponseInputItem.LocalShellCallOutput | OpenAIClient.Responses.ResponseInputItem.McpListTools | OpenAIClient.Responses.ResponseInputItem.McpApprovalRequest | OpenAIClient.Responses.ResponseInputItem.McpApprovalResponse | OpenAIClient.Responses.ResponseInputItem.McpCall | OpenAIClient.Responses.ResponseInputItem.ItemReference)[];
288
+ protected _convertMessagesToResponsesParams(messages: BaseMessage[]): (OpenAIClient.Responses.ResponseReasoningItem | OpenAIClient.Responses.EasyInputMessage | OpenAIClient.Responses.ResponseInputItem.Message | OpenAIClient.Responses.ResponseOutputMessage | OpenAIClient.Responses.ResponseFileSearchToolCall | OpenAIClient.Responses.ResponseComputerToolCall | OpenAIClient.Responses.ResponseInputItem.ComputerCallOutput | OpenAIClient.Responses.ResponseFunctionWebSearch | OpenAIClient.Responses.ResponseFunctionToolCall | OpenAIClient.Responses.ResponseInputItem.FunctionCallOutput | OpenAIClient.Responses.ResponseInputItem.ImageGenerationCall | OpenAIClient.Responses.ResponseCodeInterpreterToolCall | OpenAIClient.Responses.ResponseInputItem.LocalShellCall | OpenAIClient.Responses.ResponseInputItem.LocalShellCallOutput | OpenAIClient.Responses.ResponseInputItem.McpListTools | OpenAIClient.Responses.ResponseInputItem.McpApprovalRequest | OpenAIClient.Responses.ResponseInputItem.McpApprovalResponse | OpenAIClient.Responses.ResponseInputItem.McpCall | OpenAIClient.Responses.ResponseCustomToolCallOutput | OpenAIClient.Responses.ResponseCustomToolCall | OpenAIClient.Responses.ResponseInputItem.ItemReference)[];
289
289
  /** @internal */
290
290
  protected _convertReasoningSummary(reasoning: ChatOpenAIReasoningSummary): OpenAIClient.Responses.ResponseReasoningItem;
291
291
  /** @internal */
@@ -872,6 +872,7 @@ export interface ChatOpenAIFields extends BaseChatOpenAIFields {
872
872
  * <br />
873
873
  */
874
874
  export declare class ChatOpenAI<CallOptions extends ChatOpenAICallOptions = ChatOpenAICallOptions> extends BaseChatOpenAI<CallOptions> {
875
+ protected fields?: ChatOpenAIFields | undefined;
875
876
  /**
876
877
  * Whether to use the responses API for all requests. If `false` the responses API will be used
877
878
  * only when required in order to fulfill the request.
@@ -880,7 +881,7 @@ export declare class ChatOpenAI<CallOptions extends ChatOpenAICallOptions = Chat
880
881
  protected responses: ChatOpenAIResponses;
881
882
  protected completions: ChatOpenAICompletions;
882
883
  get lc_serializable_keys(): string[];
883
- constructor(fields?: ChatOpenAIFields);
884
+ constructor(fields?: ChatOpenAIFields | undefined);
884
885
  protected _useResponsesApi(options: this["ParsedCallOptions"] | undefined): boolean;
885
886
  getLsParams(options: this["ParsedCallOptions"]): LangSmithParams;
886
887
  invocationParams(options?: this["ParsedCallOptions"]): ChatResponsesInvocationParams | ChatCompletionsInvocationParams;
@@ -1035,11 +1035,24 @@ export class ChatOpenAIResponses extends BaseChatOpenAI {
1035
1035
  : (() => {
1036
1036
  const formatted = formatToOpenAIToolChoice(options?.tool_choice);
1037
1037
  if (typeof formatted === "object" && "type" in formatted) {
1038
- return { type: "function", name: formatted.function.name };
1039
- }
1040
- else {
1041
- return undefined;
1038
+ if (formatted.type === "function") {
1039
+ return { type: "function", name: formatted.function.name };
1040
+ }
1041
+ else if (formatted.type === "allowed_tools") {
1042
+ return {
1043
+ type: "allowed_tools",
1044
+ mode: formatted.allowed_tools.mode,
1045
+ tools: formatted.allowed_tools.tools,
1046
+ };
1047
+ }
1048
+ else if (formatted.type === "custom") {
1049
+ return {
1050
+ type: "custom",
1051
+ name: formatted.custom.name,
1052
+ };
1053
+ }
1042
1054
  }
1055
+ return undefined;
1043
1056
  })(),
1044
1057
  text: (() => {
1045
1058
  if (options?.text)
@@ -1119,7 +1132,7 @@ export class ChatOpenAIResponses extends BaseChatOpenAI {
1119
1132
  };
1120
1133
  }
1121
1134
  }
1122
- async *_streamResponseChunks(messages, options) {
1135
+ async *_streamResponseChunks(messages, options, runManager) {
1123
1136
  const streamIterable = await this.completionWithRetry({
1124
1137
  ...this.invocationParams(options),
1125
1138
  input: this._convertMessagesToResponsesParams(messages),
@@ -1130,6 +1143,10 @@ export class ChatOpenAIResponses extends BaseChatOpenAI {
1130
1143
  if (chunk == null)
1131
1144
  continue;
1132
1145
  yield chunk;
1146
+ await runManager?.handleLLMNewToken(chunk.text || "", {
1147
+ prompt: options.promptIndex ?? 0,
1148
+ completion: 0,
1149
+ }, undefined, undefined, undefined, { chunk });
1133
1150
  }
1134
1151
  }
1135
1152
  async completionWithRetry(request, requestOptions) {
@@ -1252,7 +1269,7 @@ export class ChatOpenAIResponses extends BaseChatOpenAI {
1252
1269
  index: chunk.content_index,
1253
1270
  });
1254
1271
  }
1255
- else if (chunk.type === "response.output_text_annotation.added") {
1272
+ else if (chunk.type === "response.output_text.annotation.added") {
1256
1273
  content.push({
1257
1274
  type: "text",
1258
1275
  text: "",
@@ -2611,6 +2628,12 @@ export class ChatOpenAI extends BaseChatOpenAI {
2611
2628
  }
2612
2629
  constructor(fields) {
2613
2630
  super(fields);
2631
+ Object.defineProperty(this, "fields", {
2632
+ enumerable: true,
2633
+ configurable: true,
2634
+ writable: true,
2635
+ value: fields
2636
+ });
2614
2637
  /**
2615
2638
  * Whether to use the responses API for all requests. If `false` the responses API will be used
2616
2639
  * only when required in order to fulfill the request.
@@ -2670,13 +2693,14 @@ export class ChatOpenAI extends BaseChatOpenAI {
2670
2693
  }
2671
2694
  async *_streamResponseChunks(messages, options, runManager) {
2672
2695
  if (this._useResponsesApi(options)) {
2673
- yield* this.responses._streamResponseChunks(messages, this._combineCallOptions(options));
2696
+ yield* this.responses._streamResponseChunks(messages, this._combineCallOptions(options), runManager);
2674
2697
  return;
2675
2698
  }
2676
2699
  yield* this.completions._streamResponseChunks(messages, this._combineCallOptions(options), runManager);
2677
2700
  }
2678
2701
  withConfig(config) {
2679
- this.defaultOptions = { ...this.defaultOptions, ...config };
2680
- return this;
2702
+ const newModel = new ChatOpenAI(this.fields);
2703
+ newModel.defaultOptions = { ...this.defaultOptions, ...config };
2704
+ return newModel;
2681
2705
  }
2682
2706
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@langchain/openai",
3
- "version": "0.6.3",
3
+ "version": "0.6.5",
4
4
  "description": "OpenAI integrations for LangChain.js",
5
5
  "type": "module",
6
6
  "engines": {
@@ -36,7 +36,7 @@
36
36
  "license": "MIT",
37
37
  "dependencies": {
38
38
  "js-tiktoken": "^1.0.12",
39
- "openai": "^5.3.0",
39
+ "openai": "^5.12.1",
40
40
  "zod": "^3.25.32"
41
41
  },
42
42
  "peerDependencies": {