{"record":{"id":"a736887317424411","repo":"invoke-ai/InvokeAI","slug":"key-k-does-not-match-the-expected-pattern-for","errorCode":null,"errorMessage":"Key '{k}' does not match the expected pattern for FLUX LoRA weights.","messagePattern":"Key '(.+?)' does not match the expected pattern for FLUX LoRA weights\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"invokeai/backend/patches/lora_conversions/flux_kohya_lora_conversion_utils.py","lineNumber":127,"sourceCode":"\n\ndef _convert_flux_clip_kohya_state_dict_to_invoke_format(state_dict: Dict[str, T]) -> Dict[str, T]:\n    \"\"\"Converts a CLIP LoRA state dict from the Kohya FLUX LoRA format to LoRA weight format used internally by\n    InvokeAI.\n\n    Example key conversions:\n\n    \"lora_te1_text_model_encoder_layers_0_mlp_fc1\" -> \"text_model.encoder.layers.0.mlp.fc1\",\n    \"lora_te1_text_model_encoder_layers_0_self_attn_k_proj\" -> \"text_model.encoder.layers.0.self_attn.k_proj\"\n    \"\"\"\n    converted_sd: dict[str, T] = {}\n    for k, v in state_dict.items():\n        match = re.match(FLUX_KOHYA_CLIP_KEY_REGEX, k)\n        if match:\n            new_key = f\"text_model.encoder.layers.{match.group(1)}.{match.group(2)}.{match.group(3)}\"\n            converted_sd[new_key] = v\n        else:\n            raise ValueError(f\"Key '{k}' does not match the expected pattern for FLUX LoRA weights.\")\n\n    return converted_sd\n\n\ndef _convert_flux_transformer_kohya_state_dict_to_invoke_format(state_dict: Dict[str, T]) -> Dict[str, T]:\n    \"\"\"Converts a FLUX tranformer LoRA state dict from the Kohya FLUX LoRA format to LoRA weight format used internally\n    by InvokeAI.\n\n    Example key conversions:\n    \"lora_unet_double_blocks_0_img_attn_proj\" -> \"double_blocks.0.img_attn.proj\"\n    \"lora_unet_double_blocks_0_img_attn_qkv\" -> \"double_blocks.0.img_attn.qkv\"\n    \"\"\"\n\n    def replace_func(match: re.Match[str]) -> str:\n        s = f\"{match.group(1)}.{match.group(2)}.{match.group(3)}\"\n        if match.group(4):\n            s += f\".{match.group(4)}\"\n        return s","sourceCodeStart":109,"sourceCodeEnd":145,"githubUrl":"https://github.com/invoke-ai/InvokeAI/blob/0b6a024f2ff6a86bfb953dcdb9cc504ef7397a06/invokeai/backend/patches/lora_conversions/flux_kohya_lora_conversion_utils.py#L109-L145","documentation":"_convert_flux_clip_kohya_state_dict_to_invoke_format rewrites CLIP LoRA keys via the FLUX_KOHYA_CLIP_KEY_REGEX. Keys that don't match the regex raise ValueError. This is a per-key strict validation ensuring the grouped lora_te1_* keys conform to the expected pattern.","triggerScenarios":"Calling lora_model_from_flux_kohya_state_dict (or onetrainer variant) on a file where a lora_te1_* key deviates from FLUX_KOHYA_CLIP_KEY_REGEX - e.g. different block naming, extra segments, or an SDXL-style text encoder key.","commonSituations":"LoRAs from trainers that customize text-encoder layer naming; keys like 'lora_te1_text_model_encoder_layers_0_mlp_fc1' with unexpected extra suffixes; OneTrainer files routed through the CLIP converter with nonstandard keys.","solutions":["Print the offending key and compare with FLUX_KOHYA_CLIP_KEY_REGEX; extend the regex to cover the new naming and upstream the change.","Pre-filter the state dict, dropping lora_te1 keys that don't match, if text-encoder LoRA isn't needed.","Re-export the LoRA from a standard FLUX Kohya trainer."],"exampleFix":null,"handlingStrategy":"validation","validationCode":"import re\nfrom invokeai.backend.patches.lora_conversions.flux_kohya_lora_conversion_utils import FLUX_KOHYA_CLIP_KEY_REGEX\nbad = [k for k in clip_grouped_sd if not re.match(FLUX_KOHYA_CLIP_KEY_REGEX, k)]\nassert not bad, f\"CLIP keys not matching pattern: {bad[:5]}\"","typeGuard":null,"tryCatchPattern":"try:\n    clip_sd = _convert_flux_clip_kohya_state_dict_to_invoke_format(clip_grouped_sd)\nexcept ValueError as e:\n    logger.error(\"CLIP key pattern mismatch: %s\", e)\n    clip_sd = {}","preventionTips":["Pre-match keys against FLUX_KOHYA_CLIP_KEY_REGEX before conversion.","Drop text-encoder keys if the regex doesn't cover your trainer's naming and TE LoRA is optional.","Extend and upstream the regex when trainers add new key shapes."],"tags":["lora","flux","kohya","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"}