{"record":{"id":"94d7898209f646f6","repo":"windmill-labs/windmill","slug":"ai-response-contained-empty-code-block","errorCode":null,"errorMessage":"AI response contained empty code block","messagePattern":"AI response contained empty code block","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"frontend/src/lib/components/copilot/chat/AIChatManager.svelte.ts","lineNumber":2837,"sourceCode":"\t\t\t\t\tsetToolStatus: () => {},\n\t\t\t\t\tremoveToolStatus: () => {}\n\t\t\t\t},\n\t\t\t\tsystemMessage\n\t\t\t}\n\n\t\t\tawait this.chatRequest({ ...params })\n\n\t\t\t// Validate we received a response\n\t\t\tif (!reply.trim()) {\n\t\t\t\tthrow new Error('AI response was empty')\n\t\t\t}\n\n\t\t\t// Try to extract new code from response\n\t\t\tconst newCodeMatch = reply.match(/<new_code>([\\s\\S]*?)<\\/new_code>/i)\n\t\t\tif (newCodeMatch && newCodeMatch[1]) {\n\t\t\t\tconst code = newCodeMatch[1].trim()\n\t\t\t\tif (!code) {\n\t\t\t\t\tthrow new Error('AI response contained empty code block')\n\t\t\t\t}\n\t\t\t\treturn code\n\t\t\t}\n\n\t\t\t// Fallback: try to take everything after the last <new_code> tag\n\t\t\tconst lastNewCodeMatch = reply.match(/<new_code>([\\s\\S]*)/i)\n\t\t\tif (lastNewCodeMatch && lastNewCodeMatch[1]) {\n\t\t\t\tconst code = lastNewCodeMatch[1].trim().replace(/```/g, '')\n\t\t\t\tif (!code) {\n\t\t\t\t\tthrow new Error('AI response contained empty code block')\n\t\t\t\t}\n\t\t\t\treturn code\n\t\t\t}\n\n\t\t\t// If no code tags found, throw error with helpful message\n\t\t\tthrow new Error('AI response did not contain valid code. Please try rephrasing your request.')\n\t\t} catch (error) {\n\t\t\t// if abort controller is aborted, don't throw an error","sourceCodeStart":2819,"sourceCodeEnd":2855,"githubUrl":"https://github.com/windmill-labs/windmill/blob/e474e8803ce2ff5c2df09a58dab51d45f5c922ca/frontend/src/lib/components/copilot/chat/AIChatManager.svelte.ts#L2819-L2855","documentation":"In sendInlineRequest, the primary extraction path matches `<new_code>...</new_code>` in the AI reply. If the tag pair exists but the captured content is only whitespace after trimming, no usable code was returned, so this error is thrown.","triggerScenarios":"The model emits `<new_code>` open and close tags with nothing (or only whitespace) between them — e.g. the model echoed the tag format without filling in code, or the completion was cut off right after the opening tag.","commonSituations":"Small/weak model that imitates the prompt's tag format without generating code; response truncated by max_tokens immediately after `<new_code>`; user asked a non-code question in inline mode so the model emitted empty tags.","solutions":["Retry with a more explicit instruction describing exactly the code to produce.","Select a smaller, well-defined code region so the model has a concrete edit target.","Use a stronger model in the workspace AI settings.","Rephrase the request so it clearly asks for replacement code."],"exampleFix":"// before\nconst code = await manager.sendInlineRequest('fix this', selected, selection)\n// after\nconst code = await manager.sendInlineRequest(\n  'Replace the selected code with a corrected version inside <new_code> tags',\n  selected,\n  selection\n)","handlingStrategy":"retry","validationCode":null,"typeGuard":null,"tryCatchPattern":"try {\n  code = await manager.sendInlineRequest(instr, sel, selection)\n} catch (e) {\n  if (String(e.message).includes('empty code block')) {\n    code = await manager.sendInlineRequest(moreExplicitInstr, sel, selection)\n  } else throw e\n}","preventionTips":["Phrase inline instructions as explicit 'replace with code' requests.","Select a concrete, small code region for inline edits.","Prefer stronger models for inline edit mode."],"tags":["ai","code-extraction","parsing","copilot"],"backgroundTag":"llm-missing-code-tag","analyzedSha":"e474e8803ce2ff5c2df09a58dab51d45f5c922ca","analyzedAt":"2026-09-03T12:38:19.024Z","contentChangedAt":"2026-09-03T12:38:19.024Z","schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}