{"record":{"id":"1967eda067a3eeb7","repo":"invoke-ai/InvokeAI","slug":"key-key-does-not-match-parsing-tree-parsing-tre","errorCode":null,"errorMessage":"Key {key} does not match parsing tree {parsing_tree}.","messagePattern":"Key (.+?) does not match parsing tree (.+?)\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"invokeai/backend/patches/lora_conversions/kohya_key_utils.py","lineNumber":56,"sourceCode":"\n    for part in parts:\n        if len(current_part) > 0:\n            current_part = current_part + \"_\"\n        current_part += part\n\n        if current_part in current_tree:\n            # Match found.\n            current_tree = current_tree[current_part]\n            result_parts.append(current_part)\n            current_part = \"\"\n        elif current_part.isnumeric() and INDEX_PLACEHOLDER in current_tree:\n            # Match found with index placeholder.\n            current_tree = current_tree[INDEX_PLACEHOLDER]\n            result_parts.append(current_part)\n            current_part = \"\"\n\n    if len(current_part) > 0:\n        raise ValueError(f\"Key {key} does not match parsing tree {parsing_tree}.\")\n\n    return \".\".join(result_parts)\n\n\ndef generate_kohya_parsing_tree_from_keys(keys: Iterable[str]) -> ParsingTree:\n    \"\"\"Generate a parsing tree from a list of keys.\n\n    Example:\n    ```\n    keys = [\n        \"module_a.module_b.0.attn.to_k\",\n        \"module_a.module_b.1.attn.to_k\",\n        \"module_a.module_c.proj\",\n    ]\n\n    tree = generate_kohya_parsing_tree_from_keys(keys)\n    > {\n    >     \"module_a\": {","sourceCodeStart":38,"sourceCodeEnd":74,"githubUrl":"https://github.com/invoke-ai/InvokeAI/blob/0b6a024f2ff6a86bfb953dcdb9cc504ef7397a06/invokeai/backend/patches/lora_conversions/kohya_key_utils.py#L38-L74","documentation":"insert_periods_into_kohya_key rebuilds dotted keys by walking a parsing tree (generated from known keys) that maps underscore-joined key segments. If after consuming the whole key a non-empty segment remains unmatched, the key cannot be parsed against the tree and ValueError is raised. This catches Kohya/OneTrainer keys whose module names differ from those the tree was built from.","triggerScenarios":"Calling insert_periods_into_kohya_key (directly or via OneTrainer transformer conversion / Krea2 unflattening) with a key containing a segment sequence absent from the parsing tree, or a key longer/deeper than any key used to build the tree.","commonSituations":"LoRAs from a newer trainer with module names unknown to the tree; building the tree from a subset of keys then parsing others; tests like test_insert_periods_into_kohya_key_invalid_key/too_long exercise exactly these cases.","solutions":["Rebuild the parsing tree with generate_kohya_parsing_tree_from_keys including all keys from the LoRA before parsing.","Extend the parsing tree / add the new module naming to the key-conversion logic and upstream it.","Skip or remap the unparseable key if its LoRA component is not required."],"exampleFix":"// before\ntree = generate_kohya_parsing_tree_from_keys(sd_subset_keys)\nkey = insert_periods_into_kohya_key(foreign_key, tree)  # ValueError\n// after\ntree = generate_kohya_parsing_tree_from_keys(sd_subset_keys + [foreign_key])\nkey = insert_periods_into_kohya_key(foreign_key, tree)","handlingStrategy":"validation","validationCode":"from invokeai.backend.patches.lora_conversions.kohya_key_utils import generate_kohya_parsing_tree_from_keys, insert_periods_into_kohya_key\ntree = generate_kohya_parsing_tree_from_keys(all_keys)\nfor k in all_keys:\n    try:\n        insert_periods_into_kohya_key(k, tree)\n    except ValueError as e:\n        raise ValueError(f\"pre-check failed: {e}\")","typeGuard":null,"tryCatchPattern":"try:\n    dotted = insert_periods_into_kohya_key(key, tree)\nexcept ValueError as e:\n    logger.error(\"kohya key unparseable: %s\", e)\n    dotted = None","preventionTips":["Build the parsing tree from every key in the LoRA, not a subset.","Dry-run all keys through insert_periods_into_kohya_key before applying patches.","Extend the tree/converter when new trainer module names appear."],"tags":["lora","kohya","parsing","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"}