|
| 1 | +# Migration guide |
| 2 | + |
| 3 | +This guide outlines the changes and steps needed to migrate your codebase to the latest version of the Anthropic TypeScript SDK. |
| 4 | + |
| 5 | +The main changes are that the SDK now relies on the [builtin Web fetch API](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API) instead of `node-fetch` and has zero dependencies. |
| 6 | + |
| 7 | +## Environment requirements |
| 8 | + |
| 9 | +The minimum supported runtime and tooling versions are now: |
| 10 | + |
| 11 | +- Node.js 18.x last LTS (Required for built-in fetch support) |
| 12 | + - This was previously documented as the minimum supported Node.js version but Node.js 16.x mostly worked at runtime; now it will not. |
| 13 | +- TypeScript 4.9 |
| 14 | +- Jest 28 |
| 15 | + |
| 16 | +## Minimum types requirements |
| 17 | + |
| 18 | +### DOM |
| 19 | + |
| 20 | +`tsconfig.json` |
| 21 | + |
| 22 | +```jsonc |
| 23 | +{ |
| 24 | + "target": "ES2015", // note: we recommend ES2020 or higher |
| 25 | + "lib": ["DOM", "DOM.Iterable", "ES2018"] |
| 26 | +} |
| 27 | +``` |
| 28 | + |
| 29 | +### Node.js |
| 30 | + |
| 31 | +`tsconfig.json` |
| 32 | + |
| 33 | +```jsonc |
| 34 | +{ |
| 35 | + "target": "ES2015" // note: we recommend ES2020 or higher |
| 36 | +} |
| 37 | +``` |
| 38 | + |
| 39 | +`package.json` |
| 40 | + |
| 41 | +```json |
| 42 | +{ |
| 43 | + "devDependencies": { |
| 44 | + "@types/node": ">= 18.18.7" |
| 45 | + } |
| 46 | +} |
| 47 | +``` |
| 48 | + |
| 49 | +### Cloudflare Workers |
| 50 | + |
| 51 | +`tsconfig.json` |
| 52 | + |
| 53 | +```jsonc |
| 54 | +{ |
| 55 | + "target": "ES2015", // note: we recommend ES2020 or higher |
| 56 | + "lib": ["ES2020"], // <- needed by @cloudflare/workers-types |
| 57 | + "types": ["@cloudflare/workers-types"] |
| 58 | +} |
| 59 | +``` |
| 60 | + |
| 61 | +`package.json` |
| 62 | + |
| 63 | +```json |
| 64 | +{ |
| 65 | + "devDependencies": { |
| 66 | + "@cloudflare/workers-types": ">= 0.20221111.0" |
| 67 | + } |
| 68 | +} |
| 69 | +``` |
| 70 | + |
| 71 | +### Bun |
| 72 | + |
| 73 | +`tsconfig.json` |
| 74 | + |
| 75 | +```jsonc |
| 76 | +{ |
| 77 | + "target": "ES2015" // note: we recommend ES2020 or higher |
| 78 | +} |
| 79 | +``` |
| 80 | + |
| 81 | +`package.json` |
| 82 | + |
| 83 | +```json |
| 84 | +{ |
| 85 | + "devDependencies": { |
| 86 | + "@types/bun": ">= 1.2.0" |
| 87 | + } |
| 88 | +} |
| 89 | +``` |
| 90 | + |
| 91 | +### Deno |
| 92 | + |
| 93 | +No config needed! |
| 94 | + |
| 95 | +## Breaking changes |
| 96 | + |
| 97 | +### URI encoded path parameters |
| 98 | + |
| 99 | +Path params are now properly encoded by default. If you were manually encoding path parameters before giving them to the SDK, you must now stop doing that and pass the |
| 100 | +param without any encoding applied. |
| 101 | + |
| 102 | +For example: |
| 103 | + |
| 104 | +```diff |
| 105 | +- client.example.retrieve(encodeURIComponent('string/with/slash')) |
| 106 | ++ client.example.retrieve('string/with/slash') // renders example/string%2Fwith%2Fslash |
| 107 | +``` |
| 108 | + |
| 109 | +Previously without the `encodeURIComponent()` call we would have used the path `/example/string/with/slash`; now we'll use `/example/string%2Fwith%2Fslash`. |
| 110 | + |
| 111 | +### Removed `httpAgent` in favor of `fetchOptions` |
| 112 | + |
| 113 | +The `httpAgent` client option has been removed in favor of a [platform-specific `fetchOptions` property](https://github.com/stainless-sdks/anthropic-typescript#fetch-options). |
| 114 | +This change was made as `httpAgent` relied on `node:http` agents which are not supported by any runtime's builtin fetch implementation. |
| 115 | + |
| 116 | +If you were using `httpAgent` for proxy support, check out the [new proxy documentation](https://github.com/stainless-sdks/anthropic-typescript#configuring-proxies). |
| 117 | + |
| 118 | +Before: |
| 119 | + |
| 120 | +```ts |
| 121 | +import Anthropic from '@anthropic-ai/sdk'; |
| 122 | +import http from 'http'; |
| 123 | +import { HttpsProxyAgent } from 'https-proxy-agent'; |
| 124 | + |
| 125 | +// Configure the default for all requests: |
| 126 | +const client = new Anthropic({ |
| 127 | + httpAgent: new HttpsProxyAgent(process.env.PROXY_URL), |
| 128 | +}); |
| 129 | +``` |
| 130 | + |
| 131 | +After: |
| 132 | + |
| 133 | +```ts |
| 134 | +import Anthropic from '@anthropic-ai/sdk'; |
| 135 | +import * as undici from 'undici'; |
| 136 | + |
| 137 | +const proxyAgent = new undici.ProxyAgent(process.env.PROXY_URL); |
| 138 | +const client = new Anthropic({ |
| 139 | + fetchOptions: { |
| 140 | + dispatcher: proxyAgent, |
| 141 | + }, |
| 142 | +}); |
| 143 | +``` |
| 144 | + |
| 145 | +### Removed request options overloads |
| 146 | + |
| 147 | +When making requests with no required body, query or header parameters, you must now explicitly pass `null`, `undefined` or an empty object `{}` to the params argument in order to customise request options. |
| 148 | + |
| 149 | +```diff |
| 150 | +client.example.list(); |
| 151 | +client.example.list({}, { headers: { ... } }); |
| 152 | +client.example.list(null, { headers: { ... } }); |
| 153 | +client.example.list(undefined, { headers: { ... } }); |
| 154 | +- client.example.list({ headers: { ... } }); |
| 155 | ++ client.example.list({}, { headers: { ... } }); |
| 156 | +``` |
| 157 | + |
| 158 | +This affects the following methods: |
| 159 | + |
| 160 | +- `client.messages.batches.list()` |
| 161 | +- `client.models.list()` |
| 162 | +- `client.beta.models.list()` |
| 163 | +- `client.beta.messages.batches.retrieve()` |
| 164 | +- `client.beta.messages.batches.list()` |
| 165 | +- `client.beta.messages.batches.delete()` |
| 166 | +- `client.beta.messages.batches.cancel()` |
| 167 | +- `client.beta.messages.batches.results()` |
| 168 | + |
| 169 | +### Pagination changes |
| 170 | + |
| 171 | +Note that the `for await` syntax is _not_ affected. This still works as-is: |
| 172 | + |
| 173 | +```ts |
| 174 | +// Automatically fetches more pages as needed. |
| 175 | +for await (const betaMessageBatch of client.beta.messages.batches.list()) { |
| 176 | + console.log(betaMessageBatch); |
| 177 | +} |
| 178 | +``` |
| 179 | + |
| 180 | +#### Simplified interface |
| 181 | + |
| 182 | +The pagination interface has been simplified: |
| 183 | + |
| 184 | +```ts |
| 185 | +// Before |
| 186 | +page.nextPageParams(); |
| 187 | +page.nextPageInfo(); |
| 188 | +// Required manually handling { url } | { params } type |
| 189 | + |
| 190 | +// After |
| 191 | +page.nextPageRequestOptions(); |
| 192 | +``` |
| 193 | + |
| 194 | +#### Removed unnecessary classes |
| 195 | + |
| 196 | +Page classes for individual methods are now type aliases: |
| 197 | + |
| 198 | +```ts |
| 199 | +// Before |
| 200 | +export class BetaMessageBatchesPage extends Page<BetaMessageBatch> {} |
| 201 | + |
| 202 | +// After |
| 203 | +export type BetaMessageBatchesPage = Page<BetaMessageBatch>; |
| 204 | +``` |
| 205 | + |
| 206 | +If you were importing these classes at runtime, you'll need to switch to importing the base class or only import them at the type-level. |
| 207 | + |
| 208 | +### File handling |
| 209 | + |
| 210 | +The deprecated `fileFromPath` helper has been removed in favor of native Node.js streams: |
| 211 | + |
| 212 | +```ts |
| 213 | +// Before |
| 214 | +Anthropic.fileFromPath('path/to/file'); |
| 215 | + |
| 216 | +// After |
| 217 | +import fs from 'fs'; |
| 218 | +fs.createReadStream('path/to/file'); |
| 219 | +``` |
| 220 | + |
| 221 | +Note that this function previously only worked on Node.j. If you're using Bun, you can use [`Bun.file`](https://bun.sh/docs/api/file-io) instead. |
| 222 | + |
| 223 | +### Shims removal |
| 224 | + |
| 225 | +Previously you could configure the types that the SDK used like this: |
| 226 | + |
| 227 | +```ts |
| 228 | +// Tell TypeScript and the package to use the global Web fetch instead of node-fetch. |
| 229 | +import '@anthropic-ai/sdk/shims/web'; |
| 230 | +import Anthropic from '@anthropic-ai/sdk'; |
| 231 | +``` |
| 232 | + |
| 233 | +The `@anthropic-ai/sdk/shims` imports have been removed. Your global types must now be [correctly configured](#minimum-types-requirements). |
| 234 | + |
| 235 | +### `@anthropic-ai/sdk/src` directory removed |
| 236 | + |
| 237 | +Previously IDEs may have auto-completed imports from the `@anthropic-ai/sdk/src` directory, however this |
| 238 | +directory was only included for an improved go-to-definition experience and should not have been used at runtime. |
| 239 | + |
| 240 | +If you have any `@anthropic-ai/sdk/src` imports, you must replace it with `@anthropic-ai/sdk`. |
| 241 | + |
| 242 | +```ts |
| 243 | +// Before |
| 244 | +import Anthropic from '@anthropic-ai/sdk/src'; |
| 245 | + |
| 246 | +// After |
| 247 | +import Anthropic from '@anthropic-ai/sdk'; |
| 248 | +``` |
| 249 | + |
| 250 | +### Headers |
| 251 | + |
| 252 | +The `headers` property on `APIError` objects is now an instance of the Web [Headers](https://developer.mozilla.org/en-US/docs/Web/API/Headers) class. It was previously just `Record<string, string | null | undefined>`. |
| 253 | + |
| 254 | +### Removed exports |
| 255 | + |
| 256 | +#### `Response` |
| 257 | + |
| 258 | +```typescript |
| 259 | +// Before |
| 260 | +import { Response } from '@anthropic-ai/sdk'; |
| 261 | + |
| 262 | +// After |
| 263 | +// `Response` must now come from the builtin types |
| 264 | +``` |
| 265 | + |
| 266 | +#### Resource classes |
| 267 | + |
| 268 | +If you were importing resource classes from the root package then you must now import them from the file they are defined in: |
| 269 | + |
| 270 | +```typescript |
| 271 | +// Before |
| 272 | +import { Completions } from '@anthropic-ai/sdk'; |
| 273 | + |
| 274 | +// After |
| 275 | +import { Completions } from '@anthropic-ai/sdk/resources/completions'; |
| 276 | +``` |
| 277 | + |
| 278 | +#### `@anthropic-ai/sdk/core` |
| 279 | + |
| 280 | +The `@anthropic-ai/sdk/core` file was intended to be internal-only but it was publicly accessible, as such it has been refactored and split up into internal files. |
| 281 | + |
| 282 | +If you were relying on anything that was only exported from `@anthropic-ai/sdk/core` and is also not accessible anywhere else, please open an issue and we'll consider adding it to the public API. |
| 283 | + |
| 284 | +#### Cleaned up `@anthropic-ai/sdk/uploads` exports |
| 285 | + |
| 286 | +The following exports have been removed from `@anthropic-ai/sdk/uploads` as they were not intended to be a part of the public API: |
| 287 | + |
| 288 | +- `fileFromPath` |
| 289 | +- `BlobPart` |
| 290 | +- `BlobLike` |
| 291 | +- `FileLike` |
| 292 | +- `ResponseLike` |
| 293 | +- `isResponseLike` |
| 294 | +- `isBlobLike` |
| 295 | +- `isFileLike` |
| 296 | +- `isUploadable` |
| 297 | +- `isMultipartBody` |
| 298 | +- `maybeMultipartFormRequestOptions` |
| 299 | +- `multipartFormRequestOptions` |
| 300 | +- `createForm` |
| 301 | + |
| 302 | +Note that `Uploadable` & `toFile` **are** still exported: |
| 303 | + |
| 304 | +```typescript |
| 305 | +import { type Uploadable, toFile } from '@anthropic-ai/sdk/uploads'; |
| 306 | +``` |
0 commit comments