{"record":{"id":"ad22808ca7bbf47d","repo":"invoke-ai/InvokeAI","slug":"failed-to-load-and-migrate-v3-config-file-config","errorCode":null,"errorMessage":"Failed to load and migrate v3 config file {config_path}: {e}","messagePattern":"Failed to load and migrate v3 config file (.+?): (.+?)","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"invokeai/app/services/config/config_default.py","lineNumber":626,"sourceCode":"    if loaded_config_dict[\"schema_version\"] == \"4.0.0\":\n        migrated = True\n        loaded_config_dict = migrate_v4_0_0_to_4_0_1_config_dict(loaded_config_dict)\n    if loaded_config_dict[\"schema_version\"] == \"4.0.1\":\n        migrated = True\n        loaded_config_dict = migrate_v4_0_1_to_4_0_2_config_dict(loaded_config_dict)\n    if loaded_config_dict[\"schema_version\"] == \"4.0.2\":\n        migrated = True\n        loaded_config_dict = migrate_v4_0_2_to_4_0_3_config_dict(loaded_config_dict)\n\n    if migrated:\n        shutil.copy(config_path, config_path.with_suffix(\".yaml.bak\"))\n        try:\n            # load and write without environment variables\n            migrated_config = DefaultInvokeAIAppConfig.model_validate(loaded_config_dict)\n            migrated_config.write_file(config_path)\n        except Exception as e:\n            shutil.copy(config_path.with_suffix(\".yaml.bak\"), config_path)\n            raise RuntimeError(f\"Failed to load and migrate v3 config file {config_path}: {e}\") from e\n\n    try:\n        # Meta is not included in the model fields, so we need to validate it separately\n        config = InvokeAIAppConfig.model_validate(loaded_config_dict)\n        assert config.schema_version == CONFIG_SCHEMA_VERSION, (\n            f\"Invalid schema version, expected {CONFIG_SCHEMA_VERSION}: {config.schema_version}\"\n        )\n        return config\n    except Exception as e:\n        raise RuntimeError(f\"Failed to load config file {config_path}: {e}\") from e\n\n\ndef load_external_api_keys(api_keys_file_path: Path) -> dict[str, str]:\n    \"\"\"Load external provider config (API keys and base URLs) from a dedicated YAML file.\"\"\"\n    if not api_keys_file_path.exists():\n        return {}\n\n    with open(api_keys_file_path, \"rt\", encoding=locale.getpreferredencoding()) as file:","sourceCodeStart":608,"sourceCodeEnd":644,"githubUrl":"https://github.com/invoke-ai/InvokeAI/blob/0b6a024f2ff6a86bfb953dcdb9cc504ef7397a06/invokeai/app/services/config/config_default.py#L608-L644","documentation":"load_and_migrate_config upgrades a v3 config file: it validates the loaded dict with DefaultInvokeAIAppConfig, writes the migrated file, and on any exception restores the .yaml.bak backup and re-raises as RuntimeError 'Failed to load and migrate v3 config file {path}: {e}'. This keeps the original config intact while signaling that migration failed.","triggerScenarios":"Running InvokeAI (or update_runtime_config/get_config) with a v3-era invokeai.yaml that fails model_validate — unknown/invalid keys, wrong types, malformed YAML values, or a write_file failure (permissions, read-only volume) — triggering the except branch.","commonSituations":"Upgrading InvokeAI across major versions with an old hand-edited config; config entries typed incorrectly (string where list expected, e.g. generation_devices); migrating configs in containers with read-only config mounts.","solutions":["Read the inner '{e}' message to find the exact validation failure and fix that key in the YAML","Restore the automatic backup (config_path.yaml.bak was copied back) and fix types/keys before retrying","Rename or move the old config and let InvokeAI generate a fresh one, then re-add settings incrementally","Ensure the config directory is writable so write_file can persist the migrated config"],"exampleFix":"// before (invokeai.yaml, v3)\ngeneration_devices: cuda:0   # invalid type for new schema\n// after\ngeneration_devices:\n  - cuda:0","handlingStrategy":"try-catch","validationCode":"import yaml\ndata = yaml.safe_load(open('invokeai.yaml'))\nfrom invokeai.app.services.config.config_default import DefaultInvokeAIAppConfig\nDefaultInvokeAIAppConfig.model_validate(data)  # surfaces errors before migration writes","typeGuard":null,"tryCatchPattern":"try:\n    config = load_and_migrate_config(path)\nexcept RuntimeError as e:\n    logger.error(f\"Config migration failed: {e}; .yaml.bak was restored\")\n    # fix the offending key reported in the chained exception, then retry","preventionTips":["Keep a manual copy of invokeai.yaml before upgrading","Fix validation errors reported by pydantic before re-running migration","Ensure the config directory is writable","Re-add old config values incrementally rather than carrying the whole v3 file forward"],"tags":["config","migration","runtime-error","pydantic"],"backgroundTag":"config-migration-failed","analyzedSha":"0b6a024f2ff6a86bfb953dcdb9cc504ef7397a06","analyzedAt":"2026-08-29T04:46:49.967Z","schemaVersion":2},"datasetVersion":"2026-08-29T07:17:48.351Z"}