@langchain/openai 0.5.3 → 0.5.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.
@@ -231,34 +231,32 @@ function _convertMessagesToOpenAIResponsesParams(messages, model) {
231
231
  }
232
232
  return input;
233
233
  }
234
- if (role === "user") {
235
- return {
236
- type: "message",
237
- role: "user",
238
- content: typeof lcMsg.content === "string"
239
- ? lcMsg.content
240
- : lcMsg.content.flatMap((item) => {
241
- if (item.type === "text") {
242
- return { type: "input_text", text: item.text };
243
- }
244
- if (item.type === "image_url") {
245
- const image_url = typeof item.image_url === "string"
246
- ? item.image_url
247
- : item.image_url.url;
248
- const detail = typeof item.image_url === "string"
249
- ? "auto"
250
- : item.image_url.detail;
251
- return { type: "input_image", image_url, detail };
252
- }
253
- if (item.type === "input_text" ||
254
- item.type === "input_image" ||
255
- item.type === "input_file") {
256
- return item;
257
- }
258
- return [];
259
- }),
260
- };
234
+ const content = typeof lcMsg.content === "string"
235
+ ? lcMsg.content
236
+ : lcMsg.content.flatMap((item) => {
237
+ if (item.type === "text") {
238
+ return { type: "input_text", text: item.text };
239
+ }
240
+ if (item.type === "image_url") {
241
+ const image_url = typeof item.image_url === "string"
242
+ ? item.image_url
243
+ : item.image_url.url;
244
+ const detail = typeof item.image_url === "string"
245
+ ? "auto"
246
+ : item.image_url.detail;
247
+ return { type: "input_image", image_url, detail };
248
+ }
249
+ if (item.type === "input_text" ||
250
+ item.type === "input_image" ||
251
+ item.type === "input_file") {
252
+ return item;
253
+ }
254
+ return [];
255
+ });
256
+ if (role === "user" || role === "system" || role === "developer") {
257
+ return { type: "message", role, content };
261
258
  }
259
+ console.warn(`Unsupported role found when converting to OpenAI Responses API: ${role}`);
262
260
  return [];
263
261
  });
264
262
  }
@@ -226,34 +226,32 @@ function _convertMessagesToOpenAIResponsesParams(messages, model) {
226
226
  }
227
227
  return input;
228
228
  }
229
- if (role === "user") {
230
- return {
231
- type: "message",
232
- role: "user",
233
- content: typeof lcMsg.content === "string"
234
- ? lcMsg.content
235
- : lcMsg.content.flatMap((item) => {
236
- if (item.type === "text") {
237
- return { type: "input_text", text: item.text };
238
- }
239
- if (item.type === "image_url") {
240
- const image_url = typeof item.image_url === "string"
241
- ? item.image_url
242
- : item.image_url.url;
243
- const detail = typeof item.image_url === "string"
244
- ? "auto"
245
- : item.image_url.detail;
246
- return { type: "input_image", image_url, detail };
247
- }
248
- if (item.type === "input_text" ||
249
- item.type === "input_image" ||
250
- item.type === "input_file") {
251
- return item;
252
- }
253
- return [];
254
- }),
255
- };
229
+ const content = typeof lcMsg.content === "string"
230
+ ? lcMsg.content
231
+ : lcMsg.content.flatMap((item) => {
232
+ if (item.type === "text") {
233
+ return { type: "input_text", text: item.text };
234
+ }
235
+ if (item.type === "image_url") {
236
+ const image_url = typeof item.image_url === "string"
237
+ ? item.image_url
238
+ : item.image_url.url;
239
+ const detail = typeof item.image_url === "string"
240
+ ? "auto"
241
+ : item.image_url.detail;
242
+ return { type: "input_image", image_url, detail };
243
+ }
244
+ if (item.type === "input_text" ||
245
+ item.type === "input_image" ||
246
+ item.type === "input_file") {
247
+ return item;
248
+ }
249
+ return [];
250
+ });
251
+ if (role === "user" || role === "system" || role === "developer") {
252
+ return { type: "message", role, content };
256
253
  }
254
+ console.warn(`Unsupported role found when converting to OpenAI Responses API: ${role}`);
257
255
  return [];
258
256
  });
259
257
  }
@@ -2,10 +2,11 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.formatToOpenAIToolChoice = exports.formatToOpenAIAssistantTool = exports.formatToOpenAITool = exports.formatToOpenAIFunction = exports.wrapOpenAIClientError = void 0;
4
4
  const openai_1 = require("openai");
5
- const zod_to_json_schema_1 = require("zod-to-json-schema");
6
5
  const function_calling_1 = require("@langchain/core/utils/function_calling");
7
6
  Object.defineProperty(exports, "formatToOpenAIFunction", { enumerable: true, get: function () { return function_calling_1.convertToOpenAIFunction; } });
8
7
  Object.defineProperty(exports, "formatToOpenAITool", { enumerable: true, get: function () { return function_calling_1.convertToOpenAITool; } });
8
+ const zod_to_json_schema_1 = require("zod-to-json-schema");
9
+ const types_1 = require("@langchain/core/utils/types");
9
10
  const errors_js_1 = require("./errors.cjs");
10
11
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
11
12
  function wrapOpenAIClientError(e) {
@@ -42,7 +43,9 @@ function formatToOpenAIAssistantTool(tool) {
42
43
  function: {
43
44
  name: tool.name,
44
45
  description: tool.description,
45
- parameters: (0, zod_to_json_schema_1.zodToJsonSchema)(tool.schema),
46
+ parameters: (0, types_1.isZodSchema)(tool.schema)
47
+ ? (0, zod_to_json_schema_1.zodToJsonSchema)(tool.schema)
48
+ : tool.schema,
46
49
  },
47
50
  };
48
51
  }
@@ -1,20 +1,9 @@
1
1
  import { OpenAI as OpenAIClient } from "openai";
2
2
  import type { StructuredToolInterface } from "@langchain/core/tools";
3
3
  import { convertToOpenAIFunction, convertToOpenAITool } from "@langchain/core/utils/function_calling";
4
+ import { ToolDefinition } from "@langchain/core/language_models/base";
4
5
  export declare function wrapOpenAIClientError(e: any): any;
5
6
  export { convertToOpenAIFunction as formatToOpenAIFunction, convertToOpenAITool as formatToOpenAITool, };
6
- export declare function formatToOpenAIAssistantTool(tool: StructuredToolInterface): {
7
- type: string;
8
- function: {
9
- name: string;
10
- description: string;
11
- parameters: import("zod-to-json-schema").JsonSchema7Type & {
12
- $schema?: string | undefined;
13
- definitions?: {
14
- [key: string]: import("zod-to-json-schema").JsonSchema7Type;
15
- } | undefined;
16
- };
17
- };
18
- };
7
+ export declare function formatToOpenAIAssistantTool(tool: StructuredToolInterface): ToolDefinition;
19
8
  export type OpenAIToolChoice = OpenAIClient.ChatCompletionToolChoiceOption | "any" | string;
20
9
  export declare function formatToOpenAIToolChoice(toolChoice?: OpenAIToolChoice): OpenAIClient.ChatCompletionToolChoiceOption | undefined;
@@ -1,6 +1,7 @@
1
1
  import { APIConnectionTimeoutError, APIUserAbortError, } from "openai";
2
- import { zodToJsonSchema } from "zod-to-json-schema";
3
2
  import { convertToOpenAIFunction, convertToOpenAITool, } from "@langchain/core/utils/function_calling";
3
+ import { zodToJsonSchema } from "zod-to-json-schema";
4
+ import { isZodSchema } from "@langchain/core/utils/types";
4
5
  import { addLangChainErrorFields } from "./errors.js";
5
6
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
6
7
  export function wrapOpenAIClientError(e) {
@@ -37,7 +38,9 @@ export function formatToOpenAIAssistantTool(tool) {
37
38
  function: {
38
39
  name: tool.name,
39
40
  description: tool.description,
40
- parameters: zodToJsonSchema(tool.schema),
41
+ parameters: isZodSchema(tool.schema)
42
+ ? zodToJsonSchema(tool.schema)
43
+ : tool.schema,
41
44
  },
42
45
  };
43
46
  }
@@ -2,7 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports._convertToOpenAITool = void 0;
4
4
  const function_calling_1 = require("@langchain/core/utils/function_calling");
5
- const zod_1 = require("openai/helpers/zod");
5
+ const openai_js_1 = require("./openai.cjs");
6
6
  /**
7
7
  * Formats a tool in either OpenAI format, or LangChain structured tool format
8
8
  * into an OpenAI tool format. If the tool is already in OpenAI format, return without
@@ -19,29 +19,7 @@ function _convertToOpenAITool(
19
19
  tool, fields) {
20
20
  let toolDef;
21
21
  if ((0, function_calling_1.isLangChainTool)(tool)) {
22
- const oaiToolDef = (0, zod_1.zodFunction)({
23
- name: tool.name,
24
- parameters: tool.schema,
25
- description: tool.description,
26
- });
27
- if (!oaiToolDef.function.parameters) {
28
- // Fallback to the `convertToOpenAIFunction` util if the parameters are not defined.
29
- toolDef = {
30
- type: "function",
31
- function: (0, function_calling_1.convertToOpenAIFunction)(tool, fields),
32
- };
33
- }
34
- else {
35
- toolDef = {
36
- type: oaiToolDef.type,
37
- function: {
38
- name: oaiToolDef.function.name,
39
- description: oaiToolDef.function.description,
40
- parameters: oaiToolDef.function.parameters,
41
- ...(fields?.strict !== undefined ? { strict: fields.strict } : {}),
42
- },
43
- };
44
- }
22
+ toolDef = (0, openai_js_1.formatToOpenAITool)(tool);
45
23
  }
46
24
  else {
47
25
  toolDef = tool;
@@ -1,5 +1,5 @@
1
- import { convertToOpenAIFunction, isLangChainTool, } from "@langchain/core/utils/function_calling";
2
- import { zodFunction } from "openai/helpers/zod";
1
+ import { isLangChainTool } from "@langchain/core/utils/function_calling";
2
+ import { formatToOpenAITool } from "./openai.js";
3
3
  /**
4
4
  * Formats a tool in either OpenAI format, or LangChain structured tool format
5
5
  * into an OpenAI tool format. If the tool is already in OpenAI format, return without
@@ -16,29 +16,7 @@ export function _convertToOpenAITool(
16
16
  tool, fields) {
17
17
  let toolDef;
18
18
  if (isLangChainTool(tool)) {
19
- const oaiToolDef = zodFunction({
20
- name: tool.name,
21
- parameters: tool.schema,
22
- description: tool.description,
23
- });
24
- if (!oaiToolDef.function.parameters) {
25
- // Fallback to the `convertToOpenAIFunction` util if the parameters are not defined.
26
- toolDef = {
27
- type: "function",
28
- function: convertToOpenAIFunction(tool, fields),
29
- };
30
- }
31
- else {
32
- toolDef = {
33
- type: oaiToolDef.type,
34
- function: {
35
- name: oaiToolDef.function.name,
36
- description: oaiToolDef.function.description,
37
- parameters: oaiToolDef.function.parameters,
38
- ...(fields?.strict !== undefined ? { strict: fields.strict } : {}),
39
- },
40
- };
41
- }
19
+ toolDef = formatToOpenAITool(tool);
42
20
  }
43
21
  else {
44
22
  toolDef = tool;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@langchain/openai",
3
- "version": "0.5.3",
3
+ "version": "0.5.5",
4
4
  "description": "OpenAI integrations for LangChain.js",
5
5
  "type": "module",
6
6
  "engines": {