{"record":{"id":"c7b09e4fd05af3d4","repo":"windmill-labs/windmill","slug":"ai-response-did-not-contain-valid-code-please-try","errorCode":null,"errorMessage":"AI response did not contain valid code. Please try rephrasing your request.","messagePattern":"AI response did not contain valid code\\. Please try rephrasing your request\\.","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"frontend/src/lib/components/copilot/chat/AIChatManager.svelte.ts","lineNumber":2853,"sourceCode":"\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\n\t\t\tif (this.inlineAbortController?.signal.aborted) {\n\t\t\t\treturn\n\t\t\t}\n\t\t\tconsole.error('Unexpected error in sendInlineRequest:', error)\n\t\t\tthrow new Error('An unexpected error occurred. Please try again.')\n\t\t}\n\t}\n\n\t// Optional pre-flight hook called once per send, after the user's message\n\t// bubble + loading indicator are shown optimistically but before the request\n\t// goes out. Sessions use this to commit/materialise the workspace (creating a\n\t// staged fork via the API) so the first message targets the correct workspace.\n\tbeforeSend?: () => Promise<void> | void\n\tafterFirstTurnSaved?: () => Promise<void> | void\n\n\t/** A send is between the composer clearing and its turn being installed.","sourceCodeStart":2835,"sourceCodeEnd":2871,"githubUrl":"https://github.com/windmill-labs/windmill/blob/e474e8803ce2ff5c2df09a58dab51d45f5c922ca/frontend/src/lib/components/copilot/chat/AIChatManager.svelte.ts#L2835-L2871","documentation":"Terminal extraction failure in sendInlineRequest: the AI replied with text, but no `<new_code>` tag (complete or partial) was found anywhere in the reply. Since the manager can only apply edits from the tagged code block, it throws with an actionable message telling the user to rephrase.","triggerScenarios":"The model answers conversationally — explanations, questions, or apologies — without emitting the `<new_code>...</new_code>` block the inline-edit system prompt demands.","commonSituations":"Ambiguous instructions ('what do you think?') that invite prose instead of code; a model that ignores the tag convention; instructions answered with a code fence but no `<new_code>` wrapper (fences alone are not parsed).","solutions":["Rephrase the instruction to explicitly request replacement code, e.g. 'replace the selection with ...'.","Retry — model behavior is non-deterministic and a rephrased prompt usually yields the tag.","Switch to a model that follows the tag format reliably.","Use the regular chat mode instead of inline edit for questions that need no code edit."],"exampleFix":"// before\nawait manager.sendInlineRequest('what about performance?', sel, selection)\n// after\nawait manager.sendInlineRequest('Optimize this code for performance and return the full replacement in <new_code> tags', sel, selection)","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('did not contain valid code')) {\n    code = await manager.sendInlineRequest(instr + ' — return the replacement code in <new_code> tags', sel, selection)\n  } else throw e\n}","preventionTips":["Only use inline edit for code-production requests, not questions.","Include explicit 'return code' phrasing in instructions.","Use the chat panel for explanatory questions."],"tags":["ai","code-extraction","parsing","prompting","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"}