Skip to content

feat(ai-openai): accept token-provider apiKey on openaiCompatible - #1322

Open
harshlocham wants to merge 2 commits into
TanStack:mainfrom
harshlocham:feat/compatible-token-provider
Open

feat(ai-openai): accept token-provider apiKey on openaiCompatible#1322
harshlocham wants to merge 2 commits into
TanStack:mainfrom
harshlocham:feat/compatible-token-provider

Conversation

@harshlocham

@harshlocham harshlocham commented Sep 4, 2026

Copy link
Copy Markdown
Contributor

openaiCompatible and openaiCompatibleText now accept an OpenAI SDK token-provider apiKey (() => Promise<string>). String keys still work. Azure Entra getBearerTokenProvider type-checks.

Changes

OpenAICompatibleConfig and OpenAICompatibleTextConfig typed apiKey as string. The OpenAI client already accepts string | (() => Promise<string>). This PR widens the public type to NonNullable<ClientOptions['apiKey']> so the two match.

Docs are unchanged. Existing string samples stay valid. An Azure Entra recipe is a follow-up.

Checklist

  • I have followed the steps in the Contributing guide.
  • I have tested code changes locally with pnpm run test:pr, or these tests do not apply to this pull request.
  • I fully understand the code in this pull request, including any code generated with AI assistance.
  • Docs: I updated docs/ for this change, or this change is not user-facing.
  • Changeset: I added a changeset (pnpm changeset), or this PR does not change a published package.

Release Impact

  • This change affects published code, and I have generated a changeset.
  • This change is docs/CI/dev-only (no release).

Testing

Commands run

  1. pnpm --filter @tanstack/ai-openai test:lib — 261 passed
  2. pnpm --filter @tanstack/ai-openai test:types — passed

pnpm test:pr was not run. This is a type-only change in @tanstack/ai-openai.

Manual test

  1. Open a file that calls openaiCompatible.
  2. Pass apiKey: async () => 'token'. Confirm TypeScript accepts it.
  3. Pass apiKey: 'sk-test'. Confirm TypeScript still accepts it.
  4. Omit apiKey. Confirm TypeScript errors.

How this PR makes testing easy

packages/ai-openai/tests/compatible-types.test.ts calls both factories with a token-provider function. tsc includes that file.

Public API change

Before

openaiCompatible({
  baseURL: "https://RESOURCE.openai.azure.com/openai/v1/",
  apiKey: process.env.AZURE_OPENAI_API_KEY!, // string only
  models: ["gpt-4o"],
})

After

openaiCompatible({
  baseURL: "https://RESOURCE.openai.azure.com/openai/v1/",
  apiKey: getBearerTokenProvider(
    new DefaultAzureCredential(),
    "https://ai.azure.com/.default",
  ),
  models: ["gpt-4o"],
})

Risk / rollback

Low. Types only. Revert the PR to restore apiKey: string.

Summary by CodeRabbit

  • Improvements
    • OpenAI-compatible integrations now accept asynchronous token providers for API keys.
    • Standard and text-based compatible configurations support OpenAI SDK-compatible API key providers, enabling dynamic authentication tokens.
  • Release
    • Prepared a patch release for the OpenAI integration package with updated API key typing and validation.

@coderabbitai

coderabbitai Bot commented Sep 4, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Team

Run ID: 31c89d8e-23c7-4da1-979a-21bf1f9056c2

📥 Commits

Reviewing files that changed from the base of the PR and between c2b446d and 020c735.

📒 Files selected for processing (1)
  • .changeset/green-donkeys-admire.md
🚧 Files skipped from review as they are similar to previous changes (1)
  • .changeset/green-donkeys-admire.md

Included review availability: Your plan provides up to 8 included reviews per hour; 7 remain after this review.


📝 Walkthrough

Walkthrough

The compatible OpenAI configuration types now accept OpenAI SDK token-provider functions for apiKey. Tests cover both compatible APIs, and a patch changeset records the package update.

Changes

OpenAI-compatible API key typing

Layer / File(s) Summary
Update API key contracts and validation
packages/ai-openai/src/compatible/types.ts, packages/ai-openai/tests/compatible-types.test.ts, .changeset/green-donkeys-admire.md
Both compatible configuration interfaces use NonNullable<ClientOptions['apiKey']>. Tests verify async token-provider functions for openaiCompatible and openaiCompatibleText. The changeset declares a patch release.

Estimated code review effort: 1 (Trivial) | ~5 minutes

Merge Risk: ⚪ Minimal · up to 87382

The compatible OpenAI configuration update is ready to merge with no identified current risk.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely describes the main change: token-provider apiKey support for openaiCompatible.
Description check ✅ Passed The description explains the change, rationale, testing, public API impact, release impact, and rollback plan. It includes the required checklist and confirms that a changeset was added. The Docs chec…
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check. Docstring coverage is scoped to functions touched by this diff. Analyzed 0 functions across 2…
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@tombeckenham tombeckenham added the ai-review Have an agent review the PR label Sep 4, 2026
@nx-cloud

nx-cloud Bot commented Sep 4, 2026

Copy link
Copy Markdown

View your CI Pipeline Execution ↗ for commit 020c735

Command Status Duration Result
nx run @tanstack/ai-grok-build:build ✅ Succeeded 1s View ↗

☁️ Nx Cloud last updated this comment at 2026-09-04 09:57:46 UTC

@pkg-pr-new

pkg-pr-new Bot commented Sep 4, 2026

Copy link
Copy Markdown

Open in StackBlitz

@tanstack/ai

npm i https://pkg.pr.new/@tanstack/ai@1322

@tanstack/ai-acp

npm i https://pkg.pr.new/@tanstack/ai-acp@1322

@tanstack/ai-angular

npm i https://pkg.pr.new/@tanstack/ai-angular@1322

@tanstack/ai-anthropic

npm i https://pkg.pr.new/@tanstack/ai-anthropic@1322

@tanstack/ai-bedrock

npm i https://pkg.pr.new/@tanstack/ai-bedrock@1322

@tanstack/ai-byteplus

npm i https://pkg.pr.new/@tanstack/ai-byteplus@1322

@tanstack/ai-claude-code

npm i https://pkg.pr.new/@tanstack/ai-claude-code@1322

@tanstack/ai-client

npm i https://pkg.pr.new/@tanstack/ai-client@1322

@tanstack/ai-cloudflare

npm i https://pkg.pr.new/@tanstack/ai-cloudflare@1322

@tanstack/ai-code-mode

npm i https://pkg.pr.new/@tanstack/ai-code-mode@1322

@tanstack/ai-code-mode-snippets

npm i https://pkg.pr.new/@tanstack/ai-code-mode-snippets@1322

@tanstack/ai-codex

npm i https://pkg.pr.new/@tanstack/ai-codex@1322

@tanstack/ai-cohere

npm i https://pkg.pr.new/@tanstack/ai-cohere@1322

@tanstack/ai-compaction

npm i https://pkg.pr.new/@tanstack/ai-compaction@1322

@tanstack/ai-devtools-core

npm i https://pkg.pr.new/@tanstack/ai-devtools-core@1322

@tanstack/ai-durable-stream

npm i https://pkg.pr.new/@tanstack/ai-durable-stream@1322

@tanstack/ai-elevenlabs

npm i https://pkg.pr.new/@tanstack/ai-elevenlabs@1322

@tanstack/ai-event-client

npm i https://pkg.pr.new/@tanstack/ai-event-client@1322

@tanstack/ai-fal

npm i https://pkg.pr.new/@tanstack/ai-fal@1322

@tanstack/ai-gemini

npm i https://pkg.pr.new/@tanstack/ai-gemini@1322

@tanstack/ai-grok

npm i https://pkg.pr.new/@tanstack/ai-grok@1322

@tanstack/ai-grok-build

npm i https://pkg.pr.new/@tanstack/ai-grok-build@1322

@tanstack/ai-groq

npm i https://pkg.pr.new/@tanstack/ai-groq@1322

@tanstack/ai-isolate-cloudflare

npm i https://pkg.pr.new/@tanstack/ai-isolate-cloudflare@1322

@tanstack/ai-isolate-daytona

npm i https://pkg.pr.new/@tanstack/ai-isolate-daytona@1322

@tanstack/ai-isolate-node

npm i https://pkg.pr.new/@tanstack/ai-isolate-node@1322

@tanstack/ai-isolate-quickjs

npm i https://pkg.pr.new/@tanstack/ai-isolate-quickjs@1322

@tanstack/ai-isolate-quickjs-bun

npm i https://pkg.pr.new/@tanstack/ai-isolate-quickjs-bun@1322

@tanstack/ai-llmgateway

npm i https://pkg.pr.new/@tanstack/ai-llmgateway@1322

@tanstack/ai-lovable

npm i https://pkg.pr.new/@tanstack/ai-lovable@1322

@tanstack/ai-mcp

npm i https://pkg.pr.new/@tanstack/ai-mcp@1322

@tanstack/ai-memory

npm i https://pkg.pr.new/@tanstack/ai-memory@1322

@tanstack/ai-mistral

npm i https://pkg.pr.new/@tanstack/ai-mistral@1322

@tanstack/ai-octane

npm i https://pkg.pr.new/@tanstack/ai-octane@1322

@tanstack/ai-ollama

npm i https://pkg.pr.new/@tanstack/ai-ollama@1322

@tanstack/ai-openai

npm i https://pkg.pr.new/@tanstack/ai-openai@1322

@tanstack/ai-opencode

npm i https://pkg.pr.new/@tanstack/ai-opencode@1322

@tanstack/ai-openrouter

npm i https://pkg.pr.new/@tanstack/ai-openrouter@1322

@tanstack/ai-perplexity

npm i https://pkg.pr.new/@tanstack/ai-perplexity@1322

@tanstack/ai-persistence

npm i https://pkg.pr.new/@tanstack/ai-persistence@1322

@tanstack/ai-preact

npm i https://pkg.pr.new/@tanstack/ai-preact@1322

@tanstack/ai-react

npm i https://pkg.pr.new/@tanstack/ai-react@1322

@tanstack/ai-react-ui

npm i https://pkg.pr.new/@tanstack/ai-react-ui@1322

@tanstack/ai-remix

npm i https://pkg.pr.new/@tanstack/ai-remix@1322

@tanstack/ai-sandbox

npm i https://pkg.pr.new/@tanstack/ai-sandbox@1322

@tanstack/ai-sandbox-cloudflare

npm i https://pkg.pr.new/@tanstack/ai-sandbox-cloudflare@1322

@tanstack/ai-sandbox-daytona

npm i https://pkg.pr.new/@tanstack/ai-sandbox-daytona@1322

@tanstack/ai-sandbox-docker

npm i https://pkg.pr.new/@tanstack/ai-sandbox-docker@1322

@tanstack/ai-sandbox-local-process

npm i https://pkg.pr.new/@tanstack/ai-sandbox-local-process@1322

@tanstack/ai-sandbox-sprites

npm i https://pkg.pr.new/@tanstack/ai-sandbox-sprites@1322

@tanstack/ai-sandbox-upstash-box

npm i https://pkg.pr.new/@tanstack/ai-sandbox-upstash-box@1322

@tanstack/ai-sandbox-vercel

npm i https://pkg.pr.new/@tanstack/ai-sandbox-vercel@1322

@tanstack/ai-skills

npm i https://pkg.pr.new/@tanstack/ai-skills@1322

@tanstack/ai-solid

npm i https://pkg.pr.new/@tanstack/ai-solid@1322

@tanstack/ai-solid-ui

npm i https://pkg.pr.new/@tanstack/ai-solid-ui@1322

@tanstack/ai-svelte

npm i https://pkg.pr.new/@tanstack/ai-svelte@1322

@tanstack/ai-utils

npm i https://pkg.pr.new/@tanstack/ai-utils@1322

@tanstack/ai-vercel-gateway

npm i https://pkg.pr.new/@tanstack/ai-vercel-gateway@1322

@tanstack/ai-vertex

npm i https://pkg.pr.new/@tanstack/ai-vertex@1322

@tanstack/ai-vue

npm i https://pkg.pr.new/@tanstack/ai-vue@1322

@tanstack/ai-vue-ui

npm i https://pkg.pr.new/@tanstack/ai-vue-ui@1322

@tanstack/openai-base

npm i https://pkg.pr.new/@tanstack/openai-base@1322

@tanstack/preact-ai-devtools

npm i https://pkg.pr.new/@tanstack/preact-ai-devtools@1322

@tanstack/react-ai-devtools

npm i https://pkg.pr.new/@tanstack/react-ai-devtools@1322

@tanstack/solid-ai-devtools

npm i https://pkg.pr.new/@tanstack/solid-ai-devtools@1322

@tanstack/svelte-ai-devtools

npm i https://pkg.pr.new/@tanstack/svelte-ai-devtools@1322

commit: 020c735

@github-actions github-actions Bot added the waiting-on: author Waiting for the author to respond or update label Sep 4, 2026
harshlocham and others added 2 commits September 4, 2026 18:15
Added support for `openaiCompatible` and `openaiCompatibleText` to accept a token-provider function for the `apiKey`, allowing for better type-checking with Azure Entra and rotating keys. Updated type definitions to use `NonNullable` for `apiKey` in configuration interfaces. Added tests to verify the new functionality.
@harshlocham
harshlocham force-pushed the feat/compatible-token-provider branch from 020c735 to 8738246 Compare September 4, 2026 12:45
@github-actions github-actions Bot added waiting-on: maintainer The ball is in the maintainers’ court and removed waiting-on: author Waiting for the author to respond or update labels Sep 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ai-review Have an agent review the PR waiting-on: maintainer The ball is in the maintainers’ court

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants