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

Switch from node-fetch to undici #402

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

Ethan-Arrowood
Copy link

Closes #392

const defaultHttpAgent: Agent = new KeepAliveAgent({ keepAlive: true, timeout: 5 * 60 * 1000 });
const defaultHttpsAgent: Agent = new KeepAliveAgent.HttpsAgent({ keepAlive: true, timeout: 5 * 60 * 1000 });
const defaultHttpAgent = new uf.Agent({ keepAliveTimeout: 5 * 60 * 1000 });
const defaultHttpsAgent = new uf.Agent({ keepAliveTimeout: 5 * 60 * 1000 });
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this necessary to get reasonable keepAlive behavior? IIRC, node-fetch didn't do keepAlive by default but if undici does, maybe we don't need to construct a default agent after all?

(note we also have weird timeout shenanigans elsewhere to bump the agent's timeout so that a long timeout for a given request isn't cut short by the agent's timeout; not sure whether that'd still be necessary or would need adjustment.)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd have to go look at the default agent timeout to be sure

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI: undici will honor the keep-alive hint from the server.

Copy link
Collaborator

@rattrayalex rattrayalex left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is looking clean & great so far! Thank you @Ethan-Arrowood !

@rattrayalex
Copy link
Collaborator

One thing to note is that with undici, it looks like response.body isn't a Node Readable stream, which would be breaking change for node-fetch users… any thoughts there?

@@ -125,7 +125,7 @@ export async function toFile(
}
}

return new File(bits, name, options);
return new File(bits as (string | Blob | NodeJS.ArrayBufferView)[], name, options);
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I couldn't for the life of me figure out how to get this type to work. Help appreciated!

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

heh, I believe it, can try to take a look soon… likely thursday…

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Ethan-Arrowood I think undici's type for the fileBits parameter is wrong, their own JSDoc says it accepts ArrayBuffers, but the type annotation only accepts ArrayBufferViews, which ArrayBuffers aren't assignable to.

@Ethan-Arrowood
Copy link
Author

One thing to note is that with undici, it looks like response.body isn't a Node Readable stream, which would be breaking change for node-fetch users… any thoughts there?

Yes this an important distinction. It will be a Node.js Web Stream instead. This is to spec so that code is actually interoperable. node-fetch has been a cause of much frustration for a very long time because they don't do this. Developers using fetch - in any context - should expect it to behave as specified

@rattrayalex
Copy link
Collaborator

This is to spec so that code is actually interoperable.

You mean, interoperable with web streams, not Node ones, right? So it will be breaking? Is there any way we can ease that migration for developers?

@Ethan-Arrowood
Copy link
Author

Current, interoperable with web streams. Also meaning that when you use it in code that runs in both browser and on the server it behaves the same.

There does exist some utilities for users to transform one to the other. And they are fairly close in implementation so some users won't even notice a difference. The key here is that this breaking change is intentional. Understand if you want that to happen in a major version of this library. Otherwise, if this library tried to do something else you'd just be kicking the can down the road. True interoperability should not have anything other than Web Streams. If true interoperability isn't the absolute goal, we could add a transform to this library and keep Node users on Node streams.

@rattrayalex
Copy link
Collaborator

rattrayalex commented Nov 9, 2023 via email

@rattrayalex
Copy link
Collaborator

Hey, sorry, just checking in – are you blocked on me here?

@Ethan-Arrowood
Copy link
Author

No - haven't gotten back around to this. Need to still figure out how to run tests (still haven't created an OpenAI key). Do you have CI you could enable for this PR? I'll update and mark it "ready for review" so GitHub prompts it too.

@rattrayalex
Copy link
Collaborator

Sadly we don't run CI in public for this repo yet, just internally.

The steps to test it locally are:

yarn
OPENAI_API_KEY=… yarn ts-node ecosystem-tests/cli.ts
npx prism mock https://github.com/openai/openai-openapi/raw/master/openapi.yaml & # or in a separate tab.
yarn test

@athyuttamre athyuttamre changed the base branch from master to next November 14, 2023 19:40
@athyuttamre athyuttamre changed the base branch from next to master November 14, 2023 19:41
@Ethan-Arrowood
Copy link
Author

Updated failing types I believe. Still cannot run tests. Not sure what's going wrong. Maybe my local env is in a bad state. I think this is very close, maybe a maintainer more familiar with the shim and test matrix can take it over the finish line.

The most important detail here is that Node.js now has an actual spec compliant Fetch implementation. Its types are different and most importantly it now returns Web Streams instead of Node.js Streams (spec compliant).

This is more expected IMO as fetch should do as fetch is documented to do. Node.js makes available a Readable.fromWeb() that can be used to convert the web streams back to node streams if needed.

@rattrayalex
Copy link
Collaborator

Amazing, thank you so much @Ethan-Arrowood ! Finishing off those tests ourselves sounds totally fair; we have a lot on our plate right not but will try to take a look at getting this over the line soon.

@athyuttamre
Copy link
Collaborator

athyuttamre commented Nov 19, 2023

I've approved running the CI workflow. Looks like it is failing with a module error:

yarn install v1.22.19
[1/4] Resolving packages...
[2/4] Fetching packages...
error [email protected]: The engine "node" is incompatible with this module. Expected version ">= 18". Got "1[6](https://github.com/openai/openai-node/actions/runs/6908472736/job/18819218282?pr=402#step:4:7).20.2"
error Found incompatible module.
info Visit https://yarnpkg.com/en/docs/cli/install for documentation about this command.
Error: Process completed with exit code 1.

https://github.com/openai/openai-node/actions/runs/6908472736/job/18819218282?pr=402

Perhaps we need to update the workflow to use Node 18 rather than 16?

https://github.com/openai/openai-node/blob/master/.github/workflows/ci.yml

@athyuttamre
Copy link
Collaborator

@Ethan-Arrowood can you rebase on master so that the latest CI workflow can be run with Node 18?

@athyuttamre
Copy link
Collaborator

Looks like CI now runs correctly! Some type errors to fix next it looks like.

@Ethan-Arrowood
Copy link
Author

Why are they all coming from the dist folder?

@rattrayalex
Copy link
Collaborator

Thanks Atty!

I think @jedwards1211 is working on these type errors etc – Ethan, you shouldn't need to take a look.

@mmo80
Copy link

mmo80 commented Mar 22, 2024

When will this be merged into master? The DeprecationWarning: The `punycode` module is deprecated. Please use a userland alternative instead. warning is so annoying. Everybody that uses langchain gets this warning.

@rattrayalex rattrayalex mentioned this pull request Mar 24, 2024
1 task
@rattrayalex
Copy link
Collaborator

Sorry for the annoyance.

We're hoping to merge this within the next 6-8 weeks.

It will be a breaking change and there are some deep complexities, which is why it'll take longer than one might hope, but we are actively working on it behind the scenes.

@@ -67,10 +68,11 @@ it(`raw response`, async function () {

// test that we can use node-fetch Response API

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment probably needs updating

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Replace node-fetch with undici
7 participants