jackwener/OpenCLI · error · ArgumentError

prompt cannot be empty

Error message

prompt cannot be empty

What it means

normalizePrompt collapses whitespace and trims the prompt for generate-like commands, then throws ArgumentError "prompt cannot be empty" if nothing remains. It prevents sending empty generation requests to the Midjourney API.

Source

Thrown at clis/midjourney/utils.js:94

export function parseImageIndices(value, batchSize = 4) {
  const max = Number.isInteger(batchSize) && batchSize > 0 ? batchSize : 4;
  const raw = String(value ?? 'all').trim().toLowerCase();
  if (!raw || raw === 'all') return Array.from({ length: max }, (_, index) => index);
  if (!/^\d+$/.test(raw)) {
    throw new ArgumentError(`--index must be "all" or an integer from 1 to ${max}`);
  }
  const userIndex = Number(raw);
  if (userIndex < 1 || userIndex > max) {
    throw new ArgumentError(`--index must be between 1 and ${max} for this job`);
  }
  return [userIndex - 1];
}

export function normalizePrompt(value) {
  const prompt = String(value ?? '').replace(/\s+/g, ' ').trim();
  if (!prompt) {
    throw new ArgumentError(
      'prompt cannot be empty',
      'Example: opencli midjourney generate "a blue ceramic teapot --ar 1:1"',
    );
  }
  return prompt;
}

export function promptFromFullCommand(value) {
  return String(value ?? '')
    .replace(/^\s*\/?imagine\s*(?:prompt\s*:)?\s*/i, '')
    .replace(/\s+/g, ' ')
    .trim();
}

export function promptCore(value) {
  return promptFromFullCommand(value)
    .split(/\s+--[a-z][a-z0-9-]*/i, 1)[0]
    .replace(/^(?:https?:\/\/\S+\s+)+/i, '')

View on GitHub (pinned to 49907e53dc)

Solutions

  1. Pass a non-empty quoted prompt text to the command
  2. If the prompt comes from a variable/config, print it first to confirm it is non-empty
  3. Quote the prompt in shell to avoid word-splitting dropping it entirely
  4. Combine flag-based prompts correctly (don't pass the text under the wrong flag)

Example fix

// before
PROMPT=""
opencli midjourney generate "$PROMPT"
// after
PROMPT="a blue ceramic teapot --ar 1:1"
opencli midjourney generate "$PROMPT"
Defensive patterns

Strategy: validation

Validate before calling

function requirePrompt(value) {
  const p = String(value ?? '').replace(/\s+/g, ' ').trim();
  if (!p) throw new Error('prompt cannot be empty');
  return p;
}
const prompt = requirePrompt(process.env.MJ_PROMPT);

Type guard

function hasPrompt(v) {
  return typeof v === 'string' && v.replace(/\s+/g, ' ').trim().length > 0;
}

Try / catch

try {
  await generate({ prompt });
} catch (err) {
  if (err.name === 'ArgumentError' && /prompt cannot be empty/.test(err.message)) {
    console.error('Provide a non-empty prompt, e.g. "a blue ceramic teapot --ar 1:1"');
    process.exitCode = 2;
  } else throw err;
}

Prevention

When it happens

Trigger: Calling generate/interrogate-style commands with an omitted --prompt, an empty string "", a prompt that is only whitespace (spaces, tabs, newlines), or a shell variable that expanded to nothing.

Common situations: Missing quoted argument in scripts so an empty variable is passed, prompts built from config files where the key is unset, or a prompt consisting solely of whitespace after copy-paste of invisible characters stripping to empty.

Related errors


AI-assisted analysis of jackwener/OpenCLI@49907e53dc (2026-08-29). Data as JSON: /api/errors/a657ccff34485a32. Report an issue: GitHub.