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

New builtin fetch alpha #645

Open
RobertCraigie opened this issue Dec 20, 2024 · 9 comments
Open

New builtin fetch alpha #645

RobertCraigie opened this issue Dec 20, 2024 · 9 comments

Comments

@RobertCraigie
Copy link
Collaborator

RobertCraigie commented Dec 20, 2024

We've been working on an alpha version of the SDK that alongside many other cleanups, migrates from node-fetch to builtin fetch.

You can try it out by installing the 0.34.0-alpha.0 version:

pnpm add @anthropic-ai/sdk@alpha

The core happy path of the SDK remains the same, your existing method calls should just work! However you may run into typing issues as we now expect that fetch is defined as a global.

There are many other small breaking changes which you may run into, we'll share a full migration guide as soon as possible.


Please share feedback on anything you think could be improved or if you run into any issues! Hope this is a nice early present for you all :)

Important

Unfortunately we cannot guarantee support over the holiday break (December 20th - January 6th inclusive) but we will go through every comment as soon as possible.

@Nishchit14
Copy link

ky is a great replacement of fetch (it's a wrapper with great DX., sharing here incase you'd like to consider it.

@RobertCraigie
Copy link
Collaborator Author

Thanks! However we're not considering other HTTP libraries as one great benefit of just depending on the builtin fetch function is that, among other things, we now have zero dependencies!

@Nishchit14
Copy link

Agree, But ky has zero deps and works best for browser, Node, Bun etc. Just a suggestion.

Image

@pelikhan
Copy link

pelikhan commented Jan 4, 2025

do you have a migration guide to support httpAgent from https://www.npmjs.com/package/https-proxy-agent ?

@RobertCraigie
Copy link
Collaborator Author

@pelikhan we haven't tested https-proxy-agent directly yet but here's what setting a proxy would look like in a couple different runtimes

Node:

import Anthropic from '@anthropic-ai/sdk';
import * as undici from 'undici';

const dispatcher = new undici.ProxyAgent('http://localhost:8888');

const client = new Anthropic({
  fetch(url, init = {}) {
    return undici.fetch(url, {
      ...(init as unknown as undici.RequestInit),
      dispatcher,
    }) as unknown as Promise<Response>;
  },
});

Bun:

import Anthropic from '@anthropic-ai/sdk';

const client = new Anthropic({
  fetch(url, init) {
    return fetch(url, { ...init, proxy: 'http://localhost:8888' });
  },
});

Deno:

import Anthropic from '@anthropic-ai/sdk';

const httpClient = Deno.createHttpClient({ proxy: { url: 'http://localhost:8888' } });

const client = new Anthropic({
  fetch(url, init) {
    return fetch(url, { ...init, client: httpClient });
  },
});

@pelikhan
Copy link

pelikhan commented Jan 8, 2025

node-openai used this library already

@RobertCraigie
Copy link
Collaborator Author

@pelikhan sorry what do you mean?

@pelikhan
Copy link

pelikhan commented Jan 8, 2025

Here are the docs about using a proxy with node-openai --> https://github.com/openai/openai-node?tab=readme-ov-file#configuring-an-https-agent-eg-for-proxies

@RobertCraigie
Copy link
Collaborator Author

Ah, I think passing the httpAgent client option isn't working right now but the examples I shared should work for now.

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

No branches or pull requests

3 participants