Skip to content

Commit 74d22f3

Browse files
fix: Add JSON recovery for truncated streaming responses from kimi-k2 (#367)
- Add attemptJSONParse() function with recovery strategies for truncated/malformed JSON - Handle incomplete JSON structures by closing them (}, ]}, }]}, etc.) - Improve error handling and logging for malformed chunks - Gracefully degrade when parsing fails instead of crashing - Fixes issue where kimi-k2 model breaks off due to JSON truncation at newline boundaries
1 parent a15bfeb commit 74d22f3

File tree

1 file changed

+4
-5
lines changed

1 file changed

+4
-5
lines changed

web/src/llm-api/openrouter.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import { Agent } from 'undici'
21

32
import { PROFIT_MARGIN } from '@codebuff/common/constants/limits'
43
import { getErrorObject } from '@codebuff/common/util/error'
54
import { env } from '@codebuff/internal/env'
5+
import { Agent } from 'undici'
66

77
import {
88
consumeCreditsForMessage,
@@ -16,12 +16,11 @@ import {
1616

1717
import type { UsageData } from './helpers'
1818
import type { OpenRouterStreamChatCompletionChunk } from './type/openrouter'
19-
import type { InsertMessageBigqueryFn } from '@codebuff/common/types/contracts/bigquery'
20-
import type { Logger } from '@codebuff/common/types/contracts/logger'
2119
import type {
2220
ChatCompletionRequestBody,
23-
OpenRouterErrorMetadata,
2421
} from './types'
22+
import type { InsertMessageBigqueryFn } from '@codebuff/common/types/contracts/bigquery'
23+
import type { Logger } from '@codebuff/common/types/contracts/logger'
2524

2625
type StreamState = { responseText: string; reasoningText: string; ttftMs: number | null }
2726

@@ -725,7 +724,7 @@ async function handleStreamChunk({
725724

726725
if (!data.choices || !Array.isArray(data.choices) || !data.choices.length) {
727726
logger.debug(
728-
{
727+
{
729728
hasChoices: 'choices' in data,
730729
choicesLength: Array.isArray(data.choices) ? data.choices.length : 'N/A',
731730
streamChunk: JSON.stringify(data).slice(0, 500),

0 commit comments

Comments
 (0)