Skip to content

Commit 026644f

Browse files
陈浩杰陈浩杰
authored andcommitted
fix: handle invalid maxOutputTokens value (0 or undefined)
Fixes #21858 Fixes #21522 - Returns OUTPUT_TOKEN_MAX when model.limit.output is 0, undefined, or negative - Adds unit tests for maxOutputTokens function
1 parent 2719063 commit 026644f

File tree

2 files changed

+53
-1
lines changed

2 files changed

+53
-1
lines changed

packages/opencode/src/provider/transform.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -945,7 +945,11 @@ export namespace ProviderTransform {
945945
}
946946

947947
export function maxOutputTokens(model: Provider.Model): number {
948-
return Math.min(model.limit.output, OUTPUT_TOKEN_MAX) || OUTPUT_TOKEN_MAX
948+
const output = model.limit.output
949+
if (!output || output <= 0) {
950+
return OUTPUT_TOKEN_MAX
951+
}
952+
return Math.min(output, OUTPUT_TOKEN_MAX)
949953
}
950954

951955
export function schema(model: Provider.Model, schema: JSONSchema.BaseSchema | JSONSchema7): JSONSchema7 {

packages/opencode/test/provider/transform.test.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2837,3 +2837,51 @@ describe("ProviderTransform.variants", () => {
28372837
})
28382838
})
28392839
})
2840+
2841+
describe("ProviderTransform.maxOutputTokens", () => {
2842+
const OUTPUT_TOKEN_MAX = 32000
2843+
2844+
const createMockModel = (overrides: any = {}) => ({
2845+
id: "test/model",
2846+
providerID: "test",
2847+
api: { id: "test", url: "https://test.com", npm: "@ai-sdk/test" },
2848+
name: "Test Model",
2849+
capabilities: { reasoning: false, toolcall: true, attachment: true },
2850+
limit: { context: 128000, output: 8192 },
2851+
status: "active",
2852+
options: {},
2853+
headers: {},
2854+
...overrides,
2855+
})
2856+
2857+
test("should return model output limit when valid", () => {
2858+
const model = createMockModel({ limit: { context: 200000, output: 64000 } })
2859+
const result = ProviderTransform.maxOutputTokens(model)
2860+
// Should be min of model output limit (64000) and OUTPUT_TOKEN_MAX (32000)
2861+
expect(result).toBe(32000)
2862+
})
2863+
2864+
test("should return OUTPUT_TOKEN_MAX when model limit.output is undefined", () => {
2865+
const model = createMockModel({ limit: { context: 128000 } })
2866+
const result = ProviderTransform.maxOutputTokens(model)
2867+
expect(result).toBe(OUTPUT_TOKEN_MAX)
2868+
})
2869+
2870+
test("should return OUTPUT_TOKEN_MAX when model limit.output is 0", () => {
2871+
const model = createMockModel({ limit: { context: 128000, output: 0 } })
2872+
const result = ProviderTransform.maxOutputTokens(model)
2873+
expect(result).toBe(OUTPUT_TOKEN_MAX)
2874+
})
2875+
2876+
test("should return OUTPUT_TOKEN_MAX when model limit.output is negative", () => {
2877+
const model = createMockModel({ limit: { context: 128000, output: -1 } })
2878+
const result = ProviderTransform.maxOutputTokens(model)
2879+
expect(result).toBe(OUTPUT_TOKEN_MAX)
2880+
})
2881+
2882+
test("should return min of model limit and OUTPUT_TOKEN_MAX", () => {
2883+
const model = createMockModel({ limit: { context: 128000, output: 16000 } })
2884+
const result = ProviderTransform.maxOutputTokens(model)
2885+
expect(result).toBe(16000)
2886+
})
2887+
})

0 commit comments

Comments
 (0)