Skip to content

Commit 46e9068

Browse files
committed
fix(api): honor custom model IDs for OpenAI-compatible providers
Previously, getModel() silently fell back to the provider default when the configured apiModelId wasn't found in the static providerModels list. This sent the wrong model to the API for user-supplied custom models (e.g. newly released Fireworks models), surfacing as confusing "model not found" errors. Now getModel() honors the exact user-configured ID and supplies openAiModelInfoSaneDefaults so the rest of the pipeline works, only falling back to the provider default when no model is configured. - Return known model metadata when the ID is in providerModels - Return the custom ID with sane defaults when unknown but configured - Fall back to the default only when no model ID is set
1 parent c6e6ee3 commit 46e9068

4 files changed

Lines changed: 138 additions & 20 deletions

File tree

‎src/api/providers/__tests__/base-openai-compatible-provider.spec.ts‎

Lines changed: 71 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import { Anthropic } from "@anthropic-ai/sdk"
44
import OpenAI from "openai"
55

6-
import type { ModelInfo } from "@roo-code/types"
6+
import { type ModelInfo, openAiModelInfoSaneDefaults } from "@roo-code/types"
77

88
import { BaseOpenAiCompatibleProvider } from "../base-openai-compatible-provider"
99
import { asyncStreamFrom, collectStream } from "../../../test-utils/stream"
@@ -25,18 +25,20 @@ vi.mock("openai", () => ({
2525
}),
2626
}))
2727

28+
const TEST_MODEL_INFO: ModelInfo = {
29+
maxTokens: 4096,
30+
contextWindow: 128000,
31+
supportsImages: false,
32+
supportsPromptCache: false,
33+
inputPrice: 0.5,
34+
outputPrice: 1.5,
35+
}
36+
2837
// Create a concrete test implementation of the abstract base class
2938
class TestOpenAiCompatibleProvider extends BaseOpenAiCompatibleProvider<"test-model"> {
30-
constructor(apiKey: string) {
39+
constructor(apiKey: string, options?: { apiModelId?: string; modelMaxTokens?: number }) {
3140
const testModels: Record<"test-model", ModelInfo> = {
32-
"test-model": {
33-
maxTokens: 4096,
34-
contextWindow: 128000,
35-
supportsImages: false,
36-
supportsPromptCache: false,
37-
inputPrice: 0.5,
38-
outputPrice: 1.5,
39-
},
41+
"test-model": TEST_MODEL_INFO,
4042
}
4143

4244
super({
@@ -45,6 +47,7 @@ class TestOpenAiCompatibleProvider extends BaseOpenAiCompatibleProvider<"test-mo
4547
defaultProviderModelId: "test-model",
4648
providerModels: testModels,
4749
apiKey,
50+
...options,
4851
})
4952
}
5053
}
@@ -297,6 +300,64 @@ describe("BaseOpenAiCompatibleProvider", () => {
297300
})
298301
})
299302

303+
describe("getModel", () => {
304+
it("returns the provider default when no model id is configured", () => {
305+
const model = handler.getModel()
306+
expect(model.id).toBe("test-model")
307+
expect(model.info).toEqual(TEST_MODEL_INFO)
308+
})
309+
310+
it("returns the predefined metadata for a known model id", () => {
311+
const knownHandler = new TestOpenAiCompatibleProvider("test-api-key", { apiModelId: "test-model" })
312+
const model = knownHandler.getModel()
313+
expect(model.id).toBe("test-model")
314+
expect(model.info).toEqual(TEST_MODEL_INFO)
315+
})
316+
317+
it("honors a custom model id that is not in the predefined list", () => {
318+
// Regression: a user-supplied custom id must not be silently swapped for the
319+
// provider default (which previously caused wrong-model requests / 404s).
320+
const customHandler = new TestOpenAiCompatibleProvider("test-api-key", {
321+
apiModelId: "some/custom-model-not-in-list",
322+
})
323+
const model = customHandler.getModel()
324+
expect(model.id).toBe("some/custom-model-not-in-list")
325+
expect(model.id).not.toBe("test-model")
326+
// Falls back to sane default metadata so the rest of the pipeline works.
327+
expect(model.info).toEqual(openAiModelInfoSaneDefaults)
328+
})
329+
330+
it("sends the custom model id verbatim to the API", async () => {
331+
mockCreate.mockImplementationOnce(() => asyncStreamFrom([]))
332+
333+
const customHandler = new TestOpenAiCompatibleProvider("test-api-key", {
334+
apiModelId: "some/custom-model-not-in-list",
335+
})
336+
337+
const stream = customHandler.createMessage("system prompt", [])
338+
await collectStream(stream)
339+
340+
expect(mockCreate).toHaveBeenCalledWith(
341+
expect.objectContaining({ model: "some/custom-model-not-in-list" }),
342+
undefined,
343+
)
344+
})
345+
346+
it("completePrompt sends the custom model id verbatim to the API", async () => {
347+
mockCreate.mockResolvedValueOnce({ choices: [{ message: { content: "ok" } }] })
348+
349+
const customHandler = new TestOpenAiCompatibleProvider("test-api-key", {
350+
apiModelId: "some/custom-model-not-in-list",
351+
})
352+
353+
await customHandler.completePrompt("hello")
354+
355+
expect(mockCreate).toHaveBeenCalledWith(
356+
expect.objectContaining({ model: "some/custom-model-not-in-list" }),
357+
)
358+
})
359+
})
360+
300361
describe("Tool call handling", () => {
301362
it("should yield tool_call_end events when finish_reason is tool_calls", async () => {
302363
mockCreate.mockImplementationOnce(() =>

‎src/api/providers/base-openai-compatible-provider.ts‎

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Anthropic } from "@anthropic-ai/sdk"
22
import OpenAI from "openai"
33

4-
import type { ModelInfo } from "@roo-code/types"
4+
import { type ModelInfo, openAiModelInfoSaneDefaults } from "@roo-code/types"
55

66
import { type ApiHandlerOptions, getModelMaxOutputTokens } from "../../shared/api"
77
import { TagMatcher } from "../../utils/tag-matcher"
@@ -243,11 +243,27 @@ export abstract class BaseOpenAiCompatibleProvider<ModelName extends string>
243243
}
244244

245245
override getModel() {
246-
const id =
247-
this.options.apiModelId && this.options.apiModelId in this.providerModels
248-
? (this.options.apiModelId as ModelName)
249-
: this.defaultProviderModelId
246+
const requestedId = this.options.apiModelId
250247

251-
return { id, info: this.providerModels[id] }
248+
// A known model: use its predefined metadata.
249+
if (requestedId && requestedId in this.providerModels) {
250+
const id = requestedId as ModelName
251+
return { id, info: this.providerModels[id] }
252+
}
253+
254+
// A user-supplied custom model that isn't in our static list (e.g. a newly
255+
// released Fireworks model). Honor the exact id the user configured instead
256+
// of silently falling back to the provider default, which would send the
257+
// wrong model to the API and can surface as a confusing "model not found"
258+
// error. Provide sane default metadata so the rest of the pipeline works.
259+
if (requestedId) {
260+
return {
261+
id: requestedId as ModelName,
262+
info: { ...openAiModelInfoSaneDefaults },
263+
}
264+
}
265+
266+
// No model configured: use the provider default.
267+
return { id: this.defaultProviderModelId, info: this.providerModels[this.defaultProviderModelId] }
252268
}
253269
}

‎webview-ui/src/components/ui/hooks/__tests__/useSelectedModel.spec.ts‎

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1413,7 +1413,9 @@ describe("useSelectedModel", () => {
14131413
expect(result.current.info).toEqual(getZAiModels("international_api")["glm-5.3"])
14141414
})
14151415

1416-
it("falls back when GLM-5.3 is unavailable on the China API", () => {
1416+
it("displays the configured model ID with sane defaults when it is not in the API line catalog", () => {
1417+
expect(getZAiModels("china_api")["glm-5.3"]).toBeUndefined()
1418+
14171419
const apiConfiguration: ProviderSettings = {
14181420
apiProvider: providerIdentifiers.zai,
14191421
apiModelId: "glm-5.3",
@@ -1422,6 +1424,43 @@ describe("useSelectedModel", () => {
14221424

14231425
const { result } = renderHook(() => useSelectedModel(apiConfiguration), { wrapper: createWrapper() })
14241426

1427+
expect(result.current.id).toBe("glm-5.3")
1428+
expect(result.current.info).toEqual(openAiModelInfoSaneDefaults)
1429+
})
1430+
1431+
it("displays a custom model ID that is in no Z.ai catalog", () => {
1432+
const apiConfiguration: ProviderSettings = {
1433+
apiProvider: providerIdentifiers.zai,
1434+
apiModelId: "glm-custom-preview",
1435+
}
1436+
1437+
const { result } = renderHook(() => useSelectedModel(apiConfiguration), { wrapper: createWrapper() })
1438+
1439+
expect(result.current.id).toBe("glm-custom-preview")
1440+
expect(result.current.info).toEqual(openAiModelInfoSaneDefaults)
1441+
})
1442+
1443+
it("falls back to the API line default when no model is configured", () => {
1444+
const apiConfiguration: ProviderSettings = {
1445+
apiProvider: providerIdentifiers.zai,
1446+
zaiApiLine: "china_api",
1447+
}
1448+
1449+
const { result } = renderHook(() => useSelectedModel(apiConfiguration), { wrapper: createWrapper() })
1450+
1451+
expect(result.current.id).toBe(mainlandZAiDefaultModelId)
1452+
expect(result.current.info).toEqual(getZAiModels("china_api")[mainlandZAiDefaultModelId])
1453+
})
1454+
1455+
it("falls back to the API line default when the configured model ID is empty", () => {
1456+
const apiConfiguration: ProviderSettings = {
1457+
apiProvider: providerIdentifiers.zai,
1458+
apiModelId: "",
1459+
zaiApiLine: "china_api",
1460+
}
1461+
1462+
const { result } = renderHook(() => useSelectedModel(apiConfiguration), { wrapper: createWrapper() })
1463+
14251464
expect(result.current.id).toBe(mainlandZAiDefaultModelId)
14261465
expect(result.current.info).toEqual(getZAiModels("china_api")[mainlandZAiDefaultModelId])
14271466
})

‎webview-ui/src/components/ui/hooks/useSelectedModel.ts‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -326,9 +326,11 @@ function getSelectedModel({
326326
const isChina = zaiApiLineConfigs[apiLine].isChina
327327
const models = getZAiModels(apiLine)
328328
const defaultModelId = getProviderDefaultModelId(provider, { isChina })
329-
const id = getValidatedModelId(apiConfiguration.apiModelId, models, defaultModelId)
330-
const info = models[id]
331-
return { id, info }
329+
const configuredId = apiConfiguration.apiModelId
330+
if (configuredId) {
331+
return { id: configuredId, info: models[configuredId] ?? openAiModelInfoSaneDefaults }
332+
}
333+
return { id: defaultModelId, info: models[defaultModelId] }
332334
}
333335
case providerIdentifiers.openaiNative: {
334336
const id = apiConfiguration.apiModelId ?? defaultModelId

0 commit comments

Comments
 (0)