openai-node icon indicating copy to clipboard operation
openai-node copied to clipboard

typescript fail

Open orr-levinger-zets opened this issue 1 year ago • 1 comments

Confirm this is a Node library issue and not an underlying OpenAI API issue

  • [X] This is an issue with the Node library

Describe the bug

error TS2345: Argument of type 'BlobPart[]' is not assignable to parameter of type '(Blob | BinaryLike)[]'. Type 'BlobPart' is not assignable to type 'Blob | BinaryLike'. Type 'ArrayBuffer' is not assignable to type 'Blob | BinaryLike'. Type 'ArrayBuffer' is missing the following properties from type 'Float64Array': BYTES_PER_ELEMENT, buffer, byteOffset, copyWithin, and 24 more.

131 return new File(bits, name, options); ~~~~

Found 1 error in node_modules/.pnpm/[email protected]/node_modules/openai/src/uploads.ts:131

To Reproduce

use pnpm to install the lib use it in any ts file run npx tsc

versions: typescript: 4.9.5 node: v20.7.0 pnpm: 7.33.4

Code snippets

{
  "name": "@zest/common-openai",
  "version": "1.0.0",
  "description": "Common openai",
  "scripts": {
    "test": "jest",
    "test:watch": "jest --watch",
    "test:watch:all": "jest --watchAll",
    "tsc:ci": "tsc --noEmit",
    "debug": "node --inspect-brk node_modules/.bin/jest --runInBand"
  },
  "author": "env0",
  "license": "MIT",
  "main": "index.ts",
  "private": true,
  "dependencies": {
    "openai": "4.56.0"
  }
}

OS

14.1 (23B2073)

Node version

v20.7.0

Library version

4.56.0

orr-levinger-zets avatar Aug 28 '24 15:08 orr-levinger-zets

Thanks for the report, can you share your tsconfig as well?

RobertCraigie avatar Sep 03 '24 19:09 RobertCraigie

Since it might help someone else:

I faced the same issue and got the following error:

../../../node_modules/.pnpm/[email protected]/node_modules/openai/src/core.ts:72:5 - error TS2322: Type 'unknown' is not assignable to type 'WithRequestID<T>'.

72     return _addRequestID(json, response);
       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

../../../node_modules/.pnpm/[email protected]/node_modules/openai/src/uploads.ts:138:19 - error TS2345: Argument of type 'BlobPart[]' is not assignable to parameter of type '(Blob | BinaryLike)[]'.
  Type 'BlobPart' is not assignable to type 'Blob | BinaryLike'.
    Type 'ArrayBuffer' is not assignable to type 'Blob | BinaryLike'.
      Type 'ArrayBuffer' is missing the following properties from type 'Float64Array': BYTES_PER_ELEMENT, buffer, byteOffset, copyWithin, and 24 more.

138   return new File(bits, name, options);
                      ~~~~


Found 2 errors in 2 files.

Errors  Files
     1  ../../../node_modules/.pnpm/[email protected]/node_modules/openai/src/core.ts:72
     1  ../../../node_modules/.pnpm/[email protected]/node_modules/openai/src/uploads.ts:138
 ELIFECYCLE  Command failed with exit code 2.

I then realised that my IDE auto-imported the type ChatCompletionMessageParam from the /src folder like this: import { ChatCompletionMessageParam } from "openai/src/resources/chat/completions";

This caused typescript to check the types for openai. Once I imported the type correctly (import { ChatCompletionMessageParam } from "openai/resources/chat/completions";), the error disappeared.

thomasdax98 avatar Jan 07 '25 16:01 thomasdax98

Thankfully that footgun will be fixed in v5! https://community.openai.com/t/your-feedback-requested-node-js-sdk-5-0-0-alpha/1063774/4

RobertCraigie avatar Jan 07 '25 16:01 RobertCraigie