openai-node
openai-node copied to clipboard
Latest release has typescript issues
Describe the bug
After the new GPT-3.5 update, building my typescript app now failed. I get errors:
node_modules/openai/api.ts:3819:38 - error TS2304: Cannot find name 'File'.
3819 public createTranscription(file: File, model: string, prompt?: string, responseFormat?: string, temperature?: number, options?: AxiosRequestConfig) {
~~~~
node_modules/openai/api.ts:3835:36 - error TS2304: Cannot find name 'File'.
3835 public createTranslation(file: File, model: string, prompt?: string, responseFormat?: string, temperature?: number, options?: AxiosRequestConfig) {
To Reproduce
npm i openai@latest npm run build
Code snippets
No response
OS
windows/linux
Node version
18
Library version
3.2.0
I'm also experiencing this. Anyone know a quick workaround?
I also am experiencing this
I'm also experience this. Anyone know a quick workaround?
I had to ignore typescript errors tsc -p . --outDir build 2> /dev/null || true
Seems they designed this API for use on front-end, for the "nodejs" module
This should be fixed to not expect browser types.
Workaround for me: adjusting my tsconfig.json
to include the DOM lib:
{
"compilerOptions": {
"lib": ["es2020", "dom"]
}
}
Not perfect as DOM types should not be included in a Node.js setting, but it works for now.
Also had to set "exactOptionalPropertyTypes": false
. With "exactOptionalPropertyTypes": true
, it would throw even more errors.
Adding type File = unknown
to the top of the complaining node_modules/openai/dist/api.d.ts
file silenced the 24 errors for me. Quick and dirty solution as long as you're not calling any of the methods that use it.
Adding
type File = unknown
to the top of the complainingnode_modules/openai/dist/api.d.ts
file silenced the 24 errors for me.
Use patch-package for that. node_modules
changes will overwritten by running npm install
or similar commands.
Recently installed openai 3.2.1
and typescript throws same error like first post. Is there any updates about this bug ?
Also affected by this bug...
We have an upcoming version v4.0.0 with overhauled TypeScript types; please give it a try and share any feedback in the linked discussion!
@rattrayalex not sure why this was closed. Even in the V4 beta, there are tons of type issues:
node_modules/openai/src/core.ts:135:66 - error TS2345: Argument of type 'number | undefined' is not assignable to parameter of type 'number'.
Type 'undefined' is not assignable to type 'number'.
> tsc -p . --outDir build
135 if ('timeout' in options) validatePositiveInteger('timeout', options.timeout);
~~~~~~~~~~~~~~~
node_modules/openai/src/core.ts:165:11 - error TS2322: Type 'any' is not assignable to type 'never'.
165 const req: RequestInit = {
~~~
node_modules/openai/src/core.ts:201:45 - error TS2339: Property 'headers' does not exist on type 'never'.
201 this.debug('request', url, options, req.headers);
~~~~~~~
node_modules/openai/src/core.ts:204:50 - error TS2345: Argument of type 'string' is not assignable to parameter of type 'never'.
204 const response = await this.fetchWithTimeout(url, req, timeout, controller).catch(castToError);
~~~
node_modules/openai/src/core.ts:212:60 - error TS2339: Property 'headers' does not exist on type 'never'.
212 const responseHeaders = createResponseHeaders(response.headers);
~~~~~~~
node_modules/openai/src/core.ts:214:19 - error TS2339: Property 'ok' does not exist on type 'never'.
214 if (!response.ok) {
~~
node_modules/openai/src/core.ts:219:38 - error TS2339: Property 'text' does not exist on type 'never'.
219 const errText = await response.text().catch(() => 'Unknown');
~~~~
node_modules/openai/src/core.ts:223:39 - error TS2339: Property 'status' does not exist on type 'never'.
223 this.debug('response', response.status, url, responseHeaders, errMessage);
~~~~~~
node_modules/openai/src/core.ts:225:49 - error TS2339: Property 'status' does not exist on type 'never'.
225 const err = this.makeStatusError(response.status, errJSON, errMessage, responseHeaders);
~~~~~~
node_modules/openai/src/core.ts:235:34 - error TS2339: Property 'headers' does not exist on type 'never'.
235 const contentType = response.headers.get('content-type');
~~~~~~~
node_modules/openai/src/core.ts:237:35 - error TS2339: Property 'json' does not exist on type 'never'.
237 const json = await response.json();
~~~~
node_modules/openai/src/core.ts:248:39 - error TS2339: Property 'status' does not exist on type 'never'.
248 this.debug('response', response.status, url, responseHeaders, json);
~~~~~~
node_modules/openai/src/core.ts:254:27 - error TS2339: Property 'text' does not exist on type 'never'.
254 const text = response.text();
~~~~
node_modules/openai/src/core.ts:255:37 - error TS2339: Property 'status' does not exist on type 'never'.
255 this.debug('response', response.status, url, responseHeaders, text);
~~~~~~
node_modules/openai/src/core.ts:291:13 - error TS2339: Property 'signal' does not exist on type '{}'.
291 const { signal, ...options } = init || {};
~~~~~~
node_modules/openai/src/core.ts:297:19 - error TS2345: Argument of type '{ signal: any; }' is not assignable to parameter of type 'undefined'.
297 .fetch(url, { signal: controller.signal as any, ...options })
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
node_modules/openai/src/core.ts:309:40 - error TS2339: Property 'headers' does not exist on type 'never'.
309 const shouldRetryHeader = response.headers.get('x-should-retry');
~~~~~~~
node_modules/openai/src/core.ts:316:18 - error TS2339: Property 'status' does not exist on type 'never'.
316 if (response.status === 409) return true;
~~~~~~
node_modules/openai/src/core.ts:319:18 - error TS2339: Property 'status' does not exist on type 'never'.
319 if (response.status === 429) return true;
~~~~~~
node_modules/openai/src/core.ts:322:18 - error TS2339: Property 'status' does not exist on type 'never'.
322 if (response.status >= 500) return true;
~~~~~~
node_modules/openai/src/streaming.ts:26:59 - error TS2339: Property 'headers' does not exist on type 'never'.
26 this.responseHeaders = createResponseHeaders(response.headers);
~~~~~~~
node_modules/openai/src/streaming.ts:30:24 - error TS2339: Property 'body' does not exist on type 'never'.
30 if (!this.response.body) {
~~~~
node_modules/openai/src/streaming.ts:36:67 - error TS2339: Property 'body' does not exist on type 'never'.
36 const iter = readableStreamAsyncIterable<Bytes>(this.response.body);
~~~~
node_modules/openai/src/uploads.ts:234:10 - error TS2339: Property 'append' does not exist on type 'never'.
234 form.append(key, value);
~~~~~~
node_modules/openai/src/uploads.ts:237:10 - error TS2339: Property 'append' does not exist on type 'never'.
237 form.append(key, file);
~~~~~~
@tobq I think the majority of these errors seem to be caused by an issue with your setup, can you share your tsconfig?
In addition to the tsconfig, can you share whether you are running your code in Node or another environment?
@RobertCraigie @rattrayalex I'm running my code in a node environment - without DOM types (for obvious reasons)
{
"extends": "@tsconfig/node18/tsconfig.json",
"compilerOptions": {
"moduleResolution": "Node",
"target": "ESNext",
"module": "ESNext",
"esModuleInterop": true,
}
}
Ah, @tobq do you have node-fetch@3
installed? If so, you have two options:
- Uninstall it, and use
node-fetch@2
instead. - Enable
skipLibCheck: true
in your tsconfig (which is often a good idea anyway)
If either of these options cause problems for you, please let us know (though really we should be in a different github issue at this point)
@rattrayalex I don't have node-fetch installed. I'm using Node 18.
Non it's not solved still getting error with v4 and using ts-node
with tsconfig/paths
in Typescript
tsconfig.json
"ts-node": {
"require": ["tsconfig-paths/register"]
}
/test/node_modules/openai/_shims/formdata.js:6
exports.File = File;
^
ReferenceError: File is not defined
at Object.<anonymous> (/test/node_modules/openai/_shims/formdata.js:6:16)
at Module._compile (node:internal/modules/cjs/loader:1159:14)
at Module._extensions..js (node:internal/modules/cjs/loader:1213:10)
at Object.require.extensions.<computed> [as .js] (/usr/local/lib/node_modules/ts-node/src/index.ts:1608:43)
at Module.load (node:internal/modules/cjs/loader:1037:32)
at Function.Module._load (node:internal/modules/cjs/loader:878:12)
at Module.require (node:internal/modules/cjs/loader:1061:19)
at require (node:internal/modules/cjs/helpers:103:18)
at Object.<anonymous> (/test/node_modules/openai/src/uploads.ts:66:15)
at Module._compile (node:internal/modules/cjs/loader:1159:14)
@khanakia can you share more about your setup please? Such as:
- Operating system
- Node version
- Full
tsconfig.json
I was able to solve the issue i by removing this line in tsconfig.json "*": ["node_modules/*"]
"moduleResolution": "node",
"baseUrl": "./",
"paths": {
"@feat/*": ["src/feature/*"],
"@data/*": ["data/*"],
"*": ["node_modules/*"]
}
@khanakia I'm curious why you had that line in your tsconfig to begin with? Resolving things in node_modules normally works automatically, so that line seems unnecessary, but I'm wondering if you were working around some weird issue with another package.
it was added by an old developer I have no idea also
@tobq there are some other tsconfig.json
settings you can try if you want to keep skipLibCheck: false
:
- use
"moduleResolution": "NodeNext"
- or use
"paths": { "openai/_shims/*": ["node_modules/openai/_shims/*.node"] }
The underlying problem is that even though Node correctly resolves ./_shims/*
to ./_shims/*.node
from the exports
in package.json
, tsc
doesn't take exports
into account unless you use "moduleResolution": "NodeNext"
.
Still getting the same errors as @tobq after upgrading to v^4.0.1
. My typescript version is 5.1.6
. I'm using the default tsconfig.json
generated by NestJS:
{
"compilerOptions": {
"module": "commonjs",
"declaration": true,
"removeComments": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"allowSyntheticDefaultImports": true,
"target": "ES2021",
"sourceMap": true,
"outDir": "./dist",
"baseUrl": "./",
"incremental": true,
"skipLibCheck": true,
"strictNullChecks": false,
"noImplicitAny": false,
"strictBindCallApply": false,
"forceConsistentCasingInFileNames": false,
"noFallthroughCasesInSwitch": false
}
}
@arko7n can you share your package.json
?
package.json
:
{
"name": "starkis-api-test",
"version": "0.0.1",
"description": "",
"author": "",
"private": true,
"license": "UNLICENSED",
"scripts": {
"build": "nest build",
"format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
"start": "nest start",
"start:dev": "nest start --watch",
"start:debug": "nest start --debug --watch",
"start:prod": "node dist/main",
"lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
"test": "jest",
"test:watch": "jest --watch",
"test:cov": "jest --coverage",
"test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
"test:e2e": "jest --config ./test/jest-e2e.json"
},
"dependencies": {
"@nestjs/common": "^10.0.0",
"@nestjs/core": "^10.0.0",
"@nestjs/platform-express": "^10.0.0",
"openai": "^4.1.0",
"reflect-metadata": "^0.1.13",
"rxjs": "^7.8.1"
},
"devDependencies": {
"@nestjs/cli": "^10.0.0",
"@nestjs/schematics": "^10.0.0",
"@nestjs/testing": "^10.0.0",
"@types/express": "^4.17.17",
"@types/jest": "^29.5.2",
"@types/node": "^20.3.1",
"@types/supertest": "^2.0.12",
"@typescript-eslint/eslint-plugin": "^6.4.1",
"@typescript-eslint/parser": "^6.4.1",
"eslint": "^8.42.0",
"eslint-config-prettier": "^9.0.0",
"eslint-plugin-prettier": "^5.0.0",
"jest": "^29.5.0",
"prettier": "^3.0.2",
"source-map-support": "^0.5.21",
"supertest": "^6.3.3",
"ts-jest": "^29.1.0",
"ts-loader": "^9.4.3",
"ts-node": "^10.9.1",
"tsconfig-paths": "^4.2.0",
"typescript": "^5.1.3"
},
"jest": {
"moduleFileExtensions": [
"js",
"json",
"ts"
],
"rootDir": "src",
"testRegex": ".*\\.spec\\.ts$",
"transform": {
"^.+\\.(t|j)s$": "ts-jest"
},
"collectCoverageFrom": [
"**/*.(t|j)s"
],
"coverageDirectory": "../coverage",
"testEnvironment": "node"
}
}
It's mostly generated by NestJS.
Here's my tsconfig.build.json
:
{
"extends": "./tsconfig.json",
"exclude": ["node_modules", "test", "dist", "**/*spec.ts"]
}
And .eslintrc.js
:
module.exports = {
parser: '@typescript-eslint/parser',
parserOptions: {
project: 'tsconfig.json',
tsconfigRootDir: __dirname,
sourceType: 'module',
},
plugins: ['@typescript-eslint/eslint-plugin'],
extends: [
'plugin:@typescript-eslint/recommended',
'plugin:prettier/recommended',
],
root: true,
env: {
node: true,
jest: true,
},
ignorePatterns: ['.eslintrc.js'],
rules: {
'@typescript-eslint/interface-name-prefix': 'off',
'@typescript-eslint/explicit-function-return-type': 'off',
'@typescript-eslint/explicit-module-boundary-types': 'off',
'@typescript-eslint/no-explicit-any': 'off',
},
};
Thanks for looking into it!
@arko7n actually I thought of a better thing to ask for, my apologies...can you paste the output of tsc --traceResolution | grep formdata
? (if the command that errors has extra arguments like -p tsconfig.build.json
, pass those as well)
yarn tsc --traceResolution | grep formdata
:
======== Resolving module 'openai/_shims/formdata' from '/Users/arko7n/Workspaces/Starkisplex/starkis-api-test/node_modules/openai/uploads.d.ts'. ========
'baseUrl' option is set to '/Users/arko7n/Workspaces/Starkisplex/starkis-api-test', using this value to resolve non-relative module name 'openai/_shims/formdata'.
Resolving module name 'openai/_shims/formdata' relative to base url '/Users/arko7n/Workspaces/Starkisplex/starkis-api-test' - '/Users/arko7n/Workspaces/Starkisplex/starkis-api-test/openai/_shims/formdata'.
Loading module as file / folder, candidate module location '/Users/arko7n/Workspaces/Starkisplex/starkis-api-test/openai/_shims/formdata', target file types: TypeScript, Declaration.
Loading module 'openai/_shims/formdata' from 'node_modules' folder, target file types: TypeScript, Declaration.
File '/Users/arko7n/Workspaces/Starkisplex/starkis-api-test/node_modules/openai/_shims/formdata.ts' does not exist.
File '/Users/arko7n/Workspaces/Starkisplex/starkis-api-test/node_modules/openai/_shims/formdata.tsx' does not exist.
File '/Users/arko7n/Workspaces/Starkisplex/starkis-api-test/node_modules/openai/_shims/formdata.d.ts' exists - use it as a name resolution result.
Resolving real path for '/Users/arko7n/Workspaces/Starkisplex/starkis-api-test/node_modules/openai/_shims/formdata.d.ts', result '/Users/arko7n/Workspaces/Starkisplex/starkis-api-test/node_modules/openai/_shims/formdata.d.ts'.
======== Module name 'openai/_shims/formdata' was successfully resolved to '/Users/arko7n/Workspaces/Starkisplex/starkis-api-test/node_modules/openai/_shims/formdata.d.ts' with Package ID 'openai/_shims/[email protected]'. ========
======== Resolving module '../uploads.js' from '/Users/arko7n/Workspaces/Starkisplex/starkis-api-test/node_modules/openai/_shims/formdata.d.ts'. ========
======== Resolving module './formdata.js' from '/Users/arko7n/Workspaces/Starkisplex/starkis-api-test/node_modules/openai/_shims/fileFromPath.d.ts'. ========
Loading module as file / folder, candidate module location '/Users/arko7n/Workspaces/Starkisplex/starkis-api-test/node_modules/openai/_shims/formdata.js', target file types: TypeScript, Declaration.
File name '/Users/arko7n/Workspaces/Starkisplex/starkis-api-test/node_modules/openai/_shims/formdata.js' has a '.js' extension - stripping it.
File '/Users/arko7n/Workspaces/Starkisplex/starkis-api-test/node_modules/openai/_shims/formdata.ts' does not exist.
File '/Users/arko7n/Workspaces/Starkisplex/starkis-api-test/node_modules/openai/_shims/formdata.tsx' does not exist.
File '/Users/arko7n/Workspaces/Starkisplex/starkis-api-test/node_modules/openai/_shims/formdata.d.ts' exists - use it as a name resolution result.
======== Module name './formdata.js' was successfully resolved to '/Users/arko7n/Workspaces/Starkisplex/starkis-api-test/node_modules/openai/_shims/formdata.d.ts' with Package ID 'openai/_shims/[email protected]'. ========
======== Resolving module './_shims/formdata.js' from '/Users/arko7n/Workspaces/Starkisplex/starkis-api-test/node_modules/openai/src/uploads.ts'. ========
Loading module as file / folder, candidate module location '/Users/arko7n/Workspaces/Starkisplex/starkis-api-test/node_modules/openai/src/_shims/formdata.js', target file types: TypeScript, Declaration.
File name '/Users/arko7n/Workspaces/Starkisplex/starkis-api-test/node_modules/openai/src/_shims/formdata.js' has a '.js' extension - stripping it.
File '/Users/arko7n/Workspaces/Starkisplex/starkis-api-test/node_modules/openai/src/_shims/formdata.ts' does not exist.
File '/Users/arko7n/Workspaces/Starkisplex/starkis-api-test/node_modules/openai/src/_shims/formdata.tsx' does not exist.
File '/Users/arko7n/Workspaces/Starkisplex/starkis-api-test/node_modules/openai/src/_shims/formdata.d.ts' exists - use it as a name resolution result.
======== Module name './_shims/formdata.js' was successfully resolved to '/Users/arko7n/Workspaces/Starkisplex/starkis-api-test/node_modules/openai/src/_shims/formdata.d.ts' with Package ID 'openai/src/_shims/[email protected]'. ========
======== Resolving module '../uploads' from '/Users/arko7n/Workspaces/Starkisplex/starkis-api-test/node_modules/openai/src/_shims/formdata.d.ts'. ========
======== Resolving module './formdata.js' from '/Users/arko7n/Workspaces/Starkisplex/starkis-api-test/node_modules/openai/src/_shims/getMultipartRequestOptions.ts'. ========
Loading module as file / folder, candidate module location '/Users/arko7n/Workspaces/Starkisplex/starkis-api-test/node_modules/openai/src/_shims/formdata.js', target file types: TypeScript, Declaration.
File name '/Users/arko7n/Workspaces/Starkisplex/starkis-api-test/node_modules/openai/src/_shims/formdata.js' has a '.js' extension - stripping it.
File '/Users/arko7n/Workspaces/Starkisplex/starkis-api-test/node_modules/openai/src/_shims/formdata.ts' does not exist.
File '/Users/arko7n/Workspaces/Starkisplex/starkis-api-test/node_modules/openai/src/_shims/formdata.tsx' does not exist.
File '/Users/arko7n/Workspaces/Starkisplex/starkis-api-test/node_modules/openai/src/_shims/formdata.d.ts' exists - use it as a name resolution result.
======== Module name './formdata.js' was successfully resolved to '/Users/arko7n/Workspaces/Starkisplex/starkis-api-test/node_modules/openai/src/_shims/formdata.d.ts' with Package ID 'openai/src/_shims/[email protected]'. ========
======== Resolving module './formdata.js' from '/Users/arko7n/Workspaces/Starkisplex/starkis-api-test/node_modules/openai/src/_shims/fileFromPath.ts'. ========
Resolution for module './formdata.js' was found in cache from location '/Users/arko7n/Workspaces/Starkisplex/starkis-api-test/node_modules/openai/src/_shims'.
======== Module name './formdata.js' was successfully resolved to '/Users/arko7n/Workspaces/Starkisplex/starkis-api-test/node_modules/openai/src/_shims/formdata.d.ts' with Package ID 'openai/src/_shims/[email protected]'. ========
error Command failed with exit code 2.
I'm using yarn run start
and getting these errors:
yarn run v1.22.19
$ nest start
node_modules/openai/src/core.ts:286:11 - error TS2322: Type '{ signal: AbortSignal; agent: any; headers: Record<string, string>; body: any; method: HTTPMethod; }' is not assignable to type 'never'.
286 const req: RequestInit = {
~~~
node_modules/openai/src/core.ts:338:40 - error TS2339: Property 'headers' does not exist on type 'never'.
338 debug('request', url, options, req.headers);
~~~~~~~
node_modules/openai/src/core.ts:400:40 - error TS2345: Argument of type 'Req' is not assignable to parameter of type 'Record<string, unknown>'.
400 url.search = this.stringifyQuery(query);
~~~~~
node_modules/openai/src/core.ts:388:12
388 buildURL<Req>(path: string, query: Req | undefined): string {
~~~
This type parameter might need an `extends Record<string, unknown>` constraint.
node_modules/openai/src/core.ts:429:13 - error TS2339: Property 'signal' does not exist on type '{}'.
429 const { signal, ...options } = init || {};
~~~~~~
node_modules/openai/src/core.ts:435:19 - error TS2345: Argument of type '{ signal: any; }' is not assignable to parameter of type 'never'.
435 .fetch(url, { signal: controller.signal as any, ...options })
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Found 5 error(s).
error Command failed with exit code 1.
@arko7n okay thanks! And sorry for the trouble. Unfortunately it's resolving to the non-node type declarations, which depend on global Web API types for RequestInit
, File
etc. being present. We're going to have to think hard about if we can get better one-size-fits-all behavior for the types across different environments. In the meantime, I think you'll have luck with the workarounds in https://github.com/openai/openai-node/issues/72#issuecomment-1684044188. If you try any of them let me know how it goes.
Thanks for trying. I followed #72, but it still didn't work unfortunately. For some reason serverless offline
brings up the service and it seems to be swallowing the compiler errors, so I'll need to investigate why. (I'll need the service to run without serverless since I'm moving it to ECS).
Alternatively, I can always downgrade openai to an older version that was working (like v3.3.0)
@arko7n wait a minute...are you importing from openai/src
in your own code? For some reason next start
is typechecking files in openai/src
, but we only include those files for source mapping purposes, so that you can see the source code when you go to definition.