anthropics / anthropic-sdk-typescript

Access to Anthropic's safety-first language model APIs
https://www.npmjs.com/package/@anthropic-ai/sdk
MIT License
744 stars 79 forks source link

Typescript compliation issue - Type 'RequestInit' is not assignable to type 'never' #367

Open YaronRosh opened 8 months ago

YaronRosh commented 8 months ago

having compilation issue with the @anthropic-ai/bedrock-sdk, using in node js + ts

../node_modules/@anthropic-ai/bedrock-sdk/client.d.ts:45:5 - error TS2416: Property 'buildRequest' in type 'AnthropicBedrock' is not assignable to the same property in base type 'APIClient'.
  Type '{ (options: FinalRequestOptions<unknown>): { req: RequestInit; url: string; timeout: number; }; (options: FinalRequestOptions<unknown>): { ...; }; }' is not assignable to type '<Req>(options: FinalRequestOptions<Req>) => { req: never; url: string; timeout: number; }'.
    Call signature return types '{ req: RequestInit; url: string; timeout: number; }' and '{ req: never; url: string; timeout: number; }' are incompatible.
      The types of 'req' are incompatible between these types.
        Type 'RequestInit' is not assignable to type 'never'.

45     buildRequest(options: Core.FinalRequestOptions<unknown>): {
       ~~~~~~~~~~~~

Found 1 error in ../node_modules/@anthropic-ai/bedrock-sdk/client.d.ts:45

using @anthropic-ai/bedrock-sdk: 0.9.2 typescript: 5.2.2

RobertCraigie commented 8 months ago

Can you share your tsconfig? This is likely an issue in your config.

YaronRosh commented 8 months ago
{
  "compilerOptions": {
    "outDir": "dist",
     "incremental": true,
    "sourceMap": true,
    "lib": ["esnext", "dom"],
    "jsx": "react",
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true,
    "baseUrl": ".",
    "module": "commonjs",
    "rootDir": ".",
    "target": "es6",
    "noImplicitAny": false,
    "moduleResolution": "Node",
    "allowJs": true,
    "resolveJsonModule": true
  },
  "include": [
    "src",
    "__tests__"
  ],
  "exclude": [
    "node_modules",
    "./_isolated_",
    "./workspaces",
    "**/dist"
  ]
}
rattrayalex commented 8 months ago

Can you try "moduleResolution": "nodeNext" instead of "Node"?

If that presents other challenges for you, you can try adding this line before your first import of the package:

import '@anthropic-ai/sdk/shims/node'

More information here: https://github.com/anthropics/anthropic-sdk-typescript/tree/main/src/_shims#readme

YaronRosh commented 8 months ago

it now fails on something else with or without the shim import '@anthropic-ai/sdk/shims/node'

../node_modules/@anthropic-ai/bedrock-sdk/client.d.ts:41:15 - error TS2416: Property 'prepareRequest' in type 'AnthropicBedrock' is not assignable to the same property in base type 'APIClient'.
  Type '(request: RequestInit, { url, options }: { url: string; options: import("/Users/yaronrosh/logzio/gaia-hermes-ws/node_modules/@anthropic-ai/sdk/core").FinalRequestOptions<unknown>; }) => Promise<...>' is not assignable to type '(request: import("/Users/yaronrosh/logzio/gaia-hermes-ws/node_modules/@types/node-fetch/index").RequestInit, { url, options }: { url: string; options: import("/Users/yaronrosh/logzio/gaia-hermes-ws/node_modules/@anthropic-ai/sdk/core").FinalRequestOptions<unknown>; }) => Promise<...>'.
    Types of parameters 'request' and 'request' are incompatible.
      Type 'import("/node_modules/@types/node-fetch/index").RequestInit' is not assignable to type 'RequestInit'.
        Types of property 'body' are incompatible.
          Type 'import("/node_modules/@types/node-fetch/index").BodyInit' is not assignable to type 'BodyInit'.
            Type 'ReadableStream' is not assignable to type 'BodyInit'.
              Type 'ReadableStream' is missing the following properties from type 'ReadableStream<any>': locked, cancel, getReader, pipeThrough, and 3 more.

41     protected prepareRequest(request: RequestInit, { url, options }: {
                 ~~~~~~~~~~~~~~

../node_modules/@anthropic-ai/bedrock-sdk/client.d.ts:45:5 - error TS2416: Property 'buildRequest' in type 'AnthropicBedrock' is not assignable to the same property in base type 'APIClient'.
  Type '(options: FinalRequestOptions<unknown>) => { req: RequestInit; url: string; timeout: number; }' is not assignable to type '<Req>(options: FinalRequestOptions<Req>) => { req: RequestInit; url: string; timeout: number; }'.
    Call signature return types '{ req: RequestInit; url: string; timeout: number; }' and '{ req: RequestInit; url: string; timeout: number; }' are incompatible.
      The types of 'req.body' are incompatible between these types.
        Type 'BodyInit' is not assignable to type 'import("/Users/yaronrosh/logzio/gaia-hermes-ws/node_modules/@types/node-fetch/index").BodyInit'.
          Type 'Blob' is not assignable to type 'BodyInit'.
            Type 'Blob' is missing the following properties from type 'URLSearchParams': append, delete, entries, forEach, and 8 more.

45     buildRequest(options: Core.FinalRequestOptions<unknown>): {
       ~~~~~~~~~~~~
rattrayalex commented 8 months ago

Got it. Does "moduleResolution": "nodeNext"help?

amosd92 commented 8 months ago

Answering on behalf of Yaron, it didn't help as well.

rattrayalex commented 8 months ago

hmm… that's surprising. Can you share a codesandbox with minimal repro?

S-unya commented 3 months ago

I also get exactly the same error when I set "module": "NodeNext", "moduleResolution": "NodeNext",. I also started from the same error, pretty much copying the example:

import AnthropicBedrock from '@anthropic-ai/bedrock-sdk';

const createInput = async (messages) => {
  const client = new AnthropicBedrock({
    awsAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
    awsSecretKey: process.env.AWS_ACCESS_KEY_ID,
    awsRegion: process.env.AWS_DEFAULT_REGION,
  });

  const message = await client.messages.create({
    model: 'anthropic.claude-3-5-sonnet-20240620-v1:0',
    max_tokens: 256,
    messages
  });

  return message;
}
rattrayalex commented 3 months ago

Can you share a codesandbox with a minimal repro? Without more details, we can't really directly fix this.

You might also try import '@anthropic-ai/sdk/shims/node' or import '@anthropic-ai/sdk/shims/web' before your first import of the SDK.

S-unya commented 3 months ago

This repro is pretty much the same as the docs; please run npm run build in this, https://codesandbox.io/p/sandbox/angry-thunder-ll36v2. Note it is the tsc command that fails.

I worked around this by converting the ts file to a js file with JSDOC comments, but that is a work around, not a "solution"

rattrayalex commented 3 months ago

Thanks, we'll take a look!

RobertCraigie commented 2 months ago

@S-unya unfortunately I'm getting a "Sandbox not found" error message from https://codesandbox.io/p/sandbox/angry-thunder-ll36v2, is it private?