Skip to content

fix: use fallback provider default models - #788

Closed
mturac wants to merge 1 commit into
rohitg00:mainfrom
mturac:fix/issue-778-fallback-provider-models
Closed

mturac wants to merge 1 commit into
rohitg00:mainfrom
mturac:fix/issue-778-fallback-provider-models

Conversation

@mturac

@mturac mturac commented Jun 2, 2026 •

Copy link
Copy Markdown

Summary

  • build fallback provider configs from each fallback provider's own model defaults/env overrides
  • keep the primary provider model from leaking into cross-provider fallback requests
  • add a regression covering OpenAI -> Gemini failover request bodies

Fixes #778

Validation

  • git diff --cached --check
  • npm test -- test/provider-fallback-model.test.ts
  • npm run build

Summary by CodeRabbit

  • Tests

    • Added test coverage for fallback provider behavior and model configuration across multiple providers.
  • Refactor

    • Fallback provider selection now respects per-provider environment variable defaults for model and base URL configuration.

@vercel

vercel Bot commented Jun 2, 2026

Copy link
Copy Markdown

@mturac is attempting to deploy a commit to the rohitg00's projects Team on Vercel.

A member of the Team first needs to authorize it.

@coderabbitai

coderabbitai Bot commented Jun 2, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

A new providerDefaultConfig() helper resolves each fallback provider's own model and base URL from environment variables. createFallbackProvider() now uses this helper instead of passing the primary provider's model to all fallbacks. A new test verifies each fallback provider receives its own correct model.

Changes

Fallback provider per-model resolution

Layer / File(s) Summary
Provider default config resolution helper
src/providers/index.ts
New providerDefaultConfig() function maps fallback provider types to their own configured models and base URLs from environment variables, replacing hardcoded primary-model inheritance.
Fallback provider integration and test coverage
src/providers/index.ts, test/provider-fallback-model.test.ts
createFallbackProvider() calls the new helper instead of constructing inline fbConfig. Test suite verifies primary 429 → fallback failover uses per-provider models (OpenAI gpt-4o-mini, Gemini gemini-2.5-flash) and resolves to fallback response.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

Poem

🐰 A fix for a bungled fallback chain,
Where Gemini'd masquerade as GPT's refrain—
Now each provider brings its own true name,
No cross-wired models, no 404 shame! ✨

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% 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 name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title directly matches the main change: using each fallback provider's default models instead of inheriting the primary provider's model.
Linked Issues check ✅ Passed The code changes fully address the root cause in #778 by creating provider-specific default configs and prevent model name inheritance across fallback providers.
Out of Scope Changes check ✅ Passed All changes are directly scoped to fixing the fallback provider model inheritance issue and adding a regression test; no unrelated modifications present.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ 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 and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Nitpick comments (1)
src/providers/index.ts (1)

36-66: ⚖️ Poor tradeoff

Deduplicate provider default model literals
Fallback model literals in src/providers/index.ts (e.g. "gpt-4o-mini", "claude-sonnet-4-20250514", "gemini-2.5-flash", "MiniMax-M2.7", and OpenRouter "anthropic/claude-sonnet-4-20250514") repeat the same env[...] || ... defaults already set in src/config.ts, risking drift. Reuse a shared default-model resolution/helper from src/config.ts (or a shared module) instead of redeclaring per-provider defaults.

🤖 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 `@src/providers/index.ts` around lines 36 - 66, The provider-specific fallback
model strings in the switch cases (e.g., the literals "gpt-4o-mini",
"claude-sonnet-4-20250514", "gemini-2.5-flash", "MiniMax-M2.7",
"anthropic/claude-sonnet-4-20250514") duplicate defaults already defined in
src/config.ts and should be replaced by a single shared default resolver; import
the shared default map or function from src/config.ts (e.g., DEFAULT_MODELS or
getDefaultModel) into src/providers/index.ts and use it in each case instead of
hardcoding literals (replace expressions like getEnvVar("OPENAI_MODEL") ||
"gpt-4o-mini" with getEnvVar("OPENAI_MODEL") || getDefaultModel("openai") or
DEFAULT_MODELS.openai), leaving other fields (provider, maxTokens, baseURL)
unchanged.
🤖 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.

Inline comments:
In `@src/providers/index.ts`:
- Around line 28-74: The switch in providerDefaultConfig (function
providerDefaultConfig) is non-exhaustive and can return undefined for
ProviderType "noop"; add a case for "noop" (returning a valid ProviderConfig) or
add a default/exhaustiveness guard (e.g., a never-typed default that throws) to
ensure a ProviderConfig is always returned; update the switch to include "noop"
(or a default that throws a descriptive error) so TS2366 is resolved and
unsupported providers cannot fall through to undefined.

---

Nitpick comments:
In `@src/providers/index.ts`:
- Around line 36-66: The provider-specific fallback model strings in the switch
cases (e.g., the literals "gpt-4o-mini", "claude-sonnet-4-20250514",
"gemini-2.5-flash", "MiniMax-M2.7", "anthropic/claude-sonnet-4-20250514")
duplicate defaults already defined in src/config.ts and should be replaced by a
single shared default resolver; import the shared default map or function from
src/config.ts (e.g., DEFAULT_MODELS or getDefaultModel) into
src/providers/index.ts and use it in each case instead of hardcoding literals
(replace expressions like getEnvVar("OPENAI_MODEL") || "gpt-4o-mini" with
getEnvVar("OPENAI_MODEL") || getDefaultModel("openai") or
DEFAULT_MODELS.openai), leaving other fields (provider, maxTokens, baseURL)
unchanged.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 978b7347-2c20-45b2-bd80-305aaf75f5a6

📥 Commits

Reviewing files that changed from the base of the PR and between 9a4bdbe and 62eadf7.

📒 Files selected for processing (2)
  • src/providers/index.ts
  • test/provider-fallback-model.test.ts

Comment thread src/providers/index.ts
Comment on lines +28 to +74
function providerDefaultConfig(
provider: FallbackConfig["providers"][number],
maxTokens: number,
): ProviderConfig {
switch (provider) {
case "openai":
return {
provider,
model: getEnvVar("OPENAI_MODEL") || "gpt-4o-mini",
maxTokens,
baseURL: getEnvVar("OPENAI_BASE_URL"),
};
case "minimax":
return {
provider,
model: getEnvVar("MINIMAX_MODEL") || "MiniMax-M2.7",
maxTokens,
};
case "anthropic":
return {
provider,
model: getEnvVar("ANTHROPIC_MODEL") || "claude-sonnet-4-20250514",
maxTokens,
baseURL: getEnvVar("ANTHROPIC_BASE_URL"),
};
case "gemini":
return {
provider,
model: getEnvVar("GEMINI_MODEL") || "gemini-2.5-flash",
maxTokens,
};
case "openrouter":
return {
provider,
model:
getEnvVar("OPENROUTER_MODEL") ||
"anthropic/claude-sonnet-4-20250514",
maxTokens,
};
case "agent-sdk":
return {
provider,
model: "agent-sdk",
maxTokens,
};
}
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🔴 Critical | 🏗️ Heavy lift

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
# Resolve the full ProviderType union and tsconfig strictness to confirm exhaustiveness/compile impact.
echo "=== ProviderType definition ==="
ast-grep --pattern 'export type ProviderType = $$$'
rg -nP '\bProviderType\b\s*=' --type=ts -C2

echo "=== kimi-for-coding provider presence ==="
fd -i kimi --type f
rg -nP 'kimi-for-coding' --type=ts -C1

echo "=== tsconfig return/strict flags ==="
fd -g 'tsconfig*.json' --exec cat {}

Repository: rohitg00/agentmemory

Length of output: 1799


🏁 Script executed:

#!/bin/bash
set -euo pipefail

echo "=== Show src/providers/index.ts around providerDefaultConfig ==="
nl -ba src/providers/index.ts | sed -n '1,220p'

echo "=== Find where providerDefaultConfig is called ==="
rg -n "providerDefaultConfig\s*\(" src/providers/index.ts src -S

echo "=== Find usages of 'noop' in code ==="
rg -n "\"noop\"" src -S

echo "=== Find createBaseProvider signature/handling for noop ==="
rg -n "function\s+createBaseProvider|createBaseProvider\s*\(" src/providers/index.ts src -S

Repository: rohitg00/agentmemory

Length of output: 190


🏁 Script executed:

#!/bin/bash
set -euo pipefail

echo "=== Show src/providers/index.ts around providerDefaultConfig ==="
cat -n src/providers/index.ts | sed -n '1,220p'

echo "=== Find where providerDefaultConfig is called ==="
rg -n "providerDefaultConfig\s*\(" src/providers/index.ts src -S || true

echo "=== Find usages of 'noop' in code ==="
rg -n "\"noop\"" src -S || true

echo "=== Find createBaseProvider signature/handling for noop ==="
rg -n "function\s+createBaseProvider|createBaseProvider\s*\(" src/providers/index.ts src -S || true

Repository: rohitg00/agentmemory

Length of output: 7495


🏁 Script executed:

rg -n "noImplicitReturns|strictNullChecks|noImplicitAny" tsconfig*.json **/tsconfig*.json package.json
ls -la

Repository: rohitg00/agentmemory

Length of output: 2581


🏁 Script executed:

#!/bin/bash
set -euo pipefail

echo "=== tsconfig.json (relevant compilerOptions) ==="
cat tsconfig.json

echo "=== Any other tsconfig*.json ==="
fd -i "tsconfig*.json" . -a

echo "=== Grep for noImplicitReturns / noImplicitAny ==="
rg -n "noImplicitReturns|noImplicitAny" tsconfig*.json **/tsconfig*.json . || true

Repository: rohitg00/agentmemory

Length of output: 832


🏁 Script executed:

cat package.json

Repository: rohitg00/agentmemory

Length of output: 2667


🏁 Script executed:

#!/bin/bash
set -euo pipefail

npx tsc -p tsconfig.json --noEmit

Repository: rohitg00/agentmemory

Length of output: 50376


🏁 Script executed:

#!/bin/bash
set -euo pipefail

echo "=== src/config.ts around the fallback/llm provider config (lines ~110-260) ==="
cat -n src/config.ts | sed -n '110,260p'

echo "=== src/config.ts where fallback providers are assembled (search for 'fallback' and 'providers') ==="
rg -n "FallbackConfig|fallbackConfig|providers:\s*\[|providers\.push|providers\s*:" src/config.ts src -S

echo "=== src/config.ts presence of 'noop' provider in any fallback list ==="
rg -n "\"noop\".*provider|provider:\s*\"noop\"|providers.*\"noop\"" src/config.ts src -S

Repository: rohitg00/agentmemory

Length of output: 8225


🏁 Script executed:

#!/bin/bash
set -euo pipefail

echo "=== src/config.ts loadFallbackConfig() (around 380-480) ==="
cat -n src/config.ts | sed -n '380,480p'

echo "=== src/config.ts: any mention of kimi-for-coding in fallback config ==="
rg -n "kimi-for-coding" src/config.ts src -S || true

Repository: rohitg00/agentmemory

Length of output: 2192


🏁 Script executed:

#!/bin/bash
set -euo pipefail

echo "=== src/types.ts: ProviderConfig and Noop config types ==="
rg -n "export (type|interface) ProviderConfig|interface ProviderConfig|ProviderConfig" src/types.ts
cat -n src/types.ts | sed -n '110,210p'

echo "=== src/types.ts: NoopProvider / ProviderType related types ==="
cat -n src/types.ts | sed -n '130,190p'

Repository: rohitg00/agentmemory

Length of output: 5131


🏁 Script executed:

#!/bin/bash
set -euo pipefail

echo "=== Locate kimi-for-coding provider files ==="
fd -i "kimi" src --type f || true

echo "=== Search for kimi-for-coding references ==="
rg -n "kimi-for-coding" src -S || true

echo "=== Search for KIMI_FOR_CODING_MODEL env var references ==="
rg -n "KIMI_FOR_CODING_MODEL" -S src || true

echo "=== Search for 'KimiForCodingProvider' ==="
rg -n "KimiForCodingProvider" src -S || true

Repository: rohitg00/agentmemory

Length of output: 259


🏁 Script executed:

ls -ლა src/providers
find src/providers -maxdepth 2 -type f -iname '*kimi*' -print

Repository: rohitg00/agentmemory

Length of output: 128


🏁 Script executed:

#!/bin/bash
set -euo pipefail

echo "=== src/providers directory ==="
ls -la src/providers

echo "=== kimi-related files under src/providers ==="
find src/providers -maxdepth 3 -type f \( -iname '*kimi*' -o -iname '*kimi-for*' \) -print

echo "=== any file names matching 'kimi' under src ==="
find src -maxdepth 5 -type f -iname '*kimi*' -print

Repository: rohitg00/agentmemory

Length of output: 1122


🏁 Script executed:

cat tsdown.config.ts

Repository: rohitg00/agentmemory

Length of output: 2192


🏁 Script executed:

#!/bin/bash
set -euo pipefail

npx tsc -p tsconfig.json --noEmit --pretty false 2>&1 \
  | rg -n "TS2366|providerDefaultConfig|No default config" || true

Repository: rohitg00/agentmemory

Length of output: 197


Fix providerDefaultConfig() non-exhaustive switch causing TS2366

src/providers/index.ts’s providerDefaultConfig() returns ProviderConfig but the switch doesn’t handle ProviderType = ... | "noop", producing TS2366: Function lacks ending return statement. Add a "noop" case (and/or a default exhaustiveness guard like never) so unsupported providers can’t silently return undefined and be skipped by the surrounding try/catch.

🤖 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 `@src/providers/index.ts` around lines 28 - 74, The switch in
providerDefaultConfig (function providerDefaultConfig) is non-exhaustive and can
return undefined for ProviderType "noop"; add a case for "noop" (returning a
valid ProviderConfig) or add a default/exhaustiveness guard (e.g., a never-typed
default that throws) to ensure a ProviderConfig is always returned; update the
switch to include "noop" (or a default that throws a descriptive error) so
TS2366 is resolved and unsupported providers cannot fall through to undefined.

@rohitg00

rohitg00 commented Jun 2, 2026

Copy link
Copy Markdown
Owner

Thanks @mturac — fallback provider default-model resolution shipped in v0.9.25 (PR #791 + release notes). Same approach: each fallback resolves its own env-driven default (OPENAI_MODEL / GEMINI_MODEL / ANTHROPIC_MODEL / MINIMAX_MODEL / OPENROUTER_MODEL). Closing because the fix is on main.

@rohitg00 rohitg00 closed this Jun 2, 2026
@mturac
mturac deleted the fix/issue-778-fallback-provider-models branch June 5, 2026 13:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants