@anthropic-ai/sdk 0.11.0 → 0.12.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (47) hide show
  1. package/CHANGELOG.md +38 -0
  2. package/README.md +42 -77
  3. package/_shims/index.d.ts +1 -1
  4. package/_shims/registry.d.ts +1 -1
  5. package/_shims/registry.d.ts.map +1 -1
  6. package/core.d.ts +12 -12
  7. package/core.d.ts.map +1 -1
  8. package/core.js +39 -13
  9. package/core.js.map +1 -1
  10. package/core.mjs +39 -13
  11. package/core.mjs.map +1 -1
  12. package/index.js +4 -4
  13. package/index.mjs +4 -4
  14. package/lib/MessageStream.js +2 -2
  15. package/lib/MessageStream.js.map +1 -1
  16. package/lib/MessageStream.mjs +2 -2
  17. package/lib/MessageStream.mjs.map +1 -1
  18. package/package.json +1 -1
  19. package/resources/beta/messages.d.ts +15 -31
  20. package/resources/beta/messages.d.ts.map +1 -1
  21. package/resources/beta/messages.js +3 -9
  22. package/resources/beta/messages.js.map +1 -1
  23. package/resources/beta/messages.mjs +3 -9
  24. package/resources/beta/messages.mjs.map +1 -1
  25. package/resources/completions.d.ts +14 -21
  26. package/resources/completions.d.ts.map +1 -1
  27. package/resources/completions.js +2 -4
  28. package/resources/completions.js.map +1 -1
  29. package/resources/completions.mjs +2 -4
  30. package/resources/completions.mjs.map +1 -1
  31. package/src/_shims/index.d.ts +1 -1
  32. package/src/_shims/node-runtime.ts +1 -1
  33. package/src/_shims/registry.ts +1 -1
  34. package/src/_shims/web-runtime.ts +1 -1
  35. package/src/core.ts +65 -30
  36. package/src/index.ts +4 -4
  37. package/src/lib/.keep +4 -0
  38. package/src/lib/MessageStream.ts +2 -2
  39. package/src/resources/beta/messages.ts +18 -42
  40. package/src/resources/completions.ts +16 -26
  41. package/src/uploads.ts +2 -2
  42. package/src/version.ts +1 -1
  43. package/uploads.d.ts +2 -2
  44. package/uploads.d.ts.map +1 -1
  45. package/version.d.ts +1 -1
  46. package/version.js +1 -1
  47. package/version.mjs +1 -1
@@ -7,9 +7,9 @@ export declare class Completions extends APIResource {
7
7
  /**
8
8
  * Create a Completion
9
9
  */
10
- create(params: CompletionCreateParamsNonStreaming, options?: Core.RequestOptions): APIPromise<Completion>;
11
- create(params: CompletionCreateParamsStreaming, options?: Core.RequestOptions): APIPromise<Stream<Completion>>;
12
- create(params: CompletionCreateParamsBase, options?: Core.RequestOptions): APIPromise<Stream<Completion> | Completion>;
10
+ create(body: CompletionCreateParamsNonStreaming, options?: Core.RequestOptions): APIPromise<Completion>;
11
+ create(body: CompletionCreateParamsStreaming, options?: Core.RequestOptions): APIPromise<Stream<Completion>>;
12
+ create(body: CompletionCreateParamsBase, options?: Core.RequestOptions): APIPromise<Stream<Completion> | Completion>;
13
13
  }
14
14
  export interface Completion {
15
15
  /**
@@ -41,14 +41,14 @@ export interface Completion {
41
41
  export type CompletionCreateParams = CompletionCreateParamsNonStreaming | CompletionCreateParamsStreaming;
42
42
  export interface CompletionCreateParamsBase {
43
43
  /**
44
- * Body param: The maximum number of tokens to generate before stopping.
44
+ * The maximum number of tokens to generate before stopping.
45
45
  *
46
46
  * Note that our models may stop _before_ reaching this maximum. This parameter
47
47
  * only specifies the absolute maximum number of tokens to generate.
48
48
  */
49
49
  max_tokens_to_sample: number;
50
50
  /**
51
- * Body param: The model that will complete your prompt.
51
+ * The model that will complete your prompt.
52
52
  *
53
53
  * As we improve Claude, we develop new versions of it that you can query. The
54
54
  * `model` parameter controls which version of Claude responds to your request.
@@ -61,7 +61,7 @@ export interface CompletionCreateParamsBase {
61
61
  */
62
62
  model: (string & {}) | 'claude-2.1' | 'claude-instant-1';
63
63
  /**
64
- * Body param: The prompt that you want Claude to complete.
64
+ * The prompt that you want Claude to complete.
65
65
  *
66
66
  * For proper response generation you will need to format your prompt using
67
67
  * alternating `\n\nHuman:` and `\n\nAssistant:` conversational turns. For example:
@@ -78,11 +78,11 @@ export interface CompletionCreateParamsBase {
78
78
  */
79
79
  prompt: string;
80
80
  /**
81
- * Body param: An object describing metadata about the request.
81
+ * An object describing metadata about the request.
82
82
  */
83
83
  metadata?: CompletionCreateParams.Metadata;
84
84
  /**
85
- * Body param: Sequences that will cause the model to stop generating.
85
+ * Sequences that will cause the model to stop generating.
86
86
  *
87
87
  * Our models stop on `"\n\nHuman:"`, and may include additional built-in stop
88
88
  * sequences in the future. By providing the stop_sequences parameter, you may
@@ -90,29 +90,28 @@ export interface CompletionCreateParamsBase {
90
90
  */
91
91
  stop_sequences?: Array<string>;
92
92
  /**
93
- * Body param: Whether to incrementally stream the response using server-sent
94
- * events.
93
+ * Whether to incrementally stream the response using server-sent events.
95
94
  *
96
95
  * See [streaming](https://docs.anthropic.com/claude/reference/streaming) for
97
96
  * details.
98
97
  */
99
98
  stream?: boolean;
100
99
  /**
101
- * Body param: Amount of randomness injected into the response.
100
+ * Amount of randomness injected into the response.
102
101
  *
103
102
  * Defaults to 1. Ranges from 0 to 1. Use temp closer to 0 for analytical /
104
103
  * multiple choice, and closer to 1 for creative and generative tasks.
105
104
  */
106
105
  temperature?: number;
107
106
  /**
108
- * Body param: Only sample from the top K options for each subsequent token.
107
+ * Only sample from the top K options for each subsequent token.
109
108
  *
110
109
  * Used to remove "long tail" low probability responses.
111
110
  * [Learn more technical details here](https://towardsdatascience.com/how-to-sample-from-language-models-682bceb97277).
112
111
  */
113
112
  top_k?: number;
114
113
  /**
115
- * Body param: Use nucleus sampling.
114
+ * Use nucleus sampling.
116
115
  *
117
116
  * In nucleus sampling, we compute the cumulative distribution over all the options
118
117
  * for each subsequent token in decreasing probability order and cut it off once it
@@ -120,10 +119,6 @@ export interface CompletionCreateParamsBase {
120
119
  * `temperature` or `top_p`, but not both.
121
120
  */
122
121
  top_p?: number;
123
- /**
124
- * Header param:
125
- */
126
- 'x-api-key'?: string;
127
122
  }
128
123
  export declare namespace CompletionCreateParams {
129
124
  /**
@@ -144,8 +139,7 @@ export declare namespace CompletionCreateParams {
144
139
  }
145
140
  export interface CompletionCreateParamsNonStreaming extends CompletionCreateParamsBase {
146
141
  /**
147
- * Body param: Whether to incrementally stream the response using server-sent
148
- * events.
142
+ * Whether to incrementally stream the response using server-sent events.
149
143
  *
150
144
  * See [streaming](https://docs.anthropic.com/claude/reference/streaming) for
151
145
  * details.
@@ -154,8 +148,7 @@ export interface CompletionCreateParamsNonStreaming extends CompletionCreatePara
154
148
  }
155
149
  export interface CompletionCreateParamsStreaming extends CompletionCreateParamsBase {
156
150
  /**
157
- * Body param: Whether to incrementally stream the response using server-sent
158
- * events.
151
+ * Whether to incrementally stream the response using server-sent events.
159
152
  *
160
153
  * See [streaming](https://docs.anthropic.com/claude/reference/streaming) for
161
154
  * details.
@@ -1 +1 @@
1
- {"version":3,"file":"completions.d.ts","sourceRoot":"","sources":["../src/resources/completions.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,IAAI,MAAM,wBAAwB,CAAC;AAC/C,OAAO,EAAE,UAAU,EAAE,MAAM,wBAAwB,CAAC;AACpD,OAAO,EAAE,WAAW,EAAE,MAAM,4BAA4B,CAAC;AACzD,OAAO,KAAK,cAAc,MAAM,yCAAyC,CAAC;AAC1E,OAAO,EAAE,MAAM,EAAE,MAAM,6BAA6B,CAAC;AAErD,qBAAa,WAAY,SAAQ,WAAW;IAC1C;;OAEG;IACH,MAAM,CAAC,MAAM,EAAE,kCAAkC,EAAE,OAAO,CAAC,EAAE,IAAI,CAAC,cAAc,GAAG,UAAU,CAAC,UAAU,CAAC;IACzG,MAAM,CACJ,MAAM,EAAE,+BAA+B,EACvC,OAAO,CAAC,EAAE,IAAI,CAAC,cAAc,GAC5B,UAAU,CAAC,MAAM,CAAC,UAAU,CAAC,CAAC;IACjC,MAAM,CACJ,MAAM,EAAE,0BAA0B,EAClC,OAAO,CAAC,EAAE,IAAI,CAAC,cAAc,GAC5B,UAAU,CAAC,MAAM,CAAC,UAAU,CAAC,GAAG,UAAU,CAAC;CAc/C;AAED,MAAM,WAAW,UAAU;IACzB;;;;OAIG;IACH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AM,QAAQ,UAAU,GAAG,cAAc,CAAC,UAAU,CAAC;IACrD,MAAM,QAAQ,sBAAsB,GAAG,cAAc,CAAC,sBAAsB,CAAC;IAC7E,MAAM,QAAQ,kCAAkC,GAAG,cAAc,CAAC,kCAAkC,CAAC;IACrG,MAAM,QAAQ,+BAA+B,GAAG,cAAc,CAAC,+BAA+B,CAAC;CAChG"}
@@ -4,14 +4,12 @@ Object.defineProperty(exports, "__esModule", { value: true });
4
4
  exports.Completions = void 0;
5
5
  const resource_1 = require("@anthropic-ai/sdk/resource");
6
6
  class Completions extends resource_1.APIResource {
7
- create(params, options) {
8
- const { 'x-api-key': xAPIKey, ...body } = params;
7
+ create(body, options) {
9
8
  return this._client.post('/v1/complete', {
10
9
  body,
11
10
  timeout: 600000,
12
11
  ...options,
13
- headers: { 'x-api-key': xAPIKey || '', ...options?.headers },
14
- stream: params.stream ?? false,
12
+ stream: body.stream ?? false,
15
13
  });
16
14
  }
17
15
  }
@@ -1 +1 @@
1
- {"version":3,"file":"completions.js","sourceRoot":"","sources":["../src/resources/completions.ts"],"names":[],"mappings":";AAAA,qDAAqD;;;AAIrD,yDAAyD;AAIzD,MAAa,WAAY,SAAQ,sBAAW;IAa1C,MAAM,CACJ,MAA8B,EAC9B,OAA6B;QAE7B,MAAM,EAAE,WAAW,EAAE,OAAO,EAAE,GAAG,IAAI,EAAE,GAAG,MAAM,CAAC;QACjD,OAAO,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,cAAc,EAAE;YACvC,IAAI;YACJ,OAAO,EAAE,MAAM;YACf,GAAG,OAAO;YACV,OAAO,EAAE,EAAE,WAAW,EAAE,OAAO,IAAI,EAAE,EAAE,GAAG,OAAO,EAAE,OAAO,EAAE;YAC5D,MAAM,EAAE,MAAM,CAAC,MAAM,IAAI,KAAK;SAC/B,CAA4D,CAAC;IAChE,CAAC;CACF;AA1BD,kCA0BC;AA6KD,WAAiB,WAAW;AAK5B,CAAC,EALgB,WAAW,GAAX,mBAAW,KAAX,mBAAW,QAK3B"}
1
+ {"version":3,"file":"completions.js","sourceRoot":"","sources":["../src/resources/completions.ts"],"names":[],"mappings":";AAAA,qDAAqD;;;AAIrD,yDAAyD;AAIzD,MAAa,WAAY,SAAQ,sBAAW;IAa1C,MAAM,CACJ,IAA4B,EAC5B,OAA6B;QAE7B,OAAO,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,cAAc,EAAE;YACvC,IAAI;YACJ,OAAO,EAAE,MAAM;YACf,GAAG,OAAO;YACV,MAAM,EAAE,IAAI,CAAC,MAAM,IAAI,KAAK;SAC7B,CAA4D,CAAC;IAChE,CAAC;CACF;AAxBD,kCAwBC;AAqKD,WAAiB,WAAW;AAK5B,CAAC,EALgB,WAAW,GAAX,mBAAW,KAAX,mBAAW,QAK3B"}
@@ -1,14 +1,12 @@
1
1
  // File generated from our OpenAPI spec by Stainless.
2
2
  import { APIResource } from '@anthropic-ai/sdk/resource';
3
3
  export class Completions extends APIResource {
4
- create(params, options) {
5
- const { 'x-api-key': xAPIKey, ...body } = params;
4
+ create(body, options) {
6
5
  return this._client.post('/v1/complete', {
7
6
  body,
8
7
  timeout: 600000,
9
8
  ...options,
10
- headers: { 'x-api-key': xAPIKey || '', ...options?.headers },
11
- stream: params.stream ?? false,
9
+ stream: body.stream ?? false,
12
10
  });
13
11
  }
14
12
  }
@@ -1 +1 @@
1
- {"version":3,"file":"completions.mjs","sourceRoot":"","sources":["../src/resources/completions.ts"],"names":[],"mappings":"AAAA,qDAAqD;OAI9C,EAAE,WAAW,EAAE,MAAM,4BAA4B;AAIxD,MAAM,OAAO,WAAY,SAAQ,WAAW;IAa1C,MAAM,CACJ,MAA8B,EAC9B,OAA6B;QAE7B,MAAM,EAAE,WAAW,EAAE,OAAO,EAAE,GAAG,IAAI,EAAE,GAAG,MAAM,CAAC;QACjD,OAAO,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,cAAc,EAAE;YACvC,IAAI;YACJ,OAAO,EAAE,MAAM;YACf,GAAG,OAAO;YACV,OAAO,EAAE,EAAE,WAAW,EAAE,OAAO,IAAI,EAAE,EAAE,GAAG,OAAO,EAAE,OAAO,EAAE;YAC5D,MAAM,EAAE,MAAM,CAAC,MAAM,IAAI,KAAK;SAC/B,CAA4D,CAAC;IAChE,CAAC;CACF;AA6KD,WAAiB,WAAW;AAK5B,CAAC,EALgB,WAAW,KAAX,WAAW,QAK3B"}
1
+ {"version":3,"file":"completions.mjs","sourceRoot":"","sources":["../src/resources/completions.ts"],"names":[],"mappings":"AAAA,qDAAqD;OAI9C,EAAE,WAAW,EAAE,MAAM,4BAA4B;AAIxD,MAAM,OAAO,WAAY,SAAQ,WAAW;IAa1C,MAAM,CACJ,IAA4B,EAC5B,OAA6B;QAE7B,OAAO,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,cAAc,EAAE;YACvC,IAAI;YACJ,OAAO,EAAE,MAAM;YACf,GAAG,OAAO;YACV,MAAM,EAAE,IAAI,CAAC,MAAM,IAAI,KAAK;SAC7B,CAA4D,CAAC;IAChE,CAAC;CACF;AAqKD,WAAiB,WAAW;AAK5B,CAAC,EALgB,WAAW,KAAX,WAAW,QAK3B"}
@@ -65,7 +65,7 @@ export type ReadableStream = SelectType<manual.ReadableStream, auto.ReadableStre
65
65
  // @ts-ignore
66
66
  export const ReadableStream: SelectType<typeof manual.ReadableStream, typeof auto.ReadableStream>;
67
67
 
68
- export function getMultipartRequestOptions<T extends {} = Record<string, unknown>>(
68
+ export function getMultipartRequestOptions<T = Record<string, unknown>>(
69
69
  form: FormData,
70
70
  opts: RequestOptions<T>,
71
71
  ): Promise<RequestOptions<T>>;
@@ -43,7 +43,7 @@ async function fileFromPath(path: string, ...args: any[]): Promise<File> {
43
43
  const defaultHttpAgent: Agent = new KeepAliveAgent({ keepAlive: true, timeout: 5 * 60 * 1000 });
44
44
  const defaultHttpsAgent: Agent = new KeepAliveAgent.HttpsAgent({ keepAlive: true, timeout: 5 * 60 * 1000 });
45
45
 
46
- async function getMultipartRequestOptions<T extends {} = Record<string, unknown>>(
46
+ async function getMultipartRequestOptions<T = Record<string, unknown>>(
47
47
  form: fd.FormData,
48
48
  opts: RequestOptions<T>,
49
49
  ): Promise<RequestOptions<T>> {
@@ -13,7 +13,7 @@ export interface Shims {
13
13
  Blob: any;
14
14
  File: any;
15
15
  ReadableStream: any;
16
- getMultipartRequestOptions: <T extends {} = Record<string, unknown>>(
16
+ getMultipartRequestOptions: <T = Record<string, unknown>>(
17
17
  form: Shims['FormData'],
18
18
  opts: RequestOptions<T>,
19
19
  ) => Promise<RequestOptions<T>>;
@@ -84,7 +84,7 @@ export function getRuntime({ manuallyImported }: { manuallyImported?: boolean }
84
84
  }
85
85
  }
86
86
  ),
87
- getMultipartRequestOptions: async <T extends {} = Record<string, unknown>>(
87
+ getMultipartRequestOptions: async <T = Record<string, unknown>>(
88
88
  // @ts-ignore
89
89
  form: FormData,
90
90
  opts: RequestOptions<T>,
package/src/core.ts CHANGED
@@ -217,27 +217,27 @@ export abstract class APIClient {
217
217
  return `stainless-node-retry-${uuid4()}`;
218
218
  }
219
219
 
220
- get<Req extends {}, Rsp>(path: string, opts?: PromiseOrValue<RequestOptions<Req>>): APIPromise<Rsp> {
220
+ get<Req, Rsp>(path: string, opts?: PromiseOrValue<RequestOptions<Req>>): APIPromise<Rsp> {
221
221
  return this.methodRequest('get', path, opts);
222
222
  }
223
223
 
224
- post<Req extends {}, Rsp>(path: string, opts?: PromiseOrValue<RequestOptions<Req>>): APIPromise<Rsp> {
224
+ post<Req, Rsp>(path: string, opts?: PromiseOrValue<RequestOptions<Req>>): APIPromise<Rsp> {
225
225
  return this.methodRequest('post', path, opts);
226
226
  }
227
227
 
228
- patch<Req extends {}, Rsp>(path: string, opts?: PromiseOrValue<RequestOptions<Req>>): APIPromise<Rsp> {
228
+ patch<Req, Rsp>(path: string, opts?: PromiseOrValue<RequestOptions<Req>>): APIPromise<Rsp> {
229
229
  return this.methodRequest('patch', path, opts);
230
230
  }
231
231
 
232
- put<Req extends {}, Rsp>(path: string, opts?: PromiseOrValue<RequestOptions<Req>>): APIPromise<Rsp> {
232
+ put<Req, Rsp>(path: string, opts?: PromiseOrValue<RequestOptions<Req>>): APIPromise<Rsp> {
233
233
  return this.methodRequest('put', path, opts);
234
234
  }
235
235
 
236
- delete<Req extends {}, Rsp>(path: string, opts?: PromiseOrValue<RequestOptions<Req>>): APIPromise<Rsp> {
236
+ delete<Req, Rsp>(path: string, opts?: PromiseOrValue<RequestOptions<Req>>): APIPromise<Rsp> {
237
237
  return this.methodRequest('delete', path, opts);
238
238
  }
239
239
 
240
- private methodRequest<Req extends {}, Rsp>(
240
+ private methodRequest<Req, Rsp>(
241
241
  method: HTTPMethod,
242
242
  path: string,
243
243
  opts?: PromiseOrValue<RequestOptions<Req>>,
@@ -269,9 +269,7 @@ export abstract class APIClient {
269
269
  return null;
270
270
  }
271
271
 
272
- buildRequest<Req extends {}>(
273
- options: FinalRequestOptions<Req>,
274
- ): { req: RequestInit; url: string; timeout: number } {
272
+ buildRequest<Req>(options: FinalRequestOptions<Req>): { req: RequestInit; url: string; timeout: number } {
275
273
  const { method, path, query, headers: headers = {} } = options;
276
274
 
277
275
  const body =
@@ -301,18 +299,7 @@ export abstract class APIClient {
301
299
  headers[this.idempotencyHeader] = options.idempotencyKey;
302
300
  }
303
301
 
304
- const reqHeaders: Record<string, string> = {
305
- ...(contentLength && { 'Content-Length': contentLength }),
306
- ...this.defaultHeaders(options),
307
- ...headers,
308
- };
309
- // let builtin fetch set the Content-Type for multipart bodies
310
- if (isMultipartBody(options.body) && shimsKind !== 'node') {
311
- delete reqHeaders['Content-Type'];
312
- }
313
-
314
- // Strip any headers being explicitly omitted with null
315
- Object.keys(reqHeaders).forEach((key) => reqHeaders[key] === null && delete reqHeaders[key]);
302
+ const reqHeaders = this.buildHeaders({ options, headers, contentLength });
316
303
 
317
304
  const req: RequestInit = {
318
305
  method,
@@ -324,9 +311,35 @@ export abstract class APIClient {
324
311
  signal: options.signal ?? null,
325
312
  };
326
313
 
314
+ return { req, url, timeout };
315
+ }
316
+
317
+ private buildHeaders({
318
+ options,
319
+ headers,
320
+ contentLength,
321
+ }: {
322
+ options: FinalRequestOptions;
323
+ headers: Record<string, string | null | undefined>;
324
+ contentLength: string | null | undefined;
325
+ }): Record<string, string> {
326
+ const reqHeaders: Record<string, string> = {};
327
+ if (contentLength) {
328
+ reqHeaders['content-length'] = contentLength;
329
+ }
330
+
331
+ const defaultHeaders = this.defaultHeaders(options);
332
+ applyHeadersMut(reqHeaders, defaultHeaders);
333
+ applyHeadersMut(reqHeaders, headers);
334
+
335
+ // let builtin fetch set the Content-Type for multipart bodies
336
+ if (isMultipartBody(options.body) && shimsKind !== 'node') {
337
+ delete reqHeaders['content-type'];
338
+ }
339
+
327
340
  this.validateHeaders(reqHeaders, headers);
328
341
 
329
- return { req, url, timeout };
342
+ return reqHeaders;
330
343
  }
331
344
 
332
345
  /**
@@ -358,15 +371,15 @@ export abstract class APIClient {
358
371
  return APIError.generate(status, error, message, headers);
359
372
  }
360
373
 
361
- request<Req extends {}, Rsp>(
374
+ request<Req, Rsp>(
362
375
  options: PromiseOrValue<FinalRequestOptions<Req>>,
363
376
  remainingRetries: number | null = null,
364
377
  ): APIPromise<Rsp> {
365
378
  return new APIPromise(this.makeRequest(options, remainingRetries));
366
379
  }
367
380
 
368
- private async makeRequest(
369
- optionsInput: PromiseOrValue<FinalRequestOptions>,
381
+ private async makeRequest<Req>(
382
+ optionsInput: PromiseOrValue<FinalRequestOptions<Req>>,
370
383
  retriesRemaining: number | null,
371
384
  ): Promise<APIResponseProps> {
372
385
  const options = await optionsInput;
@@ -428,7 +441,7 @@ export abstract class APIClient {
428
441
  return new PagePromise<PageClass, Item>(this, request, Page);
429
442
  }
430
443
 
431
- buildURL<Req extends Record<string, unknown>>(path: string, query: Req | null | undefined): string {
444
+ buildURL<Req>(path: string, query: Req | null | undefined): string {
432
445
  const url =
433
446
  isAbsoluteURL(path) ?
434
447
  new URL(path)
@@ -602,7 +615,7 @@ export abstract class AbstractPage<Item> implements AsyncIterable<Item> {
602
615
  );
603
616
  }
604
617
  const nextOptions = { ...this.options };
605
- if ('params' in nextInfo) {
618
+ if ('params' in nextInfo && typeof nextOptions.query === 'object') {
606
619
  nextOptions.query = { ...nextOptions.query, ...nextInfo.params };
607
620
  } else if ('url' in nextInfo) {
608
621
  const params = [...Object.entries(nextOptions.query || {}), ...nextInfo.url.searchParams.entries()];
@@ -700,7 +713,7 @@ export type Headers = Record<string, string | null | undefined>;
700
713
  export type DefaultQuery = Record<string, string | undefined>;
701
714
  export type KeysEnum<T> = { [P in keyof Required<T>]: true };
702
715
 
703
- export type RequestOptions<Req extends {} = Record<string, unknown> | Readable> = {
716
+ export type RequestOptions<Req = unknown | Record<string, unknown> | Readable> = {
704
717
  method?: HTTPMethod;
705
718
  path?: string;
706
719
  query?: Req | undefined;
@@ -737,7 +750,7 @@ const requestOptionsKeys: KeysEnum<RequestOptions> = {
737
750
  __binaryResponse: true,
738
751
  };
739
752
 
740
- export const isRequestOptions = (obj: unknown): obj is RequestOptions<Record<string, unknown> | Readable> => {
753
+ export const isRequestOptions = (obj: unknown): obj is RequestOptions => {
741
754
  return (
742
755
  typeof obj === 'object' &&
743
756
  obj !== null &&
@@ -746,7 +759,7 @@ export const isRequestOptions = (obj: unknown): obj is RequestOptions<Record<str
746
759
  );
747
760
  };
748
761
 
749
- export type FinalRequestOptions<Req extends {} = Record<string, unknown> | Readable> = RequestOptions<Req> & {
762
+ export type FinalRequestOptions<Req = unknown | Record<string, unknown> | Readable> = RequestOptions<Req> & {
750
763
  method: HTTPMethod;
751
764
  path: string;
752
765
  };
@@ -1013,6 +1026,28 @@ export function hasOwn(obj: Object, key: string): boolean {
1013
1026
  return Object.prototype.hasOwnProperty.call(obj, key);
1014
1027
  }
1015
1028
 
1029
+ /**
1030
+ * Copies headers from "newHeaders" onto "targetHeaders",
1031
+ * using lower-case for all properties,
1032
+ * ignoring any keys with undefined values,
1033
+ * and deleting any keys with null values.
1034
+ */
1035
+ function applyHeadersMut(targetHeaders: Headers, newHeaders: Headers): void {
1036
+ for (const k in newHeaders) {
1037
+ if (!hasOwn(newHeaders, k)) continue;
1038
+ const lowerKey = k.toLowerCase();
1039
+ if (!lowerKey) continue;
1040
+
1041
+ const val = newHeaders[k];
1042
+
1043
+ if (val === null) {
1044
+ delete targetHeaders[lowerKey];
1045
+ } else if (val !== undefined) {
1046
+ targetHeaders[lowerKey] = val;
1047
+ }
1048
+ }
1049
+ }
1050
+
1016
1051
  export function debug(action: string, ...args: any[]) {
1017
1052
  if (typeof process !== 'undefined' && process.env['DEBUG'] === 'true') {
1018
1053
  console.log(`Anthropic:DEBUG:${action}`, ...args);
package/src/index.ts CHANGED
@@ -136,17 +136,17 @@ export class Anthropic extends Core.APIClient {
136
136
  }
137
137
 
138
138
  protected override validateHeaders(headers: Core.Headers, customHeaders: Core.Headers) {
139
- if (this.apiKey && headers['X-Api-Key']) {
139
+ if (this.apiKey && headers['x-api-key']) {
140
140
  return;
141
141
  }
142
- if (customHeaders['X-Api-Key'] === null) {
142
+ if (customHeaders['x-api-key'] === null) {
143
143
  return;
144
144
  }
145
145
 
146
- if (this.authToken && headers['Authorization']) {
146
+ if (this.authToken && headers['authorization']) {
147
147
  return;
148
148
  }
149
- if (customHeaders['Authorization'] === null) {
149
+ if (customHeaders['authorization'] === null) {
150
150
  return;
151
151
  }
152
152
 
package/src/lib/.keep ADDED
@@ -0,0 +1,4 @@
1
+ File generated from our OpenAPI spec by Stainless.
2
+
3
+ This directory can be used to store custom files to expand the SDK.
4
+ It is ignored by Stainless code generation and its content (other than this keep file) won't be touched.
@@ -95,7 +95,7 @@ export class MessageStream implements AsyncIterable<MessageStreamEvent> {
95
95
  runner._run(() =>
96
96
  runner._createMessage(
97
97
  messages,
98
- { ...params, stream: true, 'anthropic-beta': 'messages-2023-12-15' },
98
+ { ...params, stream: true },
99
99
  { ...options, headers: { ...options?.headers, 'X-Stainless-Helper-Method': 'stream' } },
100
100
  ),
101
101
  );
@@ -132,7 +132,7 @@ export class MessageStream implements AsyncIterable<MessageStreamEvent> {
132
132
  }
133
133
  this.#beginRequest();
134
134
  const stream = await messages.create(
135
- { ...params, stream: true, 'anthropic-beta': 'messages-2023-12-15' },
135
+ { ...params, stream: true },
136
136
  { ...options, signal: this.controller.signal },
137
137
  );
138
138
  this._connected();
@@ -14,31 +14,25 @@ export class Messages extends APIResource {
14
14
  *
15
15
  * The Messages API is currently in beta.
16
16
  */
17
- create(params: MessageCreateParamsNonStreaming, options?: Core.RequestOptions): APIPromise<Message>;
17
+ create(body: MessageCreateParamsNonStreaming, options?: Core.RequestOptions): APIPromise<Message>;
18
18
  create(
19
- params: MessageCreateParamsStreaming,
19
+ body: MessageCreateParamsStreaming,
20
20
  options?: Core.RequestOptions,
21
21
  ): APIPromise<Stream<MessageStreamEvent>>;
22
22
  create(
23
- params: MessageCreateParamsBase,
23
+ body: MessageCreateParamsBase,
24
24
  options?: Core.RequestOptions,
25
25
  ): APIPromise<Stream<MessageStreamEvent> | Message>;
26
26
  create(
27
- params: MessageCreateParams,
27
+ body: MessageCreateParams,
28
28
  options?: Core.RequestOptions,
29
29
  ): APIPromise<Message> | APIPromise<Stream<MessageStreamEvent>> {
30
- const { 'anthropic-beta': anthropicBeta, 'x-api-key': xAPIKey, ...body } = params;
31
30
  return this._client.post('/v1/messages', {
32
31
  body,
33
32
  timeout: 600000,
34
33
  ...options,
35
- headers: {
36
- 'Anthropic-Beta': 'messages-2023-12-15',
37
- 'anthropic-beta': anthropicBeta,
38
- 'x-api-key': xAPIKey || '',
39
- ...options?.headers,
40
- },
41
- stream: params.stream ?? false,
34
+ headers: { 'Anthropic-Beta': 'messages-2023-12-15', ...options?.headers },
35
+ stream: body.stream ?? false,
42
36
  }) as APIPromise<Message> | APIPromise<Stream<MessageStreamEvent>>;
43
37
  }
44
38
 
@@ -218,7 +212,7 @@ export type MessageCreateParams = MessageCreateParamsNonStreaming | MessageCreat
218
212
 
219
213
  export interface MessageCreateParamsBase {
220
214
  /**
221
- * Body param: The maximum number of tokens to generate before stopping.
215
+ * The maximum number of tokens to generate before stopping.
222
216
  *
223
217
  * Note that our models may stop _before_ reaching this maximum. This parameter
224
218
  * only specifies the absolute maximum number of tokens to generate.
@@ -230,7 +224,7 @@ export interface MessageCreateParamsBase {
230
224
  max_tokens: number;
231
225
 
232
226
  /**
233
- * Body param: Input messages.
227
+ * Input messages.
234
228
  *
235
229
  * Our models are trained to operate on alternating `user` and `assistant`
236
230
  * conversational turns. When creating a new `Message`, you specify the prior
@@ -293,7 +287,7 @@ export interface MessageCreateParamsBase {
293
287
  messages: Array<MessageParam>;
294
288
 
295
289
  /**
296
- * Body param: The model that will complete your prompt.
290
+ * The model that will complete your prompt.
297
291
  *
298
292
  * As we improve Claude, we develop new versions of it that you can query. The
299
293
  * `model` parameter controls which version of Claude responds to your request.
@@ -307,22 +301,12 @@ export interface MessageCreateParamsBase {
307
301
  model: string;
308
302
 
309
303
  /**
310
- * Header param: Beta version header.
311
- *
312
- * During the beta, the Messages API requires sending the
313
- * `anthropic-beta: messages-2023-12-15` header. This is a beta-specific header
314
- * that is required in addition to the normal `anthropic-version` header. If you
315
- * are using our SDKs, both of these headers will be sent automatically.
316
- */
317
- 'anthropic-beta': string;
318
-
319
- /**
320
- * Body param: An object describing metadata about the request.
304
+ * An object describing metadata about the request.
321
305
  */
322
306
  metadata?: MessageCreateParams.Metadata;
323
307
 
324
308
  /**
325
- * Body param: Custom text sequences that will cause the model to stop generating.
309
+ * Custom text sequences that will cause the model to stop generating.
326
310
  *
327
311
  * Our models will normally stop when they have naturally completed their turn,
328
312
  * which will result in a response `stop_reason` of `"end_turn"`.
@@ -335,8 +319,7 @@ export interface MessageCreateParamsBase {
335
319
  stop_sequences?: Array<string>;
336
320
 
337
321
  /**
338
- * Body param: Whether to incrementally stream the response using server-sent
339
- * events.
322
+ * Whether to incrementally stream the response using server-sent events.
340
323
  *
341
324
  * See [streaming](https://docs.anthropic.com/claude/reference/streaming) for
342
325
  * details.
@@ -344,7 +327,7 @@ export interface MessageCreateParamsBase {
344
327
  stream?: boolean;
345
328
 
346
329
  /**
347
- * Body param: System prompt.
330
+ * System prompt.
348
331
  *
349
332
  * A system prompt is a way of providing context and instructions to Claude, such
350
333
  * as specifying a particular goal or role. See our
@@ -353,7 +336,7 @@ export interface MessageCreateParamsBase {
353
336
  system?: string;
354
337
 
355
338
  /**
356
- * Body param: Amount of randomness injected into the response.
339
+ * Amount of randomness injected into the response.
357
340
  *
358
341
  * Defaults to 1. Ranges from 0 to 1. Use temp closer to 0 for analytical /
359
342
  * multiple choice, and closer to 1 for creative and generative tasks.
@@ -361,7 +344,7 @@ export interface MessageCreateParamsBase {
361
344
  temperature?: number;
362
345
 
363
346
  /**
364
- * Body param: Only sample from the top K options for each subsequent token.
347
+ * Only sample from the top K options for each subsequent token.
365
348
  *
366
349
  * Used to remove "long tail" low probability responses.
367
350
  * [Learn more technical details here](https://towardsdatascience.com/how-to-sample-from-language-models-682bceb97277).
@@ -369,7 +352,7 @@ export interface MessageCreateParamsBase {
369
352
  top_k?: number;
370
353
 
371
354
  /**
372
- * Body param: Use nucleus sampling.
355
+ * Use nucleus sampling.
373
356
  *
374
357
  * In nucleus sampling, we compute the cumulative distribution over all the options
375
358
  * for each subsequent token in decreasing probability order and cut it off once it
@@ -377,11 +360,6 @@ export interface MessageCreateParamsBase {
377
360
  * `temperature` or `top_p`, but not both.
378
361
  */
379
362
  top_p?: number;
380
-
381
- /**
382
- * Header param:
383
- */
384
- 'x-api-key'?: string;
385
363
  }
386
364
 
387
365
  export namespace MessageCreateParams {
@@ -405,8 +383,7 @@ export namespace MessageCreateParams {
405
383
 
406
384
  export interface MessageCreateParamsNonStreaming extends MessageCreateParamsBase {
407
385
  /**
408
- * Body param: Whether to incrementally stream the response using server-sent
409
- * events.
386
+ * Whether to incrementally stream the response using server-sent events.
410
387
  *
411
388
  * See [streaming](https://docs.anthropic.com/claude/reference/streaming) for
412
389
  * details.
@@ -416,8 +393,7 @@ export interface MessageCreateParamsNonStreaming extends MessageCreateParamsBase
416
393
 
417
394
  export interface MessageCreateParamsStreaming extends MessageCreateParamsBase {
418
395
  /**
419
- * Body param: Whether to incrementally stream the response using server-sent
420
- * events.
396
+ * Whether to incrementally stream the response using server-sent events.
421
397
  *
422
398
  * See [streaming](https://docs.anthropic.com/claude/reference/streaming) for
423
399
  * details.