Skip to content

feat: Allow users to set AbortSignal inside per request config to cancel http request #459

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

Merged
merged 1 commit into from
Apr 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3,853 changes: 1,937 additions & 1,916 deletions api-report/genai-node.api.md

Large diffs are not rendered by default.

3,853 changes: 1,937 additions & 1,916 deletions api-report/genai-web.api.md

Large diffs are not rendered by default.

3,853 changes: 1,937 additions & 1,916 deletions api-report/genai.api.md

Large diffs are not rendered by default.

35 changes: 35 additions & 0 deletions sdk-samples/abort_signal.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* @license
* Copyright 2025 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import {GoogleGenAI} from '@google/genai';

const GEMINI_API_KEY = process.env.GEMINI_API_KEY;

async function abortStreaming() {
const ai = new GoogleGenAI({vertexai: false, apiKey: GEMINI_API_KEY});
const abortController = new AbortController();
const abortSignal = abortController.signal;
const response = await ai.models.generateContentStream({
model: 'gemini-2.0-flash',
contents: 'Tell me a stroy in 300 words?',
config: {
abortSignal: abortSignal,
},
});

for await (const chunk of response) {
const text = chunk.text;
console.debug(text);
abortController.abort();
}
}

async function main() {
await abortStreaming().catch((e) =>
console.error('got expected abort error', e),
);
}

main();
18 changes: 16 additions & 2 deletions src/_api_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,10 @@ export interface HttpRequest {
* Optional set of customizable configuration for HTTP requests.
*/
httpOptions?: HttpOptions;
/**
* Optional abort signal which can be used to cancel the request.
*/
abortSignal?: AbortSignal;
}

/**
Expand Down Expand Up @@ -358,6 +362,7 @@ export class ApiClient {
requestInit = await this.includeExtraHttpOptionsToRequestInit(
requestInit,
patchedHttpOptions,
request.abortSignal,
);
return this.unaryApiCall(url, requestInit, request.httpMethod);
}
Expand Down Expand Up @@ -412,18 +417,27 @@ export class ApiClient {
requestInit = await this.includeExtraHttpOptionsToRequestInit(
requestInit,
patchedHttpOptions,
request.abortSignal,
);
return this.streamApiCall(url, requestInit, request.httpMethod);
}

private async includeExtraHttpOptionsToRequestInit(
requestInit: RequestInit,
httpOptions: HttpOptions,
abortSignal?: AbortSignal,
): Promise<RequestInit> {
if (httpOptions && httpOptions.timeout && httpOptions.timeout > 0) {
if ((httpOptions && httpOptions.timeout) || abortSignal) {
const abortController = new AbortController();
const signal = abortController.signal;
setTimeout(() => abortController.abort(), httpOptions.timeout);
if (httpOptions.timeout && httpOptions?.timeout > 0) {
setTimeout(() => abortController.abort(), httpOptions.timeout);
}
if (abortSignal) {
abortSignal.addEventListener('abort', () => {
abortController.abort();
});
}
requestInit.signal = signal;
}
requestInit.headers = await this.getHeadersInternal(httpOptions);
Expand Down
10 changes: 10 additions & 0 deletions src/caches.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ export class Caches extends BaseModule {
body: JSON.stringify(body),
httpMethod: 'POST',
httpOptions: params.config?.httpOptions,
abortSignal: params.config?.abortSignal,
})
.then((httpResponse) => {
return httpResponse.json();
Expand Down Expand Up @@ -130,6 +131,7 @@ export class Caches extends BaseModule {
body: JSON.stringify(body),
httpMethod: 'POST',
httpOptions: params.config?.httpOptions,
abortSignal: params.config?.abortSignal,
})
.then((httpResponse) => {
return httpResponse.json();
Expand Down Expand Up @@ -184,6 +186,7 @@ export class Caches extends BaseModule {
body: JSON.stringify(body),
httpMethod: 'GET',
httpOptions: params.config?.httpOptions,
abortSignal: params.config?.abortSignal,
})
.then((httpResponse) => {
return httpResponse.json();
Expand Down Expand Up @@ -218,6 +221,7 @@ export class Caches extends BaseModule {
body: JSON.stringify(body),
httpMethod: 'GET',
httpOptions: params.config?.httpOptions,
abortSignal: params.config?.abortSignal,
})
.then((httpResponse) => {
return httpResponse.json();
Expand Down Expand Up @@ -272,6 +276,7 @@ export class Caches extends BaseModule {
body: JSON.stringify(body),
httpMethod: 'DELETE',
httpOptions: params.config?.httpOptions,
abortSignal: params.config?.abortSignal,
})
.then((httpResponse) => {
return httpResponse.json();
Expand Down Expand Up @@ -304,6 +309,7 @@ export class Caches extends BaseModule {
body: JSON.stringify(body),
httpMethod: 'DELETE',
httpOptions: params.config?.httpOptions,
abortSignal: params.config?.abortSignal,
})
.then((httpResponse) => {
return httpResponse.json();
Expand Down Expand Up @@ -359,6 +365,7 @@ export class Caches extends BaseModule {
body: JSON.stringify(body),
httpMethod: 'PATCH',
httpOptions: params.config?.httpOptions,
abortSignal: params.config?.abortSignal,
})
.then((httpResponse) => {
return httpResponse.json();
Expand Down Expand Up @@ -393,6 +400,7 @@ export class Caches extends BaseModule {
body: JSON.stringify(body),
httpMethod: 'PATCH',
httpOptions: params.config?.httpOptions,
abortSignal: params.config?.abortSignal,
})
.then((httpResponse) => {
return httpResponse.json();
Expand Down Expand Up @@ -436,6 +444,7 @@ export class Caches extends BaseModule {
body: JSON.stringify(body),
httpMethod: 'GET',
httpOptions: params.config?.httpOptions,
abortSignal: params.config?.abortSignal,
})
.then((httpResponse) => {
return httpResponse.json();
Expand Down Expand Up @@ -471,6 +480,7 @@ export class Caches extends BaseModule {
body: JSON.stringify(body),
httpMethod: 'GET',
httpOptions: params.config?.httpOptions,
abortSignal: params.config?.abortSignal,
})
.then((httpResponse) => {
return httpResponse.json();
Expand Down
4 changes: 4 additions & 0 deletions src/files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ export class Files extends BaseModule {
body: JSON.stringify(body),
httpMethod: 'GET',
httpOptions: params.config?.httpOptions,
abortSignal: params.config?.abortSignal,
})
.then((httpResponse) => {
return httpResponse.json();
Expand Down Expand Up @@ -180,6 +181,7 @@ export class Files extends BaseModule {
body: JSON.stringify(body),
httpMethod: 'POST',
httpOptions: params.config?.httpOptions,
abortSignal: params.config?.abortSignal,
})
.then((httpResponse) => {
return httpResponse.json();
Expand Down Expand Up @@ -235,6 +237,7 @@ export class Files extends BaseModule {
body: JSON.stringify(body),
httpMethod: 'GET',
httpOptions: params.config?.httpOptions,
abortSignal: params.config?.abortSignal,
})
.then((httpResponse) => {
return httpResponse.json();
Expand Down Expand Up @@ -292,6 +295,7 @@ export class Files extends BaseModule {
body: JSON.stringify(body),
httpMethod: 'DELETE',
httpOptions: params.config?.httpOptions,
abortSignal: params.config?.abortSignal,
})
.then((httpResponse) => {
return httpResponse.json();
Expand Down
15 changes: 15 additions & 0 deletions src/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ export class Models extends BaseModule {
body: JSON.stringify(body),
httpMethod: 'POST',
httpOptions: params.config?.httpOptions,
abortSignal: params.config?.abortSignal,
})
.then((httpResponse) => {
return httpResponse.json();
Expand Down Expand Up @@ -227,6 +228,7 @@ export class Models extends BaseModule {
body: JSON.stringify(body),
httpMethod: 'POST',
httpOptions: params.config?.httpOptions,
abortSignal: params.config?.abortSignal,
})
.then((httpResponse) => {
return httpResponse.json();
Expand Down Expand Up @@ -271,6 +273,7 @@ export class Models extends BaseModule {
body: JSON.stringify(body),
httpMethod: 'POST',
httpOptions: params.config?.httpOptions,
abortSignal: params.config?.abortSignal,
}) as Promise<AsyncGenerator<types.HttpResponse>>;

return response.then(async function* (
Expand Down Expand Up @@ -307,6 +310,7 @@ export class Models extends BaseModule {
body: JSON.stringify(body),
httpMethod: 'POST',
httpOptions: params.config?.httpOptions,
abortSignal: params.config?.abortSignal,
}) as Promise<AsyncGenerator<types.HttpResponse>>;

return response.then(async function* (
Expand Down Expand Up @@ -373,6 +377,7 @@ export class Models extends BaseModule {
body: JSON.stringify(body),
httpMethod: 'POST',
httpOptions: params.config?.httpOptions,
abortSignal: params.config?.abortSignal,
})
.then((httpResponse) => {
return httpResponse.json();
Expand Down Expand Up @@ -408,6 +413,7 @@ export class Models extends BaseModule {
body: JSON.stringify(body),
httpMethod: 'POST',
httpOptions: params.config?.httpOptions,
abortSignal: params.config?.abortSignal,
})
.then((httpResponse) => {
return httpResponse.json();
Expand Down Expand Up @@ -471,6 +477,7 @@ export class Models extends BaseModule {
body: JSON.stringify(body),
httpMethod: 'POST',
httpOptions: params.config?.httpOptions,
abortSignal: params.config?.abortSignal,
})
.then((httpResponse) => {
return httpResponse.json();
Expand Down Expand Up @@ -506,6 +513,7 @@ export class Models extends BaseModule {
body: JSON.stringify(body),
httpMethod: 'POST',
httpOptions: params.config?.httpOptions,
abortSignal: params.config?.abortSignal,
})
.then((httpResponse) => {
return httpResponse.json();
Expand Down Expand Up @@ -556,6 +564,7 @@ export class Models extends BaseModule {
body: JSON.stringify(body),
httpMethod: 'GET',
httpOptions: params.config?.httpOptions,
abortSignal: params.config?.abortSignal,
})
.then((httpResponse) => {
return httpResponse.json();
Expand Down Expand Up @@ -584,6 +593,7 @@ export class Models extends BaseModule {
body: JSON.stringify(body),
httpMethod: 'GET',
httpOptions: params.config?.httpOptions,
abortSignal: params.config?.abortSignal,
})
.then((httpResponse) => {
return httpResponse.json();
Expand Down Expand Up @@ -640,6 +650,7 @@ export class Models extends BaseModule {
body: JSON.stringify(body),
httpMethod: 'POST',
httpOptions: params.config?.httpOptions,
abortSignal: params.config?.abortSignal,
})
.then((httpResponse) => {
return httpResponse.json();
Expand Down Expand Up @@ -675,6 +686,7 @@ export class Models extends BaseModule {
body: JSON.stringify(body),
httpMethod: 'POST',
httpOptions: params.config?.httpOptions,
abortSignal: params.config?.abortSignal,
})
.then((httpResponse) => {
return httpResponse.json();
Expand Down Expand Up @@ -737,6 +749,7 @@ export class Models extends BaseModule {
body: JSON.stringify(body),
httpMethod: 'POST',
httpOptions: params.config?.httpOptions,
abortSignal: params.config?.abortSignal,
})
.then((httpResponse) => {
return httpResponse.json();
Expand Down Expand Up @@ -807,6 +820,7 @@ export class Models extends BaseModule {
body: JSON.stringify(body),
httpMethod: 'POST',
httpOptions: params.config?.httpOptions,
abortSignal: params.config?.abortSignal,
})
.then((httpResponse) => {
return httpResponse.json();
Expand Down Expand Up @@ -841,6 +855,7 @@ export class Models extends BaseModule {
body: JSON.stringify(body),
httpMethod: 'POST',
httpOptions: params.config?.httpOptions,
abortSignal: params.config?.abortSignal,
})
.then((httpResponse) => {
return httpResponse.json();
Expand Down
3 changes: 3 additions & 0 deletions src/operations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ export class Operations extends BaseModule {
body: JSON.stringify(body),
httpMethod: 'GET',
httpOptions: params.config?.httpOptions,
abortSignal: params.config?.abortSignal,
})
.then((httpResponse) => {
return httpResponse.json();
Expand Down Expand Up @@ -115,6 +116,7 @@ export class Operations extends BaseModule {
body: JSON.stringify(body),
httpMethod: 'GET',
httpOptions: params.config?.httpOptions,
abortSignal: params.config?.abortSignal,
})
.then((httpResponse) => {
return httpResponse.json();
Expand Down Expand Up @@ -158,6 +160,7 @@ export class Operations extends BaseModule {
body: JSON.stringify(body),
httpMethod: 'POST',
httpOptions: params.config?.httpOptions,
abortSignal: params.config?.abortSignal,
})
.then((httpResponse) => {
return httpResponse.json();
Expand Down
Loading