@langchain/openai 0.0.15 → 0.0.16

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.
@@ -718,18 +718,7 @@ class ChatOpenAI extends chat_models_1.BaseChatModel {
718
718
  },
719
719
  });
720
720
  }
721
- /**
722
- * Model wrapper that returns outputs formatted to match the given schema.
723
- *
724
- * @template {z.ZodObject<any, any, any, any>} RunOutput The output type for the Runnable, expected to be a Zod schema object for structured output validation.
725
- *
726
- * @param {z.ZodEffects<RunOutput>} schema The schema for the structured output. Either as a Zod schema or a valid JSON schema object.
727
- * @param {string} name The name of the function to call.
728
- * @param {"functionCalling" | "jsonMode"} method The method to use for getting the structured output. Defaults to "functionCalling".
729
- * @param {boolean | undefined} includeRaw Whether to include the raw output in the result. Defaults to false.
730
- * @returns {Runnable<RunInput, RunOutput> | Runnable<RunInput, { raw: BaseMessage; parsed: RunOutput }>} A new runnable that calls the LLM with structured output.
731
- */
732
- withStructuredOutput({ schema, name, method = "functionCalling", includeRaw = false, }) {
721
+ withStructuredOutput({ schema, name, method, includeRaw, }) {
733
722
  let llm;
734
723
  let outputParser;
735
724
  if (method === "jsonMode") {
@@ -739,6 +728,7 @@ class ChatOpenAI extends chat_models_1.BaseChatModel {
739
728
  outputParser = new output_parsers_1.JsonOutputParser();
740
729
  }
741
730
  else {
731
+ const functionName = name ?? "extract";
742
732
  // Is function calling
743
733
  if (isZodSchema(schema)) {
744
734
  const asZodSchema = (0, zod_to_json_schema_1.zodToJsonSchema)(schema);
@@ -753,11 +743,16 @@ class ChatOpenAI extends chat_models_1.BaseChatModel {
753
743
  },
754
744
  },
755
745
  ],
756
- tool_choice: "auto",
746
+ tool_choice: {
747
+ type: "function",
748
+ function: {
749
+ name: functionName,
750
+ },
751
+ },
757
752
  });
758
753
  outputParser = new openai_tools_1.JsonOutputKeyToolsParser({
759
754
  returnSingle: true,
760
- keyName: name,
755
+ keyName: functionName,
761
756
  });
762
757
  }
763
758
  else {
@@ -772,11 +767,16 @@ class ChatOpenAI extends chat_models_1.BaseChatModel {
772
767
  },
773
768
  },
774
769
  ],
775
- tool_choice: "auto",
770
+ tool_choice: {
771
+ type: "function",
772
+ function: {
773
+ name,
774
+ },
775
+ },
776
776
  });
777
777
  outputParser = new openai_tools_1.JsonOutputKeyToolsParser({
778
778
  returnSingle: true,
779
- keyName: name,
779
+ keyName: functionName,
780
780
  });
781
781
  }
782
782
  }
@@ -4,8 +4,7 @@ import { type BaseMessage } from "@langchain/core/messages";
4
4
  import { ChatGenerationChunk, type ChatResult } from "@langchain/core/outputs";
5
5
  import { type StructuredToolInterface } from "@langchain/core/tools";
6
6
  import { BaseChatModel, type BaseChatModelParams } from "@langchain/core/language_models/chat_models";
7
- import type { BaseFunctionCallOptions, BaseLanguageModelInput } from "@langchain/core/language_models/base";
8
- import { z } from "zod";
7
+ import type { BaseFunctionCallOptions, BaseLanguageModelInput, StructuredOutputMethodParams } from "@langchain/core/language_models/base";
9
8
  import { Runnable } from "@langchain/core/runnables";
10
9
  import type { AzureOpenAIInput, OpenAICallOptions, OpenAIChatInput, OpenAICoreRequestOptions, LegacyOpenAIInput } from "./types.js";
11
10
  export type { AzureOpenAIInput, OpenAICallOptions, OpenAIChatInput };
@@ -142,19 +141,9 @@ export declare class ChatOpenAI<CallOptions extends ChatOpenAICallOptions = Chat
142
141
  _llmType(): string;
143
142
  /** @ignore */
144
143
  _combineLLMOutput(...llmOutputs: OpenAILLMOutput[]): OpenAILLMOutput;
145
- withStructuredOutput<RunInput = BaseLanguageModelInput, RunOutput extends z.ZodObject<any, any, any, any> = z.ZodObject<any, any, any, any>>({ schema, name, method, includeRaw, }: {
146
- schema: z.ZodEffects<RunOutput> | Record<string, any>;
147
- name: string;
148
- method?: "functionCalling" | "jsonMode";
149
- includeRaw: true;
150
- }): Runnable<RunInput, {
144
+ withStructuredOutput<RunOutput extends Record<string, any> = Record<string, any>>({ schema, name, method, includeRaw, }: StructuredOutputMethodParams<RunOutput, false>): Runnable<BaseLanguageModelInput, RunOutput>;
145
+ withStructuredOutput<RunOutput extends Record<string, any> = Record<string, any>>({ schema, name, method, includeRaw, }: StructuredOutputMethodParams<RunOutput, true>): Runnable<BaseLanguageModelInput, {
151
146
  raw: BaseMessage;
152
147
  parsed: RunOutput;
153
148
  }>;
154
- withStructuredOutput<RunInput = BaseLanguageModelInput, RunOutput extends z.ZodObject<any, any, any, any> = z.ZodObject<any, any, any, any>>({ schema, name, method, includeRaw, }: {
155
- schema: z.ZodEffects<RunOutput> | Record<string, any>;
156
- name: string;
157
- method?: "functionCalling" | "jsonMode";
158
- includeRaw?: false;
159
- }): Runnable<RunInput, RunOutput>;
160
149
  }
@@ -714,18 +714,7 @@ export class ChatOpenAI extends BaseChatModel {
714
714
  },
715
715
  });
716
716
  }
717
- /**
718
- * Model wrapper that returns outputs formatted to match the given schema.
719
- *
720
- * @template {z.ZodObject<any, any, any, any>} RunOutput The output type for the Runnable, expected to be a Zod schema object for structured output validation.
721
- *
722
- * @param {z.ZodEffects<RunOutput>} schema The schema for the structured output. Either as a Zod schema or a valid JSON schema object.
723
- * @param {string} name The name of the function to call.
724
- * @param {"functionCalling" | "jsonMode"} method The method to use for getting the structured output. Defaults to "functionCalling".
725
- * @param {boolean | undefined} includeRaw Whether to include the raw output in the result. Defaults to false.
726
- * @returns {Runnable<RunInput, RunOutput> | Runnable<RunInput, { raw: BaseMessage; parsed: RunOutput }>} A new runnable that calls the LLM with structured output.
727
- */
728
- withStructuredOutput({ schema, name, method = "functionCalling", includeRaw = false, }) {
717
+ withStructuredOutput({ schema, name, method, includeRaw, }) {
729
718
  let llm;
730
719
  let outputParser;
731
720
  if (method === "jsonMode") {
@@ -735,6 +724,7 @@ export class ChatOpenAI extends BaseChatModel {
735
724
  outputParser = new JsonOutputParser();
736
725
  }
737
726
  else {
727
+ const functionName = name ?? "extract";
738
728
  // Is function calling
739
729
  if (isZodSchema(schema)) {
740
730
  const asZodSchema = zodToJsonSchema(schema);
@@ -749,11 +739,16 @@ export class ChatOpenAI extends BaseChatModel {
749
739
  },
750
740
  },
751
741
  ],
752
- tool_choice: "auto",
742
+ tool_choice: {
743
+ type: "function",
744
+ function: {
745
+ name: functionName,
746
+ },
747
+ },
753
748
  });
754
749
  outputParser = new JsonOutputKeyToolsParser({
755
750
  returnSingle: true,
756
- keyName: name,
751
+ keyName: functionName,
757
752
  });
758
753
  }
759
754
  else {
@@ -768,11 +763,16 @@ export class ChatOpenAI extends BaseChatModel {
768
763
  },
769
764
  },
770
765
  ],
771
- tool_choice: "auto",
766
+ tool_choice: {
767
+ type: "function",
768
+ function: {
769
+ name,
770
+ },
771
+ },
772
772
  });
773
773
  outputParser = new JsonOutputKeyToolsParser({
774
774
  returnSingle: true,
775
- keyName: name,
775
+ keyName: functionName,
776
776
  });
777
777
  }
778
778
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@langchain/openai",
3
- "version": "0.0.15",
3
+ "version": "0.0.16",
4
4
  "description": "OpenAI integrations for LangChain.js",
5
5
  "type": "module",
6
6
  "engines": {
@@ -39,7 +39,7 @@
39
39
  "author": "LangChain",
40
40
  "license": "MIT",
41
41
  "dependencies": {
42
- "@langchain/core": "~0.1.39",
42
+ "@langchain/core": "~0.1.41",
43
43
  "js-tiktoken": "^1.0.7",
44
44
  "openai": "^4.26.0",
45
45
  "zod": "^3.22.4",