Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/custom-registry-max-output-size.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@moonshot-ai/kimi-code": patch
---

Fix 400 "Invalid max_tokens" errors for models imported from a custom registry (api.json) by recording each model's advertised output token limit.
10 changes: 10 additions & 0 deletions packages/oauth/src/custom-registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,14 @@ function resolveMaxContextSize(model: CustomRegistryModelEntry): number {
return CUSTOM_REGISTRY_DEFAULT_MAX_CONTEXT;
}

function resolveMaxOutputSize(model: CustomRegistryModelEntry): number | undefined {
const output = model.limit?.output;
if (typeof output === 'number' && Number.isInteger(output) && output > 0) {
return output;
}
return undefined;
}

function resolveCapabilities(model: CustomRegistryModelEntry): string[] {
if (hasRichCapabilityHints(model)) {
return capabilitiesFromCustomEntry(model);
Expand Down Expand Up @@ -340,6 +348,7 @@ export function applyCustomRegistryProvider(
for (const [modelKey, model] of Object.entries(entry.models)) {
const aliasKey = `${providerKey}/${modelKey}`;
const maxContextSize = resolveMaxContextSize(model);
const maxOutputSize = resolveMaxOutputSize(model);
const capabilities = resolveCapabilities(model);
const displayName =
typeof model.name === 'string' && model.name.length > 0 ? model.name : model.id;
Expand All @@ -351,6 +360,7 @@ export function applyCustomRegistryProvider(
maxContextSize,
capabilities,
displayName,
maxOutputSize,
...(model.support_efforts !== undefined ? { supportEfforts: model.support_efforts } : {}),
...(model.default_effort !== undefined ? { defaultEffort: model.default_effort } : {}),
};
Expand Down
1 change: 1 addition & 0 deletions packages/oauth/src/model-alias-merge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export const CUSTOM_REGISTRY_MODEL_FIELDS: ReadonlySet<string> = new Set([
'provider',
'model',
'maxContextSize',
'maxOutputSize',
'capabilities',
'displayName',
'supportEfforts',
Expand Down
60 changes: 58 additions & 2 deletions packages/oauth/test/custom-registry.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ describe('fetchCustomRegistry', () => {
const error = await fetchCustomRegistry(
KOKUB_SOURCE,
{ fetchImpl: fetchMock as unknown as typeof fetch },
).catch((caught: unknown) => caught);
).catch((error: unknown) => error);

expect(error).toBeInstanceOf(CustomRegistryApiError);
expect((error as CustomRegistryApiError).status).toBe(401);
Expand Down Expand Up @@ -330,6 +330,31 @@ describe('applyCustomRegistryProvider', () => {
expect(alias.capabilities).not.toContain('image_out');
});

it('maps limit.output onto the model alias maxOutputSize', () => {
const config: ManagedKimiConfigShape = { providers: {} };

applyCustomRegistryProvider(
config,
{
id: 'registry_chat-completions',
name: 'Sample Registry (chat completions)',
api: 'https://registry.example.test/v1',
type: 'openai_responses',
models: {
'deepseek-flash': {
id: 'deepseek-flash',
name: 'DeepSeek Flash',
limit: { context: 1000000, output: 393216 },
},
},
},
KOKUB_SOURCE,
);

const alias = config.models?.['registry_chat-completions/deepseek-flash'];
expect(alias?.['maxOutputSize']).toBe(393216);
});

it('clears stale aliases for the same provider before re-populating', () => {
const config: ManagedKimiConfigShape = {
providers: {
Expand Down Expand Up @@ -462,6 +487,37 @@ describe('applyCustomRegistryProvider', () => {
expect(alias['supportEfforts']).toEqual(['low', 'high', 'max']);
});

it('drops a stale maxOutputSize when a refresh no longer declares limit.output', () => {
const config: ManagedKimiConfigShape = {
providers: {},
models: {
'registry_chat-completions/gpt-5.5': {
provider: 'registry_chat-completions',
model: 'gpt-5.5',
maxContextSize: 131072,
maxOutputSize: 8192,
} as Record<string, unknown>,
},
};

applyCustomRegistryProvider(
config,
{
id: 'registry_chat-completions',
name: 'Sample Registry (chat completions)',
api: 'https://registry.example.test/v1',
type: 'openai',
models: {
'gpt-5.5': { id: 'gpt-5.5', name: 'GPT 5.5' },
},
},
KOKUB_SOURCE,
);

const alias = config.models?.['registry_chat-completions/gpt-5.5'];
expect(alias?.['maxOutputSize']).toBeUndefined();
});

it('drops stale effort fields when a refresh no longer declares them', () => {
const config: ManagedKimiConfigShape = {
providers: {},
Expand Down Expand Up @@ -580,7 +636,7 @@ describe('applyCustomRegistryEntries', () => {
applyCustomRegistryEntries(config, entries, source);
applyCustomRegistryEntries(config, entries, source);

expect(Object.keys(config.providers).sort()).toEqual(['a', 'b', 'c']);
expect(Object.keys(config.providers).toSorted()).toEqual(['a', 'b', 'c']);
expect(config.models?.['a/m1']).toBeDefined();
expect(config.models?.['b/m1']).toBeDefined();
expect(config.models?.['c/m1']).toBeDefined();
Expand Down