{"record":{"id":"f2768069bbfbc29d","repo":"huggingface/transformers","slug":"source-pattern-pattern-contains-1-backrefere","errorCode":null,"errorMessage":"Source pattern '{pattern}' contains \\\\1 backreference, but no capturing groups found in target_patterns.","messagePattern":"Source pattern '\\{pattern\\}' contains \\\\\\\\1 backreference, but no capturing groups found in target_patterns\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"src/transformers/core_model_loading.py","lineNumber":820,"sourceCode":"            if captured_group is not None:\n                target_capturing_groups.append(captured_group)\n\n        # Validate that we only have one unique capturing group pattern across all targets\n        # This ensures deterministic reverse mapping when sources have \\1 backreferences\n        unique_capturing_groups = set(target_capturing_groups)\n        if len(unique_capturing_groups) > 1:\n            raise ValueError(\n                f\"Multiple different capturing groups found in target_patterns: {unique_capturing_groups}. \"\n                f\"All target patterns must use the same capturing group pattern.\"\n            )\n        unique_capturing_group = unique_capturing_groups.pop() if unique_capturing_groups else None\n\n        # We also need to check capturing groups in the sources during reverse mapping (e.g. timm_wrapper, sam3)\n        for i, pattern in enumerate(self.source_patterns):\n            # Replace capturing groups\n            if r\"\\1\" in pattern:\n                if unique_capturing_group is None:\n                    raise ValueError(\n                        f\"Source pattern '{pattern}' contains \\\\1 backreference, but no capturing groups \"\n                        f\"found in target_patterns.\"\n                    )\n                # Use the unique capturing group from target_patterns for all sources\n                pattern = pattern.replace(r\"\\1\", unique_capturing_group, 1)\n            # Potentially process a bit more for consistency - only if they are consistent pairs, i.e. the length is the same\n            if len(self.source_patterns) == len(self.target_patterns):\n                pattern = process_source_pattern(pattern, self._original_target_patterns[i])\n            self.source_patterns[i] = pattern\n\n        # Construct the regex we will use to rename keys from the sources to the targets\n        branches = []\n        for i, source_pattern in enumerate(self.source_patterns):\n            group_name = f\"g{i}\"\n            pattern = source_pattern.replace(\".*.\", r\"\\..*\\.\")\n            branches.append(f\"(?P<{group_name}>{pattern})\")\n        self.compiled_sources = re.compile(\"|\".join(branches))\n","sourceCodeStart":802,"sourceCodeEnd":838,"githubUrl":"https://github.com/huggingface/transformers/blob/a597f974857b3d92939971296bc0deb93d33d780/src/transformers/core_model_loading.py#L802-L838","documentation":"Raised in WeightTransform.__init__ (core_model_loading.py:820). When a source pattern contains the backreference \\1 (meaning 'insert whatever the target's capturing group matched'), at least one target pattern must define a capturing group to supply that content. If no target pattern has a capturing group, there is nothing to substitute into \\1 and the transform is ill-defined, so init fails immediately.","triggerScenarios":"WeightTransform(source_patterns=[r'blocks.\\1.attn'], target_patterns=[r'layers.attn']) — the source uses \\1 but no target contains a (...) group. Common when the author forgets the parentheses in the target pattern.","commonSituations":"Authoring bidirectional (round-trippable) conversion recipes: \\1 in sources is what makes the reverse mapping reconstruct original names. Omitting the group in targets (or using a non-capturing (?:...) group) breaks the reverse direction and is rejected up front.","solutions":["Add the capturing group to the target pattern(s), e.g. target r'layers.(\\d+).attn' so \\1 in sources resolves to the matched digits.","Ensure ALL targets use that same group (see the sibling check for multiple distinct groups).","If you do not need reverse mapping, remove \\1 from the source patterns and write the source pattern explicitly."],"exampleFix":"# before\nWeightTransform(source_patterns=[r'blk.\\1.attn'], target_patterns=[r'layers.attn'])\n\n# after\nWeightTransform(source_patterns=[r'blk.\\1.attn'], target_patterns=[r'layers.(\\d+).attn'])","handlingStrategy":"validation","validationCode":"has_target_group = any(re.search(r'\\((?!\\?:)', p) for p in target_patterns)\nuses_backref = any(r'\\1' in p for p in source_patterns)\nassert not (uses_backref and not has_target_group), 'source \\\\1 backreference needs a capturing group in target_patterns'","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Whenever a source pattern uses \\\\1, immediately add the matching (...) group to the targets and keep them adjacent in code.","Prefer writing target patterns first (with the group), then derive sources.","Unit-test reverse_transform() for every recipe so bad backreferences fail in CI, not at save time."],"tags":["weight-conversion","regex","backreference","programmer-error"],"backgroundTag":null,"analyzedSha":"a597f974857b3d92939971296bc0deb93d33d780","analyzedAt":"2026-08-14T18:24:08.354Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}