-
Notifications
You must be signed in to change notification settings - Fork 577
Description
[Bug] Suggestions JSON parsing fails due to incomplete response text accumulation
Labels: bug, server, suggestions
Description
When generating suggestions, the server throws a JSON parsing error:
[Server Error] [Suggestions] Failed to parse suggestions JSON from AI response:
Error: No valid JSON found in response
at generateSuggestions (apps/server/src/routes/suggestions/generate-suggestions.ts:103:13)
Root Cause
In generate-suggestions.ts, the streamed response text is being overwritten instead of accumulated:
Line 70:
responseText = block.text; // BUG: overwrites previous chunksWhen the AI response is streamed in multiple chunks, only the last chunk is retained. If the JSON object spans multiple chunks, the final responseText contains incomplete JSON, causing the regex match to fail.
Affected Code
File: apps/server/src/routes/suggestions/generate-suggestions.ts
// Lines 66-86 - Current implementation
for await (const msg of stream) {
if (msg.type === "assistant" && msg.message.content) {
for (const block of msg.message.content) {
if (block.type === "text") {
responseText = block.text; // ❌ OVERWRITES - loses previous chunks
// ...
}
}
}
}Suggested Fix
Change line 70 from assignment to concatenation:
// Option 1: Simple concatenation
responseText += block.text;
// Option 2: If blocks are complete messages, check behavior first
// The streaming behavior needs to be verified - does each msg.type === "assistant"
// contain incremental text or the full text so far?Note: Before fixing, verify the streaming behavior of @anthropic-ai/claude-agent-sdk - some SDKs send cumulative text (full text so far) while others send deltas (incremental chunks).
Additional Improvements
-
Better JSON extraction regex - The current regex
/\{[\s\S]*"suggestions"[\s\S]*\}/is greedy and may match too much. Consider using a proper JSON parser or more specific extraction. -
Log the response text for debugging when parsing fails:
} catch (error) { logger.error("Failed to parse suggestions JSON. Response was:", responseText); logger.error("Parse error:", error); }
-
Handle edge cases - Empty responses, timeout responses, or responses without JSON format.
Environment
- Component: Server (
apps/server) - File:
apps/server/src/routes/suggestions/generate-suggestions.ts:70 - Dependency:
@anthropic-ai/claude-agent-sdk