{"record":{"id":"0da53ef4cb8de563","repo":"FoundationAgents/MetaGPT","slug":"invalid-escape-0-r","errorCode":null,"errorMessage":"Invalid \\\\escape: {0!r}","messagePattern":"Invalid \\\\\\\\escape: (.+?)","errorType":"exception","errorClass":"JSONDecodeError","httpStatus":null,"severity":"error","filePath":"metagpt/utils/custom_decoder.py","lineNumber":255,"sourceCode":"        elif terminator != \"\\\\\":\n            if strict:\n                # msg = \"Invalid control character %r at\" % (terminator,)\n                msg = \"Invalid control character {0!r} at\".format(terminator)\n                raise JSONDecodeError(msg, s, end)\n            else:\n                _append(terminator)\n                continue\n        try:\n            esc = s[end]\n        except IndexError:\n            raise JSONDecodeError(\"Unterminated string starting at\", s, begin) from None\n        # If not a unicode escape sequence, must be in the lookup table\n        if esc != \"u\":\n            try:\n                char = _b[esc]\n            except KeyError:\n                msg = \"Invalid \\\\escape: {0!r}\".format(esc)\n                raise JSONDecodeError(msg, s, end)\n            end += 1\n        else:\n            uni = _decode_uXXXX(s, end)\n            end += 5\n            if 0xD800 <= uni <= 0xDBFF and s[end : end + 2] == \"\\\\u\":\n                uni2 = _decode_uXXXX(s, end + 1)\n                if 0xDC00 <= uni2 <= 0xDFFF:\n                    uni = 0x10000 + (((uni - 0xD800) << 10) | (uni2 - 0xDC00))\n                    end += 6\n            char = chr(uni)\n        _append(char)\n    return \"\".join(chunks), end\n\n\nscanstring = py_scanstring\n\n\nclass CustomDecoder(json.JSONDecoder):","sourceCodeStart":237,"sourceCodeEnd":273,"githubUrl":"https://github.com/FoundationAgents/MetaGPT/blob/11cdf466d042aece04fc6cfd13b28e1a70341b1f/metagpt/utils/custom_decoder.py#L237-L273","documentation":"Raised inside py_scanstring: after a backslash, the following character is not 'u' and is not in the BACKSLASH escape lookup table ({\\\" \\\\ / b f n r t}), so it is not a valid JSON escape sequence.","triggerScenarios":"Decoding strings containing invalid escapes such as \\\\x, \\\\a, \\\\ ', or \\\\d — e.g. '{\"re\": \"\\\\d+\"}' where the intent was a regex but the backslash is not doubled, or Python-style escapes (\\\\x41) inside JSON.","commonSituations":"Embedding regular expressions, Windows paths, or LaTeX/markdown text into JSON without escaping backslashes; LLMs emitting single backslashes in regex values; converting Python string literals to JSON with str() instead of json.dumps.","solutions":["Double every literal backslash in the JSON string (\\\\d -> \\\\\\\\d).","Build the JSON with json.dumps so escaping is handled automatically.","Pre-normalize the text with a regex that doubles lone backslashes not part of valid escapes.","If the source is an LLM, prompt it to emit valid JSON escapes or re-run the generation."],"exampleFix":"// before\n'{\"pattern\": \"\\\\d{3}\"}'  # Invalid \\\\escape: 'd'\n\n// after\n'{\"pattern\": \"\\\\\\\\d{3}\"}'  # or in Python: json.dumps({\"pattern\": r\"\\d{3}\"})","handlingStrategy":"validation","validationCode":"import re\nVALID = set('\"\\\\/bfnrtu')\ndef escapes_valid(s: str) -> bool:\n    return all(m.group(1) in VALID for m in re.finditer(r'\\\\(.)', s))","typeGuard":null,"tryCatchPattern":"try:\n    obj = decoder.decode(s)\nexcept json.JSONDecodeError as e:\n    if e.msg.startswith('Invalid \\\\escape'):\n        obj = decoder.decode(re.sub(r'\\\\(?![\"\\\\/bfnrtu])', r'\\\\\\\\', s))","preventionTips":["Double backslashes in regex/Windows-path values (\\\\d -> \\\\\\\\d).","Never convert Python literals to JSON with str(); use json.dumps.","Prompt LLMs to emit JSON-escaped strings and validate before use."],"tags":["json","parsing","escaping","regex"],"backgroundTag":null,"analyzedSha":"11cdf466d042aece04fc6cfd13b28e1a70341b1f","analyzedAt":"2026-08-14T23:20:02.994Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}