{"record":{"id":"0ab6e603568c604a","repo":"invoke-ai/InvokeAI","slug":"key-key-does-not-match-the-expected-pattern-fo","errorCode":null,"errorMessage":"Key '{key}' does not match the expected pattern for xlabs FLUX LoRA weights.","messagePattern":"Key '(.+?)' does not match the expected pattern for xlabs FLUX LoRA weights\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"invokeai/backend/patches/lora_conversions/flux_xlabs_lora_conversion_utils.py","lineNumber":67,"sourceCode":"    The xlabs format uses:\n    - lora1 for image attention stream (img_attn)\n    - lora2 for text attention stream (txt_attn)\n    - qkv for query/key/value projection\n    - proj for output projection\n\n    Key mapping:\n    - double_blocks.X.processor.qkv_lora1 -> double_blocks.X.img_attn.qkv\n    - double_blocks.X.processor.proj_lora1 -> double_blocks.X.img_attn.proj\n    - double_blocks.X.processor.qkv_lora2 -> double_blocks.X.txt_attn.qkv\n    - double_blocks.X.processor.proj_lora2 -> double_blocks.X.txt_attn.proj\n    \"\"\"\n    # Group keys by layer (without the .down.weight/.up.weight suffix)\n    grouped_state_dict: dict[str, dict[str, torch.Tensor]] = {}\n\n    for key, value in state_dict.items():\n        match = re.match(FLUX_XLABS_KEY_REGEX, key)\n        if not match:\n            raise ValueError(f\"Key '{key}' does not match the expected pattern for xlabs FLUX LoRA weights.\")\n\n        block_idx = match.group(1)\n        component = match.group(2)  # qkv or proj\n        lora_stream = match.group(3)  # 1 or 2\n        direction = match.group(4)  # down or up\n\n        # Map lora1 -> img_attn, lora2 -> txt_attn\n        attn_type = \"img_attn\" if lora_stream == \"1\" else \"txt_attn\"\n\n        # Create the InvokeAI-style layer key\n        layer_key = f\"double_blocks.{block_idx}.{attn_type}.{component}\"\n\n        if layer_key not in grouped_state_dict:\n            grouped_state_dict[layer_key] = {}\n\n        # Map down/up to lora_down/lora_up\n        param_name = f\"lora_{direction}.weight\"\n        grouped_state_dict[layer_key][param_name] = value","sourceCodeStart":49,"sourceCodeEnd":85,"githubUrl":"https://github.com/invoke-ai/InvokeAI/blob/0b6a024f2ff6a86bfb953dcdb9cc504ef7397a06/invokeai/backend/patches/lora_conversions/flux_xlabs_lora_conversion_utils.py#L49-L85","documentation":"lora_model_from_flux_xlabs_state_dict expects every key to match FLUX_XLABS_KEY_REGEX, which encodes block index, component (qkv/proj), lora stream (1/2), and direction (down/up). Any non-conforming key raises ValueError before grouping. The file is not a valid xlabs FLUX LoRA.","triggerScenarios":"Passing a state dict with keys not matching the xlabs pattern (e.g. Kohya 'lora_unet_...' keys, diffusers 'transformer...' keys, or truncated xlabs keys missing the .down.weight/.up.weight suffix) to lora_model_from_flux_xlabs_state_dict.","commonSituations":"Using the xlabs converter on a diffusers/kohya FLUX LoRA (wrong converter choice); xlabs tooling version differences renaming keys; manual key editing or partial extraction from a safetensors file.","solutions":["Confirm the LoRA really is the xlabs format; if it's Kohya/diffusers/OneTrainer, call the matching converter instead.","Compare the failing key to FLUX_XLABS_KEY_REGEX and fix or filter non-conforming keys before conversion.","Re-export the LoRA using xlabs tooling so keys follow the expected pattern."],"exampleFix":null,"handlingStrategy":"validation","validationCode":"import re\nfrom invokeai.backend.patches.lora_conversions.flux_xlabs_lora_conversion_utils import FLUX_XLABS_KEY_REGEX\nbad = [k for k in state_dict if not re.match(FLUX_XLABS_KEY_REGEX, k)]\nassert not bad, f\"keys not matching xlabs pattern: {bad[:5]}\"","typeGuard":"def is_xlabs_key(k: str) -> bool:\n    return bool(re.match(FLUX_XLABS_KEY_REGEX, k))","tryCatchPattern":"try:\n    lora = lora_model_from_flux_xlabs_state_dict(sd, model)\nexcept ValueError as e:\n    logger.error(\"not an xlabs FLUX LoRA: %s\", e)\n    lora = None","preventionTips":["Verify the file is xlabs format before choosing this converter.","Pre-validate keys with FLUX_XLABS_KEY_REGEX.","Use the correct converter per export tool (kohya/diffusers/onetrainer/xlabs)."],"tags":["lora","flux","xlabs","regex","key-format"],"backgroundTag":"unsupported-lora-key-format","analyzedSha":"0b6a024f2ff6a86bfb953dcdb9cc504ef7397a06","analyzedAt":"2026-08-29T04:46:49.967Z","schemaVersion":2},"datasetVersion":"2026-08-29T07:17:48.351Z"}