Skip to content
Source
Sign inSign up
TanStack/ai

feat(ai-openai): generic openaiCompatible adapter for OpenAI-compatible providers #676

MergedAlemTuzlakmerged 15 commits intomain fromrefs/pull/676/headmerged by AlemTuzlak View on GitHub

AlemTuzlakcommented

Summary

Adds a generic openaiCompatible({ baseURL, apiKey, models }) provider-factory (plus an openaiCompatibleText one-shot helper), exported from @tanstack/ai-openai/compatible, for any provider that implements the OpenAI Chat Completions API — DeepSeek, Moonshot/Kimi, Together, Fireworks, Cerebras, Qwen, Perplexity, NVIDIA NIM, local servers (LM Studio/vLLM/Ollama), and more — without a dedicated package per vendor.

This directly answers the most-repeated community request (discussions #109 Kimi, #151/#153/#155 OpenAI/Anthropic-compatible providers) and supersedes the need for the in-flight per-vendor adapters behind #227 (Z.AI) and #174 (Nebius).

What's included

  • openaiCompatible(config) — configure baseURL/apiKey/models once, select a model per call (the model arg is a type-safe union). Builds the OpenAI SDK client once and reuses it.
  • openaiCompatibleText(model, config) — single-model one-shot helper.
  • Hybrid model declaration — bare strings get optimistic defaults (text/image in, streaming, tools, structured output); createModel(name, { input, features, tools }) declares precise per-model capabilities.
  • api: 'chat-completions' | 'responses' toggle (default chat-completions; Responses for the rare provider like Azure that supports it).
  • Built on the existing @tanstack/openai-base adapters — no new stream/tool logic.

Core change

  • @tanstack/ai's createModel gains a backward-compatible capabilities overload: createModel(name, { input, features, tools, modelOptions }) alongside the existing createModel(name, input). ExtendedModelDef gains optional features/tools. (The capabilities key is modelOptions, matching the existing ExtendedModelDef.modelOptions field and the chat({ modelOptions }) API.)

Tests / docs

  • Unit + type tests for the factory, model-name union, capability resolution, both API modes, and header passthrough. Type tests use the repo's .test.ts convention (expectTypeOf) so both tsc and knip recognize them.
  • Regression guard in @tanstack/openai-base for usage-only terminal chunks with empty choices (the #371 class — confirmed the base already handles it).
  • E2E: new openai-compatible provider wired into the matrix — all 15 specs pass (reuses existing chat-completions fixtures).
  • New docs page docs/adapters/openai-compatible.md with a verified provider/baseURL table covering all known compatible providers, plus an Azure callout; nav + cross-links updated.
  • adapter-configuration agent skill updated.
  • Changesets: @tanstack/ai-openai minor, @tanstack/ai minor.

Test Plan

  • pnpm --filter @tanstack/ai --filter @tanstack/ai-openai --filter @tanstack/openai-base test:types — clean
  • test:lib — ai 965, openai-base 94, ai-openai 178 passing
  • pnpm --filter @tanstack/ai-openai test:build — publint clean
  • pnpm --filter @tanstack/ai-e2e test:e2e -- --grep "openai-compatible" — 15/15 passing
  • pnpm test:knip — clean
  • pnpm test:docs — 0 broken links
  • test:eslint — 0 errors

🤖 Generated with Claude Code

Summary by CodeRabbit

  • New Features

    • Added generic OpenAI-compatible adapter supporting third-party providers that implement the OpenAI Chat Completions API (e.g., DeepSeek, Ollama, local servers).
    • Extended model creation API to support defining capabilities (input modalities, features, tools) alongside existing syntax.
  • Documentation

    • Added comprehensive guide for configuring and using OpenAI-compatible providers.
    • Updated adapter selection documentation with new provider option.
    1. coderabbitai[bot](GitHub profile, opens in a new tab)commented

      Review Change Stack

      📝 Walkthrough

      Walkthrough

      This pull request adds an OpenAI-compatible adapter subpath with type-safe factory helpers and two adapter classes, extends createModel to accept a capabilities object (optional features/tools), and includes types, runtime and type tests, regression tests, documentation, package exports, and E2E integration.

      Changes

      OpenAI-Compatible Adapter and Model Capabilities

      Layer / File(s)Summary
      Core model capabilities API
      packages/ai/src/extend-adapter.ts, packages/ai/src/index.ts, .changeset/create-model-capabilities.md, packages/ai/tests/extend-adapter.test.ts
      ExtendedModelDef gains optional features and tools generics/fields. createModel now supports legacy (name, inputArray) and new (name, capabilities) overloads; introduces ModelCapabilities and applies defaults for input and modelOptions.
      OpenAI-compatible type system and adapters
      packages/ai-openai/src/compatible/types.ts, packages/ai-openai/src/compatible/adapter.ts
      Adds CompatibleModelInput union, ModelNameOf, conditional resolvers (ResolveCompatInput/Options/Tools) with optimistic defaults, CompatibleApi, and two adapters wrapping Chat Completions and Responses endpoints.
      OpenAI-compatible factory functions and tests
      packages/ai-openai/src/compatible/index.ts, packages/ai-openai/tests/compatible.test.ts, packages/ai-openai/tests/compatible-types.test.ts
      Implements openaiCompatible(config) multi-model factory and openaiCompatibleText(model, config) one-shot helper; runtime tests verify client construction, adapter selection, and naming; type tests validate model-name unions and input resolution.
      Package configuration and build
      packages/ai-openai/package.json, packages/ai-openai/vite.config.ts
      Adds ./compatible subpath export to package exports and includes ./src/compatible/index.ts in Vite build entries.
      Type-level and capability overload validation
      packages/ai-openai/tests/compatible-types.test.ts, packages/ai/tests/extend-adapter.test.ts
      Type-compatibility tests validate ModelNameOf, ResolveCompatInput behavior, compile-time model name enforcement, and createModel capabilities overload behavior (features/tools capture and backward-compatible inference).
      Regression testing
      packages/openai-base/tests/chat-completions-empty-choices.test.ts
      Vitest regression suite for empty-choices terminal chunks ensuring TOOL_CALL_END and RUN_FINISHED events are emitted with correct finish reason and token usage.
      Documentation and references
      docs/adapters/openai-compatible.md, docs/adapters/openai.md, docs/config.json, packages/ai/skills/ai-core/adapter-configuration/SKILL.md
      New OpenAI-compatible adapter docs with usage, model declaration options, configuration pass-through, API selection, provider examples (including Azure and local setups), tools example; cross-references and docs navigation updated.
      End-to-end test integration
      testing/e2e/src/lib/types.ts, testing/e2e/src/lib/providers.ts, testing/e2e/src/lib/feature-support.ts, testing/e2e/tests/test-matrix.ts
      Adds openai-compatible provider to E2E provider list, feature-support matrix, and test provider factory using openaiCompatibleText with configurable baseURL and headers.

      Estimated code review effort

      🎯 4 (Complex) | ⏱️ ~45 minutes

      Suggested reviewers

      • jherr
      • tombeckenham
      • crutchcorn

      🐰 A compatible provider hops in with cheer, Models declare features and tools far and near, createModel listens to capabilities told, Adapters connect endpoints both new and old, Docs and tests celebrate the bridge we now steer. 🥕

      🚥 Pre-merge checks | ✅ 4 | ❌ 1

      ❌ Failed checks (1 warning)

      Check nameStatusExplanationResolution
      Docstring Coverage⚠️ WarningDocstring coverage is 42.86% which is insufficient. The required threshold is 80.00%.Write docstrings for the functions missing them to satisfy the coverage threshold.
      ✅ Passed checks (4 passed)
      Check nameStatusExplanation
      Title check✅ PassedThe PR title accurately describes the main change: introducing a generic openaiCompatible adapter for OpenAI-compatible providers in @tanstack/ai-openai.
      Linked Issues check✅ PassedCheck skipped because no linked issues were found for this pull request.
      Out of Scope Changes check✅ PassedCheck skipped because no linked issues were found for this pull request.
      Description check✅ PassedThe PR description comprehensively covers objectives, implementation details, test coverage, and release notes, fully aligned with the template structure.
      ✏️ Tip: You can configure your own custom pre-merge checks in the settings.
      ✨ Finishing Touches
      📝 Generate docstrings
      • Create stacked PR
      • Commit on current branch
      🧪 Generate unit tests (beta)
      • Create PR with unit tests
      • Commit unit tests in branch `feat/openai-compatible-adapter`

      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 and usage tips.
    2. github-actions[bot](GitHub profile, opens in a new tab)commented

      🚀 Changeset Version Preview

      11 package(s) bumped directly, 19 bumped as dependents.

      🟥 Major bumps

      PackageVersionReason
      @tanstack/ai-anthropic0.12.0 → 1.0.0Changeset
      @tanstack/ai-event-client0.4.3 → 1.0.0Changeset
      @tanstack/ai-gemini0.13.0 → 1.0.0Changeset
      @tanstack/ai-grok0.9.2 → 1.0.0Changeset
      @tanstack/ai-groq0.2.9 → 1.0.0Changeset
      @tanstack/ai-ollama0.6.24 → 1.0.0Changeset
      @tanstack/ai-openai0.10.5 → 1.0.0Changeset
      @tanstack/ai-openrouter0.11.0 → 1.0.0Changeset
      @tanstack/openai-base0.5.0 → 1.0.0Changeset
      @tanstack/ai-code-mode0.2.0 → 1.0.0Dependent
      @tanstack/ai-code-mode-skills0.2.0 → 1.0.0Dependent
      @tanstack/ai-elevenlabs0.2.15 → 1.0.0Dependent
      @tanstack/ai-fal0.7.18 → 1.0.0Dependent
      @tanstack/ai-isolate-node0.1.25 → 1.0.0Dependent
      @tanstack/ai-isolate-quickjs0.1.25 → 1.0.0Dependent
      @tanstack/ai-preact0.8.0 → 1.0.0Dependent
      @tanstack/ai-react0.14.0 → 1.0.0Dependent
      @tanstack/ai-react-ui0.8.5 → 1.0.0Dependent
      @tanstack/ai-solid0.12.0 → 1.0.0Dependent
      @tanstack/ai-solid-ui0.7.5 → 1.0.0Dependent
      @tanstack/ai-svelte0.12.0 → 1.0.0Dependent
      @tanstack/ai-vue0.12.0 → 1.0.0Dependent

      🟨 Minor bumps

      PackageVersionReason
      @tanstack/ai0.24.0 → 0.25.0Changeset

      🟩 Patch bumps

      PackageVersionReason
      @tanstack/ai-devtools-core0.4.3 → 0.4.4Changeset
      @tanstack/ai-client0.15.0 → 0.15.1Dependent
      @tanstack/ai-isolate-cloudflare0.2.16 → 0.2.17Dependent
      @tanstack/ai-vue-ui0.2.9 → 0.2.10Dependent
      @tanstack/preact-ai-devtools0.1.46 → 0.1.47Dependent
      @tanstack/react-ai-devtools0.2.46 → 0.2.47Dependent
      @tanstack/solid-ai-devtools0.2.46 → 0.2.47Dependent
    3. nx-cloud[bot](GitHub profile, opens in a new tab)commented

      View your CI Pipeline Execution ↗ for commit 2197233c28f2377aa7cfd70384bed4f965715064

      CommandStatusDurationResult
      nx run-many --targets=build --exclude=examples/...✅ Succeeded1m 16sView ↗

      ☁️ Nx Cloud last updated this comment at 2026-06-01 17:02:59 UTC

    4. pkg-pr-new[bot](GitHub profile, opens in a new tab)commented

      Open in StackBlitz

      @tanstack/ai

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

      @tanstack/ai-anthropic

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

      @tanstack/ai-client

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

      @tanstack/ai-code-mode

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

      @tanstack/ai-code-mode-skills

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

      @tanstack/ai-devtools-core

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

      @tanstack/ai-elevenlabs

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

      @tanstack/ai-event-client

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

      @tanstack/ai-fal

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

      @tanstack/ai-gemini

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

      @tanstack/ai-grok

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

      @tanstack/ai-groq

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

      @tanstack/ai-isolate-cloudflare

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

      @tanstack/ai-isolate-node

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

      @tanstack/ai-isolate-quickjs

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

      @tanstack/ai-ollama

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

      @tanstack/ai-openai

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

      @tanstack/ai-openrouter

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

      @tanstack/ai-preact

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

      @tanstack/ai-react

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

      @tanstack/ai-react-ui

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

      @tanstack/ai-solid

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

      @tanstack/ai-solid-ui

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

      @tanstack/ai-svelte

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

      @tanstack/ai-utils

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

      @tanstack/ai-vue

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

      @tanstack/ai-vue-ui

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

      @tanstack/openai-base

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

      @tanstack/preact-ai-devtools

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

      @tanstack/react-ai-devtools

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

      @tanstack/solid-ai-devtools

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

      commit: 86b4410

    5. 🧹 Nitpick comments (1)
      packages/ai-openai/tests/compatible.test.ts (1)

      81-90: ⚡ Quick win

      Add a one-shot api: 'responses' test case.

      openaiCompatibleText has a responses branch in packages/ai-openai/src/compatible/index.ts (Lines 99-102), but this suite only asserts the default chat path. A small test here would close that regression gap.

      Suggested test addition
       describe('openaiCompatibleText', () => {
         it('builds a single-model Chat Completions adapter', () => {
           const adapter = openaiCompatibleText('model-a', {
             baseURL: 'https://api.example.com/v1',
             apiKey: 'sk-test',
           })
           expect(adapter).toBeInstanceOf(OpenAICompatibleChatAdapter)
           expect(adapter.model).toBe('model-a')
         })
      +
      +  it('builds a single-model Responses adapter when api: "responses"', () => {
      +    const adapter = openaiCompatibleText('model-a', {
      +      baseURL: 'https://api.example.com/v1',
      +      apiKey: 'sk-test',
      +      api: 'responses',
      +    })
      +    expect(adapter).toBeInstanceOf(OpenAICompatibleResponsesAdapter)
      +    expect(adapter.model).toBe('model-a')
      +  })
       })
      🤖 Prompt for AI Agents
      Verify each finding against current code. Fix only still-valid issues, skip the
      rest with a brief reason, keep changes minimal, and validate.
      
      In `@packages/ai-openai/tests/compatible.test.ts` around lines 81 - 90, Add a test
      that calls openaiCompatibleText with api: 'responses' and asserts it returns the
      responses-path adapter: call openaiCompatibleText('model-a', { baseURL:
      'https://api.example.com/v1', apiKey: 'sk-test', api: 'responses' }) and expect
      the result to be an instance of OpenAICompatibleResponsesAdapter and have model
      === 'model-a', mirroring the existing chat-path test that uses
      OpenAICompatibleChatAdapter.
      🤖 Prompt for all review comments with AI agents
      Verify each finding against current code. Fix only still-valid issues, skip the
      rest with a brief reason, keep changes minimal, and validate.
      
      Nitpick comments:
      In `@packages/ai-openai/tests/compatible.test.ts`:
      - Around line 81-90: Add a test that calls openaiCompatibleText with api:
      'responses' and asserts it returns the responses-path adapter: call
      openaiCompatibleText('model-a', { baseURL: 'https://api.example.com/v1', apiKey:
      'sk-test', api: 'responses' }) and expect the result to be an instance of
      OpenAICompatibleResponsesAdapter and have model === 'model-a', mirroring the
      existing chat-path test that uses OpenAICompatibleChatAdapter.

      ℹ️ Review info
      ⚙️ Run configuration

      Configuration used: defaults

      Review profile: CHILL

      Plan: Pro

      Run ID: 12d9bccd-e7e6-411f-9e90-655f54caf9dc

      📥 Commits

      Reviewing files that changed from the base of the PR and between f008c5fe2158261503e3a48c676b3d39526105f0 and 9a02825b04672d8a6034a5e579073d848b809b47.

      📒 Files selected for processing (21)
      • .changeset/create-model-capabilities.md
      • .changeset/openai-compatible-adapter.md
      • docs/adapters/openai-compatible.md
      • docs/adapters/openai.md
      • docs/config.json
      • packages/ai-openai/package.json
      • packages/ai-openai/src/compatible/adapter.ts
      • packages/ai-openai/src/compatible/index.ts
      • packages/ai-openai/src/compatible/types.ts
      • packages/ai-openai/tests/compatible.test-d.ts
      • packages/ai-openai/tests/compatible.test.ts
      • packages/ai-openai/vite.config.ts
      • packages/ai/skills/ai-core/adapter-configuration/SKILL.md
      • packages/ai/src/extend-adapter.test-d.ts
      • packages/ai/src/extend-adapter.ts
      • packages/ai/src/index.ts
      • packages/openai-base/tests/chat-completions-empty-choices.test.ts
      • testing/e2e/src/lib/feature-support.ts
      • testing/e2e/src/lib/providers.ts
      • testing/e2e/src/lib/types.ts
      • testing/e2e/tests/test-matrix.ts
    6. tannerlinsley approved these changes

    7. AlemTuzlak merged this5d6cd28

    8. AlemTuzlak closed this

Sign in to join this conversation.