@langchain/openai 0.0.16 → 0.0.17

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,7 +718,24 @@ class ChatOpenAI extends chat_models_1.BaseChatModel {
718
718
  },
719
719
  });
720
720
  }
721
- withStructuredOutput({ schema, name, method, includeRaw, }) {
721
+ withStructuredOutput(outputSchema, config) {
722
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
723
+ let schema;
724
+ let name;
725
+ let method;
726
+ let includeRaw;
727
+ if (isStructuredOutputMethodParams(outputSchema)) {
728
+ schema = outputSchema.schema;
729
+ name = outputSchema.name;
730
+ method = outputSchema.method;
731
+ includeRaw = outputSchema.includeRaw;
732
+ }
733
+ else {
734
+ schema = outputSchema;
735
+ name = config?.name;
736
+ method = config?.method;
737
+ includeRaw = config?.includeRaw;
738
+ }
722
739
  let llm;
723
740
  let outputParser;
724
741
  if (method === "jsonMode") {
@@ -806,3 +823,11 @@ function isZodSchema(input) {
806
823
  // Check for a characteristic method of Zod schemas
807
824
  return typeof input?.parse === "function";
808
825
  }
826
+ function isStructuredOutputMethodParams(x
827
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
828
+ ) {
829
+ return (x !== undefined &&
830
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
831
+ typeof x.schema ===
832
+ "object");
833
+ }
@@ -4,7 +4,8 @@ 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, StructuredOutputMethodParams } from "@langchain/core/language_models/base";
7
+ import type { BaseFunctionCallOptions, BaseLanguageModelInput, StructuredOutputMethodOptions, StructuredOutputMethodParams } from "@langchain/core/language_models/base";
8
+ import { z } from "zod";
8
9
  import { Runnable } from "@langchain/core/runnables";
9
10
  import type { AzureOpenAIInput, OpenAICallOptions, OpenAIChatInput, OpenAICoreRequestOptions, LegacyOpenAIInput } from "./types.js";
10
11
  export type { AzureOpenAIInput, OpenAICallOptions, OpenAIChatInput };
@@ -141,8 +142,8 @@ export declare class ChatOpenAI<CallOptions extends ChatOpenAICallOptions = Chat
141
142
  _llmType(): string;
142
143
  /** @ignore */
143
144
  _combineLLMOutput(...llmOutputs: OpenAILLMOutput[]): OpenAILLMOutput;
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, {
145
+ withStructuredOutput<RunOutput extends Record<string, any> = Record<string, any>>(outputSchema: StructuredOutputMethodParams<RunOutput, false> | z.ZodType<RunOutput> | Record<string, any>, config?: StructuredOutputMethodOptions<false>): Runnable<BaseLanguageModelInput, RunOutput>;
146
+ withStructuredOutput<RunOutput extends Record<string, any> = Record<string, any>>(outputSchema: StructuredOutputMethodParams<RunOutput, true> | z.ZodType<RunOutput> | Record<string, any>, config?: StructuredOutputMethodOptions<true>): Runnable<BaseLanguageModelInput, {
146
147
  raw: BaseMessage;
147
148
  parsed: RunOutput;
148
149
  }>;
@@ -714,7 +714,24 @@ export class ChatOpenAI extends BaseChatModel {
714
714
  },
715
715
  });
716
716
  }
717
- withStructuredOutput({ schema, name, method, includeRaw, }) {
717
+ withStructuredOutput(outputSchema, config) {
718
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
719
+ let schema;
720
+ let name;
721
+ let method;
722
+ let includeRaw;
723
+ if (isStructuredOutputMethodParams(outputSchema)) {
724
+ schema = outputSchema.schema;
725
+ name = outputSchema.name;
726
+ method = outputSchema.method;
727
+ includeRaw = outputSchema.includeRaw;
728
+ }
729
+ else {
730
+ schema = outputSchema;
731
+ name = config?.name;
732
+ method = config?.method;
733
+ includeRaw = config?.includeRaw;
734
+ }
718
735
  let llm;
719
736
  let outputParser;
720
737
  if (method === "jsonMode") {
@@ -801,3 +818,11 @@ function isZodSchema(input) {
801
818
  // Check for a characteristic method of Zod schemas
802
819
  return typeof input?.parse === "function";
803
820
  }
821
+ function isStructuredOutputMethodParams(x
822
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
823
+ ) {
824
+ return (x !== undefined &&
825
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
826
+ typeof x.schema ===
827
+ "object");
828
+ }
@@ -641,8 +641,7 @@ describe("ChatOpenAI withStructuredOutput", () => {
641
641
  number1: z.number(),
642
642
  number2: z.number(),
643
643
  });
644
- const modelWithStructuredOutput = model.withStructuredOutput({
645
- schema: calculatorSchema,
644
+ const modelWithStructuredOutput = model.withStructuredOutput(calculatorSchema, {
646
645
  name: "calculator",
647
646
  });
648
647
  const prompt = ChatPromptTemplate.fromMessages([
@@ -668,8 +667,7 @@ describe("ChatOpenAI withStructuredOutput", () => {
668
667
  number1: z.number(),
669
668
  number2: z.number(),
670
669
  });
671
- const modelWithStructuredOutput = model.withStructuredOutput({
672
- schema: calculatorSchema,
670
+ const modelWithStructuredOutput = model.withStructuredOutput(calculatorSchema, {
673
671
  name: "calculator",
674
672
  method: "jsonMode",
675
673
  });
@@ -728,8 +726,7 @@ describe("ChatOpenAI withStructuredOutput", () => {
728
726
  number1: z.number(),
729
727
  number2: z.number(),
730
728
  });
731
- const modelWithStructuredOutput = model.withStructuredOutput({
732
- schema: zodToJsonSchema(calculatorSchema),
729
+ const modelWithStructuredOutput = model.withStructuredOutput(zodToJsonSchema(calculatorSchema), {
733
730
  name: "calculator",
734
731
  method: "jsonMode",
735
732
  });
@@ -761,8 +758,7 @@ describe("ChatOpenAI withStructuredOutput", () => {
761
758
  number1: z.number(),
762
759
  number2: z.number(),
763
760
  });
764
- const modelWithStructuredOutput = model.withStructuredOutput({
765
- schema: calculatorSchema,
761
+ const modelWithStructuredOutput = model.withStructuredOutput(calculatorSchema, {
766
762
  name: "calculator",
767
763
  includeRaw: true,
768
764
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@langchain/openai",
3
- "version": "0.0.16",
3
+ "version": "0.0.17",
4
4
  "description": "OpenAI integrations for LangChain.js",
5
5
  "type": "module",
6
6
  "engines": {