Rewrite ai-gateway-provider: new API, backward-compatible deprecations, comprehensive tests#409
Open
threepointone wants to merge 6 commits intomainfrom
Open
Rewrite ai-gateway-provider: new API, backward-compatible deprecations, comprehensive tests#409threepointone wants to merge 6 commits intomainfrom
threepointone wants to merge 6 commits intomainfrom
Conversation
🦋 Changeset detectedLatest commit: 0adac9c The changes in this PR will be included in the next version bump. This PR includes changesets to release 2 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
commit: |
Major rewrite of packages/ai-gateway-provider: new public API (createAIGateway and createAIGatewayFallback) that wraps standard AI SDK providers instead of providing many internal provider wrappers. Removes src/auth.ts and all src/providers/* wrapper files, drops optionalDependencies and subpath provider exports, and updates package.json scripts and peerDeps. index.ts was refactored to add resolveProvider, capture/restore fetch logic, request normalization, BYOK auth stripping, cf-aig-* gateway option headers, and binding vs REST dispatch. README and new chat.md updated with usage, options, supported providers, and Workers guidance. Added comprehensive unit, integration, and e2e tests and accompanying changeset files.
Delete the local patch for @ai-sdk/google-vertex and clear patchedDependencies in package.json. pnpm-lock.yaml was updated (regenerated) to remove references to the patch and related patched package entries — the patch is no longer required.
Delete the CODEOWNERS file which previously assigned /packages/ai-gateway-provider/* to @G4brym, removing that ownership entry and any associated automatic reviews/notifications.
Allow request cancellation for AI Gateway calls by adding an optional AbortSignal parameter. Updated AiGatewayBinding.run signature to accept options with signal, extended dispatchToGateway to accept and forward a signal to either the binding.run call or the underlying fetch, and wired options.abortSignal through AiGatewayChatLanguageModel and AiGatewayFallbackModel when dispatching. This enables callers to abort in-flight gateway requests.
Introduce new createAIGateway API that wraps standard AI SDK providers and add deprecation-compatible shims for old provider imports. Add provider subpath exports in package.json and optionalDependencies for @ai-sdk/* packages. Implement robustness and bug fixes: normalize headers, clone responses for model fetch, restore original fetch via try/finally, propagate abort signals to bindings/fetch, handle non-JSON 400/401 gateway bodies, and strip BYOK auth headers. Add deprecated createAiGateway compatibility layer and type aliases with one-time console warnings, update README migration docs, and add unit/robustness tests covering these behaviors.
4207348 to
c846865
Compare
Add a new README section showing how to combine createAIGateway with the AI SDK's createProviderRegistry to route requests by model name across multiple providers. Includes a TypeScript example wiring OpenAI and Anthropic through the same gateway and explains that provider-specific features are preserved while routing traffic for caching, logging, and analytics.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
New API for
ai-gateway-provider— users now bring their own AI SDK provider and wrap it withcreateAIGateway. The old API (createAiGateway,ai-gateway-provider/providers/*) still works with deprecation warnings, making this a non-breaking minor release.Before:
After:
New features
createAIGateway— wraps any AI SDK provider, supports binding + REST API modescreateAIGatewayFallback— cross-provider fallback in a single gateway requestproviderName— explicit provider name for custom base URLs / proxiesbyok: true— strips provider auth headers for BYOK / Unified BillingbyokAliasandzdrgateway options (were missing entirely)resolveProviderexported as a utilityBug fixes
cf-skip-cache→cf-aig-skip-cache,cf-cache-ttl→cf-aig-cache-ttl)Authorization: Bearer unusedwas forwarded to the provider)try/finallyso model instances recover from errorscacheTtl: 0silently ignored (falsy check →!== undefined)SyntaxErrorinstead of returning the raw responsefeedResponseToModelreturning a consumedResponseon repeated calls — now usesresp.clone()finallycleanup not running whencaptureModelRequestfails partway through the model listbinding.run()andfetch()callsproviderNameheaderKeyfields from provider registrybigintfrommetadatatype (was a runtime crash —JSON.stringifycan't serialize bigints)Deprecated (all still work, with one-time console warnings)
createAiGateway()createAIGateway()/createAIGatewayFallback()ai-gateway-provider/providers/*subpath imports@ai-sdk/*AiGatewaySettingstypeAiGatewayConfigAiGatewayAPISettingstypeAiGatewayAPIConfigAiGatewayBindingSettingstypeAiGatewayBindingConfigAiGatewayRetiestypeAiGatewayRetriesDeprecated shims preserve the old provider subpath imports by re-exporting from
@ai-sdk/*packages (kept asoptionalDependencies). The oldauthWrapper/CF_TEMP_TOKENBYOK mechanism is intentionally dropped — it was leaking keys. Users doing BYOK should usebyok: trueon the new API.All deprecated APIs will be removed in the next major version.
Tests
try/finallycorrectness)createAiGatewaycompat (callable,.chat(), array fallback, binding)providerName, custom base URLs, compat endpointworkers-ai-provider(chat, stream, multi-turn, tools, structured output, embeddings, images via AI Gateway binding)Notes for reviewers
The fetch-hijack pattern is inherited from the old code. It works by replacing
model.config.fetchwith a spy to capture the request, then replaying with the gateway response. We now save and restore the original fetch in atry/finallyblock. Concurrent use of the same model instance is still unsafe (but eachgateway("gpt-4o")call creates a fresh instance, so this isn't a practical concern).Workers AI doesn't go through
createAIGateway— it usesbinding.run()(RPC, not HTTP) and has its own built-ingatewayoption. This is documented in both READMEs. Theworkers-ai-providerchange is test-only (adding?gateway=param support to the test worker).BYOK requires
byok: trueon the gateway config. Without it, the provider's auth header is forwarded to the gateway. Withbyok: true, auth headers (authorization,x-api-key,api-key,x-goog-api-key) are stripped before dispatch. Headers are normalized to lowercase first so stripping works regardless of original casing.The
compatprovider entry enables the Unified API / Dynamic Routes flow. Users point the OpenAI SDK athttps://gateway.ai.cloudflare.com/v1/compatand use model names likegoogle-ai-studio/gemini-2.5-proordynamic/my-route..envfiles —test/integration/.envcontains real credentials and is gitignored. Only.env.exampleis committed.Test plan
npm run test:unit)npx tsc --noEmit)