Skip to content

Support AbortSignal in AI Gateway binding.#6537

Merged
kentonv merged 2 commits into
mainfrom
kenton/aig-abort-signal
Apr 9, 2026
Merged

Support AbortSignal in AI Gateway binding.#6537
kentonv merged 2 commits into
mainfrom
kenton/aig-abort-signal

Conversation

@kentonv

@kentonv kentonv commented Apr 8, 2026

Copy link
Copy Markdown
Member

I had intended to cover this in #6379 but missed it somehow.

(Written by GPT 5.4.)

I also added the ability to opt out of RPC by passing {beta: false}, because AbortSignal doesn't work over RPC yet, and Gadgets needs this, and Gadgets sets the "experimental" flag so would otherwise get RPC by default.

kentonv added 2 commits April 8, 2026 18:41
I had intended to cover this in #6379 but missed it somehow.

(Written by GPT 5.4.)
`AbortSignal` does not work over RPC, but people with the "experimental" compat flag are opted into RPC.

Gadgets desperately needs AbortSignal support, but uses "experimental".

Since there was already a "beta" flag to explicitly opt in, I made it so explicitly setting it `false` opts out, overriding the "experimental" flag.
@kentonv kentonv requested review from a team as code owners April 8, 2026 23:54
@kentonv kentonv requested review from G4brym and NuroDev April 8, 2026 23:54
@ask-bonk

ask-bonk Bot commented Apr 8, 2026

Copy link
Copy Markdown
Contributor

I've reviewed all the changed files and surrounding context. Let me now analyze the changes:

Changes summary:

  1. ai-api.ts: Changes RPC opt-in logic from aiBindingExperimental || options?.beta === true to options?.beta ?? aiBindingExperimental — allows explicit opt-out via {beta: false}
  2. aig-api.ts: Adds signal?: AbortSignal to run() options, passes it through to fetch()
  3. Tests: Two new test cases for AbortSignal (pre-aborted and in-flight abort)
  4. Type definitions: Updated across all snapshot files

Analysis:

  • The options?.beta ?? aiBindingExperimental logic is correct. The nullish coalescing operator (??) falls through to aiBindingExperimental only when options?.beta is undefined or null, not when it's false. This enables the documented opt-out behavior. The comment explains the rationale clearly.

  • The AbortSignal pass-through in aig-api.ts is straightforward — it just forwards signal from options to fetch(). The conditional assignment (if (options?.signal)) is fine since undefined is the default for signal in RequestInit anyway.

  • The test coverage is good: tests both pre-aborted signal and in-flight abort via AbortController.

  • Type definitions are consistent across all four snapshot files and aig.d.ts.

  • No backward compatibility concerns: signal is a new optional field, and the beta: false opt-out is additive behavior.

  • No security concerns: the signal just propagates standard abort semantics to the underlying fetch.

  • No compat flag needed: this is purely additive (new optional parameter, new opt-out capability).

I don't see any actionable issues with this PR.

LGTM

github run

@kentonv

kentonv commented Apr 9, 2026

Copy link
Copy Markdown
Member Author

By the way, our own ai-gateway-provider package (vercel AI SDK plugin) is already passing this option along when the application passes an AbortSignal to AI SDK... because I asked @threepointone to implement that before I realized the runtime itself didn't support it, and he dutifully did as I asked.

As a result, when the binding started using RPC (for "experimental" users) last week, it actually broke anyone using ai-gateway-provider, AbortSignal, and the "experimental" flag together. Which may very well have been only me, but it was a spontaneous breakage in prod, which confused me.

@G4brym G4brym left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

lgtm
i was the one enabling RPC when experimental flag is active last week, sorry about breaking your worker

@kentonv kentonv merged commit 121f7f9 into main Apr 9, 2026
37 of 41 checks passed
@kentonv kentonv deleted the kenton/aig-abort-signal branch April 9, 2026 15:26
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.

3 participants