@langchain/openai 0.5.12 → 0.5.14

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.
@@ -0,0 +1,35 @@
1
+ const iife = (fn) => fn();
2
+ export function isHeaders(headers) {
3
+ return (typeof Headers !== "undefined" &&
4
+ headers !== null &&
5
+ typeof headers === "object" &&
6
+ Object.prototype.toString.call(headers) === "[object Headers]");
7
+ }
8
+ export function normalizeHeaders(headers) {
9
+ const output = iife(() => {
10
+ // If headers is a Headers instance
11
+ if (isHeaders(headers)) {
12
+ return headers;
13
+ }
14
+ // If headers is an array of [key, value] pairs
15
+ else if (Array.isArray(headers)) {
16
+ return new Headers(headers);
17
+ }
18
+ // If headers is a NullableHeaders-like object (has 'values' property that is a Headers)
19
+ else if (typeof headers === "object" &&
20
+ headers !== null &&
21
+ "values" in headers &&
22
+ isHeaders(headers.values)) {
23
+ return headers.values;
24
+ }
25
+ // If headers is a plain object
26
+ else if (typeof headers === "object" && headers !== null) {
27
+ const entries = Object.entries(headers)
28
+ .filter(([, v]) => typeof v === "string")
29
+ .map(([k, v]) => [k, v]);
30
+ return new Headers(entries);
31
+ }
32
+ return new Headers();
33
+ });
34
+ return Object.fromEntries(output.entries());
35
+ }
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.formatFunctionDefinitions = void 0;
3
+ exports.formatFunctionDefinitions = formatFunctionDefinitions;
4
4
  function isAnyOfProp(prop) {
5
5
  return (prop.anyOf !== undefined &&
6
6
  Array.isArray(prop.anyOf));
@@ -26,7 +26,6 @@ function formatFunctionDefinitions(functions) {
26
26
  lines.push("} // namespace functions");
27
27
  return lines.join("\n");
28
28
  }
29
- exports.formatFunctionDefinitions = formatFunctionDefinitions;
30
29
  // Format just the properties of an object (not including the surrounding braces)
31
30
  function formatObjectProperties(obj, indent) {
32
31
  const lines = [];
@@ -1,12 +1,15 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.formatToOpenAIToolChoice = exports.formatToOpenAIAssistantTool = exports.formatToOpenAITool = exports.formatToOpenAIFunction = exports.wrapOpenAIClientError = void 0;
3
+ exports.formatToOpenAITool = exports.formatToOpenAIFunction = void 0;
4
+ exports.wrapOpenAIClientError = wrapOpenAIClientError;
5
+ exports.formatToOpenAIAssistantTool = formatToOpenAIAssistantTool;
6
+ exports.formatToOpenAIToolChoice = formatToOpenAIToolChoice;
4
7
  const openai_1 = require("openai");
5
8
  const function_calling_1 = require("@langchain/core/utils/function_calling");
6
9
  Object.defineProperty(exports, "formatToOpenAIFunction", { enumerable: true, get: function () { return function_calling_1.convertToOpenAIFunction; } });
7
10
  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
11
  const types_1 = require("@langchain/core/utils/types");
12
+ const json_schema_1 = require("@langchain/core/utils/json_schema");
10
13
  const errors_js_1 = require("./errors.cjs");
11
14
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
12
15
  function wrapOpenAIClientError(e) {
@@ -36,20 +39,18 @@ function wrapOpenAIClientError(e) {
36
39
  }
37
40
  return error;
38
41
  }
39
- exports.wrapOpenAIClientError = wrapOpenAIClientError;
40
42
  function formatToOpenAIAssistantTool(tool) {
41
43
  return {
42
44
  type: "function",
43
45
  function: {
44
46
  name: tool.name,
45
47
  description: tool.description,
46
- parameters: (0, types_1.isZodSchema)(tool.schema)
47
- ? (0, zod_to_json_schema_1.zodToJsonSchema)(tool.schema)
48
+ parameters: (0, types_1.isInteropZodSchema)(tool.schema)
49
+ ? (0, json_schema_1.toJsonSchema)(tool.schema)
48
50
  : tool.schema,
49
51
  },
50
52
  };
51
53
  }
52
- exports.formatToOpenAIAssistantTool = formatToOpenAIAssistantTool;
53
54
  function formatToOpenAIToolChoice(toolChoice) {
54
55
  if (!toolChoice) {
55
56
  return undefined;
@@ -75,4 +76,3 @@ function formatToOpenAIToolChoice(toolChoice) {
75
76
  return toolChoice;
76
77
  }
77
78
  }
78
- exports.formatToOpenAIToolChoice = formatToOpenAIToolChoice;
@@ -1,7 +1,7 @@
1
1
  import { APIConnectionTimeoutError, APIUserAbortError, } from "openai";
2
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";
3
+ import { isInteropZodSchema } from "@langchain/core/utils/types";
4
+ import { toJsonSchema } from "@langchain/core/utils/json_schema";
5
5
  import { addLangChainErrorFields } from "./errors.js";
6
6
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
7
7
  export function wrapOpenAIClientError(e) {
@@ -38,8 +38,8 @@ export function formatToOpenAIAssistantTool(tool) {
38
38
  function: {
39
39
  name: tool.name,
40
40
  description: tool.description,
41
- parameters: isZodSchema(tool.schema)
42
- ? zodToJsonSchema(tool.schema)
41
+ parameters: isInteropZodSchema(tool.schema)
42
+ ? toJsonSchema(tool.schema)
43
43
  : tool.schema,
44
44
  },
45
45
  };
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.convertPromptToOpenAI = void 0;
3
+ exports.convertPromptToOpenAI = convertPromptToOpenAI;
4
4
  const chat_models_js_1 = require("../chat_models.cjs");
5
5
  /**
6
6
  * Convert a formatted LangChain prompt (e.g. pulled from the hub) into
@@ -39,4 +39,3 @@ function convertPromptToOpenAI(formattedPrompt) {
39
39
  messages: (0, chat_models_js_1._convertMessagesToOpenAIParams)(messages),
40
40
  };
41
41
  }
42
- exports.convertPromptToOpenAI = convertPromptToOpenAI;
@@ -1,7 +1,9 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports._convertToOpenAITool = void 0;
3
+ exports._convertToOpenAITool = _convertToOpenAITool;
4
4
  const function_calling_1 = require("@langchain/core/utils/function_calling");
5
+ const types_1 = require("@langchain/core/utils/types");
6
+ const zod_1 = require("openai/helpers/zod");
5
7
  const openai_js_1 = require("./openai.cjs");
6
8
  /**
7
9
  * Formats a tool in either OpenAI format, or LangChain structured tool format
@@ -19,7 +21,21 @@ function _convertToOpenAITool(
19
21
  tool, fields) {
20
22
  let toolDef;
21
23
  if ((0, function_calling_1.isLangChainTool)(tool)) {
22
- toolDef = (0, openai_js_1.formatToOpenAITool)(tool);
24
+ // FIXME: This is a hack to use OpenAI's native zodFunction util
25
+ // since their json schema standard is not always compatible with
26
+ // the schemas produced by zod. Ideally, we should be using the
27
+ // `zodFunction` util always, but that can only happen when OpenAI
28
+ // supports zod v4.
29
+ if ((0, types_1.isZodSchemaV3)(tool.schema)) {
30
+ toolDef = (0, zod_1.zodFunction)({
31
+ name: tool.name,
32
+ parameters: tool.schema,
33
+ description: tool.description,
34
+ });
35
+ }
36
+ else {
37
+ toolDef = (0, openai_js_1.formatToOpenAITool)(tool);
38
+ }
23
39
  }
24
40
  else {
25
41
  toolDef = tool;
@@ -29,4 +45,3 @@ tool, fields) {
29
45
  }
30
46
  return toolDef;
31
47
  }
32
- exports._convertToOpenAITool = _convertToOpenAITool;
@@ -1,4 +1,6 @@
1
1
  import { isLangChainTool } from "@langchain/core/utils/function_calling";
2
+ import { isZodSchemaV3 } from "@langchain/core/utils/types";
3
+ import { zodFunction } from "openai/helpers/zod";
2
4
  import { formatToOpenAITool } from "./openai.js";
3
5
  /**
4
6
  * Formats a tool in either OpenAI format, or LangChain structured tool format
@@ -16,7 +18,21 @@ export function _convertToOpenAITool(
16
18
  tool, fields) {
17
19
  let toolDef;
18
20
  if (isLangChainTool(tool)) {
19
- toolDef = formatToOpenAITool(tool);
21
+ // FIXME: This is a hack to use OpenAI's native zodFunction util
22
+ // since their json schema standard is not always compatible with
23
+ // the schemas produced by zod. Ideally, we should be using the
24
+ // `zodFunction` util always, but that can only happen when OpenAI
25
+ // supports zod v4.
26
+ if (isZodSchemaV3(tool.schema)) {
27
+ toolDef = zodFunction({
28
+ name: tool.name,
29
+ parameters: tool.schema,
30
+ description: tool.description,
31
+ });
32
+ }
33
+ else {
34
+ toolDef = formatToOpenAITool(tool);
35
+ }
20
36
  }
21
37
  else {
22
38
  toolDef = tool;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@langchain/openai",
3
- "version": "0.5.12",
3
+ "version": "0.5.14",
4
4
  "description": "OpenAI integrations for LangChain.js",
5
5
  "type": "module",
6
6
  "engines": {
@@ -17,7 +17,7 @@
17
17
  "build": "yarn turbo:command build:internal --filter=@langchain/openai",
18
18
  "build:internal": "yarn lc_build --create-entrypoints --pre --tree-shaking",
19
19
  "lint:eslint": "NODE_OPTIONS=--max-old-space-size=4096 eslint --cache --ext .ts,.js src/",
20
- "lint:dpdm": "dpdm --exit-code circular:1 --no-warning --no-tree src/*.ts src/**/*.ts",
20
+ "lint:dpdm": "dpdm --skip-dynamic-imports circular --exit-code circular:1 --no-warning --no-tree src/*.ts src/**/*.ts",
21
21
  "lint": "yarn lint:eslint && yarn lint:dpdm",
22
22
  "lint:fix": "yarn lint:eslint --fix && yarn lint:dpdm",
23
23
  "clean": "rm -rf .turbo dist/",
@@ -36,26 +36,25 @@
36
36
  "license": "MIT",
37
37
  "dependencies": {
38
38
  "js-tiktoken": "^1.0.12",
39
- "openai": "^4.96.0",
40
- "zod": "^3.22.4",
41
- "zod-to-json-schema": "^3.22.3"
39
+ "openai": "^5.3.0",
40
+ "zod": "^3.25.32"
42
41
  },
43
42
  "peerDependencies": {
44
- "@langchain/core": ">=0.3.48 <0.4.0"
43
+ "@langchain/core": ">=0.3.58 <0.4.0"
45
44
  },
46
45
  "devDependencies": {
47
46
  "@azure/identity": "^4.2.1",
48
47
  "@jest/globals": "^29.5.0",
49
- "@langchain/core": "0.3.57",
48
+ "@langchain/core": "workspace:*",
50
49
  "@langchain/scripts": ">=0.1.0 <0.2.0",
51
50
  "@langchain/standard-tests": "0.0.0",
52
51
  "@swc/core": "^1.3.90",
53
52
  "@swc/jest": "^0.2.29",
54
- "dpdm": "^3.12.0",
53
+ "dpdm": "^3.14.0",
55
54
  "eslint": "^8.33.0",
56
55
  "eslint-config-airbnb-base": "^15.0.0",
57
56
  "eslint-config-prettier": "^8.6.0",
58
- "eslint-plugin-import": "^2.27.5",
57
+ "eslint-plugin-import": "^2.31.0",
59
58
  "eslint-plugin-jest": "^27.6.0",
60
59
  "eslint-plugin-no-instanceof": "^1.0.1",
61
60
  "eslint-plugin-prettier": "^4.2.1",
@@ -65,7 +64,7 @@
65
64
  "release-it": "^18.1.2",
66
65
  "rimraf": "^5.0.1",
67
66
  "ts-jest": "^29.1.0",
68
- "typescript": "~5.1.6"
67
+ "typescript": "~5.8.3"
69
68
  },
70
69
  "publishConfig": {
71
70
  "access": "public"
@@ -103,4 +102,4 @@
103
102
  "index.d.ts",
104
103
  "index.d.cts"
105
104
  ]
106
- }
105
+ }