abi/screenshot-to-code · error · ValueError

Invalid input mode: {input_mode}

Error message

Invalid input mode: {input_mode}

What it means

Error emitted via throw_error in backend/routes/generate_code.py:299 when params['inputMode'] is absent or not a member of the InputMode literal union (screenshot vs import modes). The check runs immediately after stack validation, so a request failing here already passed the stack check.

Source

Thrown at backend/routes/generate_code.py:299

        self.throw_error = throw_error
        self.asset_base_url = asset_base_url

    async def extract_and_validate(self, params: Dict[str, Any]) -> ExtractedParams:
        """Extract and validate all parameters from the request"""
        # Read the code config settings (stack) from the request.
        generated_code_config = params.get("generatedCodeConfig", "")
        if generated_code_config not in get_args(Stack):
            await self.throw_error(
                f"Invalid generated code config: {generated_code_config}"
            )
            raise ValueError(f"Invalid generated code config: {generated_code_config}")
        validated_stack = cast(Stack, generated_code_config)

        # Validate the input mode
        input_mode = params.get("inputMode")
        if input_mode not in get_args(InputMode):
            await self.throw_error(f"Invalid input mode: {input_mode}")
            raise ValueError(f"Invalid input mode: {input_mode}")
        validated_input_mode = cast(InputMode, input_mode)

        openai_api_key = self._get_from_settings_dialog_or_env(
            params, "openAiApiKey", OPENAI_API_KEY
        )

        # If neither is provided, we throw an error later only if Claude is used.
        anthropic_api_key = self._get_from_settings_dialog_or_env(
            params, "anthropicApiKey", ANTHROPIC_API_KEY
        )
        gemini_api_key = self._get_from_settings_dialog_or_env(
            params, "geminiApiKey", GEMINI_API_KEY
        )
        replicate_api_key = self._get_from_settings_dialog_or_env(
            params, "replicateApiKey", REPLICATE_API_KEY
        )

        # Base URL for OpenAI API

View on GitHub (pinned to d026163f58)

Solutions

  1. Set inputMode explicitly to one of the backend's InputMode values (see the InputMode type the backend defines).
  2. Update the mismatched side (frontend or backend) so both know the same mode names.
  3. For custom clients, log the exact payload before sending to spot undefined fields.
Defensive patterns

Strategy: type-guard

Type guard

const INPUT_MODES = ['screenshot', 'import'] as const;
type InputMode = typeof INPUT_MODES[number];
function isInputMode(v: unknown): v is InputMode {
  return typeof v === 'string' && (INPUT_MODES as readonly string[]).includes(v);
}

Try / catch

if (msg.type === 'error' && msg.message.startsWith('Invalid input mode')) {
  // send a known mode from the backend's union and retry
}

Prevention

When it happens

Trigger: WebSocket generation request with inputMode missing, undefined, or an unrecognized value (e.g. 'file' instead of the defined import mode name).

Common situations: Version skew between frontend and backend after a new input mode was introduced; custom automation clients omitting inputMode; typos in mode strings.

Related errors


AI-assisted analysis of abi/screenshot-to-code@d026163f58 (2026-08-14). Data as JSON: /api/errors/9d89a904d280bc86. Report an issue: GitHub.