invoke-ai/InvokeAI · error · ValueError

Key '{k}' does not match the expected pattern for FLUX LoRA

Error message

Key '{k}' does not match the expected pattern for FLUX LoRA weights.

What it means

_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.

Source

Thrown at invokeai/backend/patches/lora_conversions/flux_kohya_lora_conversion_utils.py:127


def _convert_flux_clip_kohya_state_dict_to_invoke_format(state_dict: Dict[str, T]) -> Dict[str, T]:
    """Converts a CLIP LoRA state dict from the Kohya FLUX LoRA format to LoRA weight format used internally by
    InvokeAI.

    Example key conversions:

    "lora_te1_text_model_encoder_layers_0_mlp_fc1" -> "text_model.encoder.layers.0.mlp.fc1",
    "lora_te1_text_model_encoder_layers_0_self_attn_k_proj" -> "text_model.encoder.layers.0.self_attn.k_proj"
    """
    converted_sd: dict[str, T] = {}
    for k, v in state_dict.items():
        match = re.match(FLUX_KOHYA_CLIP_KEY_REGEX, k)
        if match:
            new_key = f"text_model.encoder.layers.{match.group(1)}.{match.group(2)}.{match.group(3)}"
            converted_sd[new_key] = v
        else:
            raise ValueError(f"Key '{k}' does not match the expected pattern for FLUX LoRA weights.")

    return converted_sd


def _convert_flux_transformer_kohya_state_dict_to_invoke_format(state_dict: Dict[str, T]) -> Dict[str, T]:
    """Converts a FLUX tranformer LoRA state dict from the Kohya FLUX LoRA format to LoRA weight format used internally
    by InvokeAI.

    Example key conversions:
    "lora_unet_double_blocks_0_img_attn_proj" -> "double_blocks.0.img_attn.proj"
    "lora_unet_double_blocks_0_img_attn_qkv" -> "double_blocks.0.img_attn.qkv"
    """

    def replace_func(match: re.Match[str]) -> str:
        s = f"{match.group(1)}.{match.group(2)}.{match.group(3)}"
        if match.group(4):
            s += f".{match.group(4)}"
        return s

View on GitHub (pinned to 0b6a024f2f)

Solutions

  1. Print the offending key and compare with FLUX_KOHYA_CLIP_KEY_REGEX; extend the regex to cover the new naming and upstream the change.
  2. Pre-filter the state dict, dropping lora_te1 keys that don't match, if text-encoder LoRA isn't needed.
  3. Re-export the LoRA from a standard FLUX Kohya trainer.
Defensive patterns

Strategy: validation

Validate before calling

import re
from invokeai.backend.patches.lora_conversions.flux_kohya_lora_conversion_utils import FLUX_KOHYA_CLIP_KEY_REGEX
bad = [k for k in clip_grouped_sd if not re.match(FLUX_KOHYA_CLIP_KEY_REGEX, k)]
assert not bad, f"CLIP keys not matching pattern: {bad[:5]}"

Try / catch

try:
    clip_sd = _convert_flux_clip_kohya_state_dict_to_invoke_format(clip_grouped_sd)
except ValueError as e:
    logger.error("CLIP key pattern mismatch: %s", e)
    clip_sd = {}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of invoke-ai/InvokeAI@0b6a024f2f (2026-08-29). Data as JSON: /api/errors/a736887317424411. Report an issue: GitHub.