@langchain/openai 0.6.0 → 0.6.2

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.
@@ -531,6 +531,7 @@ class AzureChatOpenAI extends chat_models_js_1.ChatOpenAI {
531
531
  params.ls_provider = "azure";
532
532
  return params;
533
533
  }
534
+ /** @internal */
534
535
  _getClientOptions(options) {
535
536
  if (!this.client) {
536
537
  const openAIEndpointConfig = {
@@ -442,7 +442,8 @@ export declare class AzureChatOpenAI extends ChatOpenAI {
442
442
  configuration?: ClientOptions;
443
443
  });
444
444
  getLsParams(options: this["ParsedCallOptions"]): LangSmithParams;
445
- protected _getClientOptions(options: OpenAICoreRequestOptions | undefined): OpenAICoreRequestOptions;
445
+ /** @internal */
446
+ _getClientOptions(options: OpenAICoreRequestOptions | undefined): OpenAICoreRequestOptions;
446
447
  toJSON(): any;
447
448
  withStructuredOutput<RunOutput extends Record<string, any> = Record<string, any>>(outputSchema: InteropZodType<RunOutput> | Record<string, any>, config?: StructuredOutputMethodOptions<false>): Runnable<BaseLanguageModelInput, RunOutput>;
448
449
  withStructuredOutput<RunOutput extends Record<string, any> = Record<string, any>>(outputSchema: InteropZodType<RunOutput> | Record<string, any>, config?: StructuredOutputMethodOptions<true>): Runnable<BaseLanguageModelInput, {
@@ -528,6 +528,7 @@ export class AzureChatOpenAI extends ChatOpenAI {
528
528
  params.ls_provider = "azure";
529
529
  return params;
530
530
  }
531
+ /** @internal */
531
532
  _getClientOptions(options) {
532
533
  if (!this.client) {
533
534
  const openAIEndpointConfig = {
@@ -288,7 +288,7 @@ class BaseChatOpenAI extends chat_models_1.BaseChatModel {
288
288
  get lc_aliases() {
289
289
  return {
290
290
  apiKey: "openai_api_key",
291
- model: "model_name",
291
+ modelName: "model",
292
292
  };
293
293
  }
294
294
  get lc_serializable_keys() {
@@ -311,6 +311,7 @@ class BaseChatOpenAI extends chat_models_1.BaseChatModel {
311
311
  "streaming",
312
312
  "streamUsage",
313
313
  "model",
314
+ "modelName",
314
315
  "modelKwargs",
315
316
  "stop",
316
317
  "stopSequences",
@@ -542,6 +543,12 @@ class BaseChatOpenAI extends chat_models_1.BaseChatModel {
542
543
  writable: true,
543
544
  value: void 0
544
545
  });
546
+ Object.defineProperty(this, "defaultOptions", {
547
+ enumerable: true,
548
+ configurable: true,
549
+ writable: true,
550
+ value: void 0
551
+ });
545
552
  Object.defineProperty(this, "lc_serializable", {
546
553
  enumerable: true,
547
554
  configurable: true,
@@ -636,6 +643,13 @@ class BaseChatOpenAI extends chat_models_1.BaseChatModel {
636
643
  }
637
644
  return resFormat;
638
645
  }
646
+ _combineCallOptions(additionalOptions) {
647
+ return {
648
+ ...this.defaultOptions,
649
+ ...(additionalOptions ?? {}),
650
+ };
651
+ }
652
+ /** @internal */
639
653
  _getClientOptions(options) {
640
654
  if (!this.client) {
641
655
  const openAIEndpointConfig = {
@@ -690,6 +704,12 @@ class BaseChatOpenAI extends chat_models_1.BaseChatModel {
690
704
  ...kwargs,
691
705
  });
692
706
  }
707
+ async stream(input, options) {
708
+ return super.stream(input, this._combineCallOptions(options));
709
+ }
710
+ async invoke(input, options) {
711
+ return super.invoke(input, this._combineCallOptions(options));
712
+ }
693
713
  /** @ignore */
694
714
  _combineLLMOutput(...llmOutputs) {
695
715
  return llmOutputs.reduce((acc, llmOutput) => {
@@ -2615,6 +2635,9 @@ class ChatOpenAI extends BaseChatOpenAI {
2615
2635
  this.useResponsesApi = fields?.useResponsesApi ?? false;
2616
2636
  this.responses = new ChatOpenAIResponses(fields);
2617
2637
  this.completions = new ChatOpenAICompletions(fields);
2638
+ // Override `_getClientOptions` so sub classes of ChatOpenAI use the same client config
2639
+ this.responses._getClientOptions = this._getClientOptions;
2640
+ this.completions._getClientOptions = this._getClientOptions;
2618
2641
  }
2619
2642
  _useResponsesApi(options) {
2620
2643
  const usesBuiltInTools = options?.tools?.some(isBuiltInTool);
@@ -2626,6 +2649,20 @@ class ChatOpenAI extends BaseChatOpenAI {
2626
2649
  this.reasoning?.summary != null;
2627
2650
  return this.useResponsesApi || usesBuiltInTools || hasResponsesOnlyKwargs;
2628
2651
  }
2652
+ getLsParams(options) {
2653
+ const optionsWithDefaults = this._combineCallOptions(options);
2654
+ if (this._useResponsesApi(options)) {
2655
+ return this.responses.getLsParams(optionsWithDefaults);
2656
+ }
2657
+ return this.completions.getLsParams(optionsWithDefaults);
2658
+ }
2659
+ invocationParams(options) {
2660
+ const optionsWithDefaults = this._combineCallOptions(options);
2661
+ if (this._useResponsesApi(options)) {
2662
+ return this.responses.invocationParams(optionsWithDefaults);
2663
+ }
2664
+ return this.completions.invocationParams(optionsWithDefaults);
2665
+ }
2629
2666
  /** @ignore */
2630
2667
  async _generate(messages, options, runManager) {
2631
2668
  if (this._useResponsesApi(options)) {
@@ -2635,33 +2672,14 @@ class ChatOpenAI extends BaseChatOpenAI {
2635
2672
  }
2636
2673
  async *_streamResponseChunks(messages, options, runManager) {
2637
2674
  if (this._useResponsesApi(options)) {
2638
- yield* this.responses._streamResponseChunks(messages, options);
2675
+ yield* this.responses._streamResponseChunks(messages, this._combineCallOptions(options));
2639
2676
  return;
2640
2677
  }
2641
- yield* this.completions._streamResponseChunks(messages, options, runManager);
2678
+ yield* this.completions._streamResponseChunks(messages, this._combineCallOptions(options), runManager);
2642
2679
  }
2643
2680
  withConfig(config) {
2644
- // FIXME: assigning additional config options to the inner chat classes this way
2645
- // is awkward, but it's the only way to preserve the original object identity
2646
- // and still thread config options, which is important since this class is a "proxy"
2647
- // for the inner chat classes. This will be fixed in a later version of langchain
2648
- // when the core runnable interface is improved (0.4) to support this out of the box.
2649
- const bindChatOpenAIConfig = (cls, config) => {
2650
- const oldGenerate = cls._generate;
2651
- const oldStreamResponseChunks = cls._streamResponseChunks;
2652
- return Object.assign(cls, {
2653
- _generate(messages, options, runManager) {
2654
- return oldGenerate.call(cls, messages, { ...options, ...config }, runManager);
2655
- },
2656
- _streamResponseChunks(messages, options, runManager) {
2657
- return oldStreamResponseChunks.call(cls, messages, { ...options, ...config }, runManager);
2658
- },
2659
- });
2660
- };
2661
- this.responses = bindChatOpenAIConfig(this.responses, config);
2662
- this.completions = bindChatOpenAIConfig(this.completions, config);
2663
- // Proxy chat class is also bound for `_useResponsesApi`,
2664
- return bindChatOpenAIConfig(this, config);
2681
+ this.defaultOptions = { ...this.defaultOptions, ...config };
2682
+ return this;
2665
2683
  }
2666
2684
  }
2667
2685
  exports.ChatOpenAI = ChatOpenAI;
@@ -168,6 +168,7 @@ declare abstract class BaseChatOpenAI<CallOptions extends BaseChatOpenAICallOpti
168
168
  * Specifies the service tier for prioritization and latency optimization.
169
169
  */
170
170
  service_tier?: OpenAIClient.Chat.ChatCompletionCreateParams["service_tier"];
171
+ protected defaultOptions: CallOptions;
171
172
  _llmType(): string;
172
173
  static lc_name(): string;
173
174
  get callKeys(): string[];
@@ -199,11 +200,15 @@ declare abstract class BaseChatOpenAI<CallOptions extends BaseChatOpenAICallOpti
199
200
  * @internal
200
201
  */
201
202
  protected _getResponseFormat(resFormat?: CallOptions["response_format"]): ResponseFormatText | ResponseFormatJSONObject | ResponseFormatJSONSchema | undefined;
202
- protected _getClientOptions(options: OpenAICoreRequestOptions | undefined): OpenAICoreRequestOptions;
203
+ protected _combineCallOptions(additionalOptions?: this["ParsedCallOptions"]): this["ParsedCallOptions"];
204
+ /** @internal */
205
+ _getClientOptions(options: OpenAICoreRequestOptions | undefined): OpenAICoreRequestOptions;
203
206
  protected _convertChatOpenAIToolToCompletionsTool(tool: ChatOpenAIToolType, fields?: {
204
207
  strict?: boolean;
205
208
  }): OpenAIClient.ChatCompletionTool;
206
209
  bindTools(tools: ChatOpenAIToolType[], kwargs?: Partial<CallOptions>): Runnable<BaseLanguageModelInput, AIMessageChunk, CallOptions>;
210
+ stream(input: BaseLanguageModelInput, options?: CallOptions): Promise<import("@langchain/core/utils/stream").IterableReadableStream<AIMessageChunk>>;
211
+ invoke(input: BaseLanguageModelInput, options?: CallOptions): Promise<AIMessageChunk>;
207
212
  /** @ignore */
208
213
  _combineLLMOutput(...llmOutputs: OpenAILLMOutput[]): OpenAILLMOutput;
209
214
  getNumTokensFromMessages(messages: BaseMessage[]): Promise<{
@@ -863,6 +868,8 @@ export declare class ChatOpenAI<CallOptions extends ChatOpenAICallOptions = Chat
863
868
  get lc_serializable_keys(): string[];
864
869
  constructor(fields?: ChatOpenAIFields);
865
870
  protected _useResponsesApi(options: this["ParsedCallOptions"] | undefined): boolean;
871
+ getLsParams(options: this["ParsedCallOptions"]): LangSmithParams;
872
+ invocationParams(options?: this["ParsedCallOptions"]): ChatResponsesInvocationParams | ChatCompletionsInvocationParams;
866
873
  /** @ignore */
867
874
  _generate(messages: BaseMessage[], options: this["ParsedCallOptions"], runManager?: CallbackManagerForLLMRun): Promise<ChatResult>;
868
875
  _streamResponseChunks(messages: BaseMessage[], options: this["ParsedCallOptions"], runManager?: CallbackManagerForLLMRun): AsyncGenerator<ChatGenerationChunk>;
@@ -283,7 +283,7 @@ class BaseChatOpenAI extends BaseChatModel {
283
283
  get lc_aliases() {
284
284
  return {
285
285
  apiKey: "openai_api_key",
286
- model: "model_name",
286
+ modelName: "model",
287
287
  };
288
288
  }
289
289
  get lc_serializable_keys() {
@@ -306,6 +306,7 @@ class BaseChatOpenAI extends BaseChatModel {
306
306
  "streaming",
307
307
  "streamUsage",
308
308
  "model",
309
+ "modelName",
309
310
  "modelKwargs",
310
311
  "stop",
311
312
  "stopSequences",
@@ -537,6 +538,12 @@ class BaseChatOpenAI extends BaseChatModel {
537
538
  writable: true,
538
539
  value: void 0
539
540
  });
541
+ Object.defineProperty(this, "defaultOptions", {
542
+ enumerable: true,
543
+ configurable: true,
544
+ writable: true,
545
+ value: void 0
546
+ });
540
547
  Object.defineProperty(this, "lc_serializable", {
541
548
  enumerable: true,
542
549
  configurable: true,
@@ -631,6 +638,13 @@ class BaseChatOpenAI extends BaseChatModel {
631
638
  }
632
639
  return resFormat;
633
640
  }
641
+ _combineCallOptions(additionalOptions) {
642
+ return {
643
+ ...this.defaultOptions,
644
+ ...(additionalOptions ?? {}),
645
+ };
646
+ }
647
+ /** @internal */
634
648
  _getClientOptions(options) {
635
649
  if (!this.client) {
636
650
  const openAIEndpointConfig = {
@@ -685,6 +699,12 @@ class BaseChatOpenAI extends BaseChatModel {
685
699
  ...kwargs,
686
700
  });
687
701
  }
702
+ async stream(input, options) {
703
+ return super.stream(input, this._combineCallOptions(options));
704
+ }
705
+ async invoke(input, options) {
706
+ return super.invoke(input, this._combineCallOptions(options));
707
+ }
688
708
  /** @ignore */
689
709
  _combineLLMOutput(...llmOutputs) {
690
710
  return llmOutputs.reduce((acc, llmOutput) => {
@@ -2608,6 +2628,9 @@ export class ChatOpenAI extends BaseChatOpenAI {
2608
2628
  this.useResponsesApi = fields?.useResponsesApi ?? false;
2609
2629
  this.responses = new ChatOpenAIResponses(fields);
2610
2630
  this.completions = new ChatOpenAICompletions(fields);
2631
+ // Override `_getClientOptions` so sub classes of ChatOpenAI use the same client config
2632
+ this.responses._getClientOptions = this._getClientOptions;
2633
+ this.completions._getClientOptions = this._getClientOptions;
2611
2634
  }
2612
2635
  _useResponsesApi(options) {
2613
2636
  const usesBuiltInTools = options?.tools?.some(isBuiltInTool);
@@ -2619,6 +2642,20 @@ export class ChatOpenAI extends BaseChatOpenAI {
2619
2642
  this.reasoning?.summary != null;
2620
2643
  return this.useResponsesApi || usesBuiltInTools || hasResponsesOnlyKwargs;
2621
2644
  }
2645
+ getLsParams(options) {
2646
+ const optionsWithDefaults = this._combineCallOptions(options);
2647
+ if (this._useResponsesApi(options)) {
2648
+ return this.responses.getLsParams(optionsWithDefaults);
2649
+ }
2650
+ return this.completions.getLsParams(optionsWithDefaults);
2651
+ }
2652
+ invocationParams(options) {
2653
+ const optionsWithDefaults = this._combineCallOptions(options);
2654
+ if (this._useResponsesApi(options)) {
2655
+ return this.responses.invocationParams(optionsWithDefaults);
2656
+ }
2657
+ return this.completions.invocationParams(optionsWithDefaults);
2658
+ }
2622
2659
  /** @ignore */
2623
2660
  async _generate(messages, options, runManager) {
2624
2661
  if (this._useResponsesApi(options)) {
@@ -2628,32 +2665,13 @@ export class ChatOpenAI extends BaseChatOpenAI {
2628
2665
  }
2629
2666
  async *_streamResponseChunks(messages, options, runManager) {
2630
2667
  if (this._useResponsesApi(options)) {
2631
- yield* this.responses._streamResponseChunks(messages, options);
2668
+ yield* this.responses._streamResponseChunks(messages, this._combineCallOptions(options));
2632
2669
  return;
2633
2670
  }
2634
- yield* this.completions._streamResponseChunks(messages, options, runManager);
2671
+ yield* this.completions._streamResponseChunks(messages, this._combineCallOptions(options), runManager);
2635
2672
  }
2636
2673
  withConfig(config) {
2637
- // FIXME: assigning additional config options to the inner chat classes this way
2638
- // is awkward, but it's the only way to preserve the original object identity
2639
- // and still thread config options, which is important since this class is a "proxy"
2640
- // for the inner chat classes. This will be fixed in a later version of langchain
2641
- // when the core runnable interface is improved (0.4) to support this out of the box.
2642
- const bindChatOpenAIConfig = (cls, config) => {
2643
- const oldGenerate = cls._generate;
2644
- const oldStreamResponseChunks = cls._streamResponseChunks;
2645
- return Object.assign(cls, {
2646
- _generate(messages, options, runManager) {
2647
- return oldGenerate.call(cls, messages, { ...options, ...config }, runManager);
2648
- },
2649
- _streamResponseChunks(messages, options, runManager) {
2650
- return oldStreamResponseChunks.call(cls, messages, { ...options, ...config }, runManager);
2651
- },
2652
- });
2653
- };
2654
- this.responses = bindChatOpenAIConfig(this.responses, config);
2655
- this.completions = bindChatOpenAIConfig(this.completions, config);
2656
- // Proxy chat class is also bound for `_useResponsesApi`,
2657
- return bindChatOpenAIConfig(this, config);
2674
+ this.defaultOptions = { ...this.defaultOptions, ...config };
2675
+ return this;
2658
2676
  }
2659
2677
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@langchain/openai",
3
- "version": "0.6.0",
3
+ "version": "0.6.2",
4
4
  "description": "OpenAI integrations for LangChain.js",
5
5
  "type": "module",
6
6
  "engines": {
@@ -45,7 +45,7 @@
45
45
  "devDependencies": {
46
46
  "@azure/identity": "^4.2.1",
47
47
  "@jest/globals": "^29.5.0",
48
- "@langchain/core": "0.3.62",
48
+ "@langchain/core": "workspace:*",
49
49
  "@langchain/scripts": ">=0.1.0 <0.2.0",
50
50
  "@langchain/standard-tests": "0.0.0",
51
51
  "@swc/core": "^1.3.90",
@@ -102,4 +102,4 @@
102
102
  "index.d.ts",
103
103
  "index.d.cts"
104
104
  ]
105
- }
105
+ }