Skip to content

fix: forward providerType to chat API for custom provider support#114

Merged
wyuc merged 5 commits intoTHU-MAIC:mainfrom
nkmohit:fix/custom-provider-chat-support
Mar 22, 2026
Merged

fix: forward providerType to chat API for custom provider support#114
wyuc merged 5 commits intoTHU-MAIC:mainfrom
nkmohit:fix/custom-provider-chat-support

Conversation

@nkmohit
Copy link
Contributor

@nkmohit nkmohit commented Mar 19, 2026

Summary

Fixes chat failures for custom LLM providers by forwarding providerType and requiresApiKey through the chat request path and resolving chat models with the shared resolveModel() helper. Previously, /api/chat used an older provider resolution path and could fail with Unknown provider: custom-... Please provide providerType. for custom providers that worked elsewhere in the app.

Related Issues

Fixes #116

Changes

  • Added providerType and requiresApiKey to StatelessChatRequest
  • Forwarded provider metadata from components/chat/use-chat-sessions.ts in chat, resume, and discussion request flows
  • Refactored app/api/chat/route.ts to use the shared resolveModel() helper
  • Removed the older manual chat provider resolution path that could not resolve custom-* providers without explicit provider metadata

Type of Change

  • Bug fix (non-breaking change that fixes an issue)
  • New feature (non-breaking change that adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation update
  • Refactoring (no functional changes)
  • CI/CD or build changes

Verification

Steps to reproduce / test

  1. Configure a custom provider in Settings with a valid provider type, base URL, and model
  2. Select that custom provider/model in the app
  3. Send a message in the chat UI
  4. Confirm chat no longer fails with Unknown provider: custom-... Please provide providerType.

What you personally verified

  • Verified the chat request type and payload now include providerType and requiresApiKey
  • Verified /api/chat now resolves models through resolveModel(), matching the provider-aware path used by other API routes
  • Ran pnpm exec tsc --noEmit
  • Ran pnpm lint app/api/chat/route.ts components/chat/use-chat-sessions.ts lib/types/chat.ts
  • Ran pnpm build
  • Verified GET /api/health returned success locally
  • Verified a direct POST /api/chat request with a custom provider model and providerType streamed a normal SSE response instead of returning the previous 500
  • Verified the old failure still reproduces if providerType is omitted, confirming the fix addresses the intended bug

Evidence

  • CI passes (pnpm check && pnpm lint && npx tsc --noEmit)
  • Manually tested locally
  • Screenshots / recordings attached (if UI changes)
    before:
Screenshot 2026-03-19 at 1 50 30 PM

after:
Screenshot 2026-03-19 at 1 51 14 PM

Checklist

  • My code follows the project's coding style
  • I have performed a self-review of my code
  • I have added/updated documentation as needed
  • My changes do not introduce new warnings

Copy link
Contributor

@wyuc wyuc left a comment

Choose a reason for hiding this comment

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

Replaces the hand-rolled provider resolution in chat route with the shared resolveModel() helper. All three call sites covered, net -13 lines. LGTM.

@wyuc wyuc mentioned this pull request Mar 22, 2026
13 tasks
@wyuc wyuc merged commit d0c324c into THU-MAIC:main Mar 22, 2026
1 check passed
@nkmohit nkmohit deleted the fix/custom-provider-chat-support branch March 22, 2026 16:27
wyuc added a commit that referenced this pull request Mar 23, 2026
)

PR #114 refactored the chat route to use resolveModel() but replaced the
server-resolved API key with the raw client-sent body.apiKey for both the
401 check and the statelessGenerate call. This broke server-configured
providers (e.g. OPENAI_API_KEY env var) where the client sends no key.

- Add apiKey field to ResolvedModel interface so callers can access the
  effective key after server-side fallback resolution
- Use the resolved apiKey for the 401 guard and pass it to statelessGenerate

Fixes #220

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
cosarah pushed a commit that referenced this pull request Mar 23, 2026
)

* fix: use resolved API key in chat route instead of client-sent key (#220)

PR #114 refactored the chat route to use resolveModel() but replaced the
server-resolved API key with the raw client-sent body.apiKey for both the
401 check and the statelessGenerate call. This broke server-configured
providers (e.g. OPENAI_API_KEY env var) where the client sends no key.

- Add apiKey field to ResolvedModel interface so callers can access the
  effective key after server-side fallback resolution
- Use the resolved apiKey for the 401 guard and pass it to statelessGenerate

Fixes #220

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

* fix: allow keyless providers (e.g. local Ollama) to bypass API key check

Add requiresApiKey !== false guard so providers that don't need an API
key are not rejected by the 401 check.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
ifishcool pushed a commit to box3lab/Linksy that referenced this pull request Mar 23, 2026
…HU-MAIC#221)

* fix: use resolved API key in chat route instead of client-sent key (THU-MAIC#220)

PR THU-MAIC#114 refactored the chat route to use resolveModel() but replaced the
server-resolved API key with the raw client-sent body.apiKey for both the
401 check and the statelessGenerate call. This broke server-configured
providers (e.g. OPENAI_API_KEY env var) where the client sends no key.

- Add apiKey field to ResolvedModel interface so callers can access the
  effective key after server-side fallback resolution
- Use the resolved apiKey for the 401 guard and pass it to statelessGenerate

Fixes THU-MAIC#220

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

* fix: allow keyless providers (e.g. local Ollama) to bypass API key check

Add requiresApiKey !== false guard so providers that don't need an API
key are not rejected by the 401 check.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
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.

[Bug]: Chat fails with "Unknown provider: custom-xxx" for custom providers

2 participants