Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Internal use of axios causes error inside Cloudflare Workers #30

Closed
chucksellick opened this issue Nov 29, 2022 · 5 comments
Closed

Internal use of axios causes error inside Cloudflare Workers #30

chucksellick opened this issue Nov 29, 2022 · 5 comments
Labels
bug Something isn't working fixed in v4 Issues addressed by v4

Comments

@chucksellick
Copy link

Describe the bug

I am trying to use the client inside a Cloudflare Worker and I get an error as follows:

TypeError: adapter is not a function
    at dispatchRequest (index.js:35781:14)
    at Axios.request (index.js:36049:19)
    at Function.wrap [as request] (index.js:34878:20)

Seems to be a common problem as the way Axios checks for XHR breaks in CF workers which is a reduced node environment:

https://community.cloudflare.com/t/typeerror-e-adapter-s-adapter-is-not-a-function/166469/2

Recommendation is to use fetch instead.

To Reproduce

Try to use API in a cloudflare worker

Code snippets

No response

OS

Windows 10

Node version

Node v16

Library version

openai v3.1.0

@chucksellick chucksellick added the bug Something isn't working label Nov 29, 2022
@aliuq
Copy link

aliuq commented Dec 8, 2022

I got the same error, because of axios adapters used XMLHttpRequest and node http, but workers used fetch to send request, so we should specify a fetch adapter, I found a package @vespaiach/axios-fetch-adapter, you can refer to the following code

const { Configuration, OpenAIApi } = require("openai");
+ const fetchAdapter = require("@vespaiach/axios-fetch-adapter")

const configuration = new Configuration({
  apiKey: process.env.OPENAI_API_KEY,
+  baseOptions: {
+    adapter: fetchAdapter
+  }
});
const openai = new OpenAIApi(configuration);

Import

import { Configuration, OpenAIApi }  from 'openai'
+ import fetchAdapter from '@vespaiach/axios-fetch-adapter'

const configuration = new Configuration({
  apiKey: process.env.OPENAI_API_KEY,
+  baseOptions: {
+    adapter: fetchAdapter
+  }
});
const openai = new OpenAIApi(configuration);

@jimmywarting
Copy link

jimmywarting commented Dec 28, 2022

Think you should ditch axios in favour of using fetch directly instead.
Either use node-fetch, undici or global fetch (NodeJS v18 provides fetch built in - coming from undici)
using fetch instead means it would be more functional inside of service worker, deno, bun.js and any other env. it would be more cross compatible.

also form-data is now a piece of legacy and more proper, spec'ed FormData is available to use.

When logging anything from response.data then it dumps way too much unnecessary information that cluters the hole terminal

if you got problem importing node-fetch@3 b/c it's ESM only then you could opt for something like this: node-fetch/node-fetch#1279 (comment)

@manovotny
Copy link

Looks like this was resurfaced in #47 and I am guessing it will be fixed if / when #45 is merged.

@aroman
Copy link

aroman commented Jun 9, 2023

would love to see this fixed — it's a blocker for using this library in Cloudflare, and as other comments have pointed out, there is no reason to use axios when fetch is widely supported

@rattrayalex rattrayalex added the fixed in v4 Issues addressed by v4 label Jul 10, 2023
@rattrayalex
Copy link
Collaborator

This is fixed in our upcoming v4! Please give it a try on Cloudflare Workers and let us know if you run into any issues.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working fixed in v4 Issues addressed by v4
Projects
None yet
Development

No branches or pull requests

6 participants