windmill-labs/windmill · error

AI response did not contain valid code. Please try rephrasin

Error message

AI response did not contain valid code. Please try rephrasing your request.

What it means

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.

Source

Thrown at frontend/src/lib/components/copilot/chat/AIChatManager.svelte.ts:2853

				const code = newCodeMatch[1].trim()
				if (!code) {
					throw new Error('AI response contained empty code block')
				}
				return code
			}

			// Fallback: try to take everything after the last <new_code> tag
			const lastNewCodeMatch = reply.match(/<new_code>([\s\S]*)/i)
			if (lastNewCodeMatch && lastNewCodeMatch[1]) {
				const code = lastNewCodeMatch[1].trim().replace(/```/g, '')
				if (!code) {
					throw new Error('AI response contained empty code block')
				}
				return code
			}

			// If no code tags found, throw error with helpful message
			throw new Error('AI response did not contain valid code. Please try rephrasing your request.')
		} catch (error) {
			// if abort controller is aborted, don't throw an error
			if (this.inlineAbortController?.signal.aborted) {
				return
			}
			console.error('Unexpected error in sendInlineRequest:', error)
			throw new Error('An unexpected error occurred. Please try again.')
		}
	}

	// Optional pre-flight hook called once per send, after the user's message
	// bubble + loading indicator are shown optimistically but before the request
	// goes out. Sessions use this to commit/materialise the workspace (creating a
	// staged fork via the API) so the first message targets the correct workspace.
	beforeSend?: () => Promise<void> | void
	afterFirstTurnSaved?: () => Promise<void> | void

	/** A send is between the composer clearing and its turn being installed.

View on GitHub (pinned to e474e8803c)

Solutions

  1. Rephrase the instruction to explicitly request replacement code, e.g. 'replace the selection with ...'.
  2. Retry — model behavior is non-deterministic and a rephrased prompt usually yields the tag.
  3. Switch to a model that follows the tag format reliably.
  4. Use the regular chat mode instead of inline edit for questions that need no code edit.

Example fix

// before
await manager.sendInlineRequest('what about performance?', sel, selection)
// after
await manager.sendInlineRequest('Optimize this code for performance and return the full replacement in <new_code> tags', sel, selection)
Defensive patterns

Strategy: retry

Try / catch

try {
  code = await manager.sendInlineRequest(instr, sel, selection)
} catch (e) {
  if (String(e.message).includes('did not contain valid code')) {
    code = await manager.sendInlineRequest(instr + ' — return the replacement code in <new_code> tags', sel, selection)
  } else throw e
}

Prevention

When it happens

Trigger: The model answers conversationally — explanations, questions, or apologies — without emitting the `<new_code>...</new_code>` block the inline-edit system prompt demands.

Common situations: 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).

Related errors


AI-assisted analysis of windmill-labs/windmill@e474e8803c (2026-09-03). Data as JSON: /api/errors/c7b09e4fd05af3d4. Report an issue: GitHub.