{"record":{"id":"da80e8004849ff3b","repo":"pypa/pip","slug":"failed-to-parse-toml-in-scriptfile-r","errorCode":null,"errorMessage":"Failed to parse TOML in {scriptfile!r}","messagePattern":"Failed to parse TOML in (.+?)","errorType":"validation","errorClass":"PEP723Exception","httpStatus":null,"severity":"error","filePath":"src/pip/_internal/req/pep723.py","lineNumber":35,"sourceCode":"    with open(scriptfile, encoding=\"utf8\") as f:\n        script = f.read()\n\n    name = \"script\"\n    matches = list(\n        filter(lambda m: m.group(\"type\") == name, re.finditer(REGEX, script))\n    )\n\n    if len(matches) > 1:\n        raise PEP723Exception(f\"Multiple {name!r} blocks found in {scriptfile!r}\")\n    elif len(matches) == 1:\n        content = \"\".join(\n            line[2:] if line.startswith(\"# \") else line[1:]\n            for line in matches[0].group(\"content\").splitlines(keepends=True)\n        )\n        try:\n            metadata = tomllib.loads(content)\n        except Exception as exc:\n            raise PEP723Exception(f\"Failed to parse TOML in {scriptfile!r}\") from exc\n    else:\n        raise PEP723Exception(\n            f\"File does not contain {name!r} metadata: {scriptfile!r}\"\n        )\n\n    return metadata\n","sourceCodeStart":17,"sourceCodeEnd":42,"githubUrl":"https://github.com/pypa/pip/blob/f399c3718970b1b0e2478dac5296eb62679a9b86/src/pip/_internal/req/pep723.py#L17-L42","documentation":"pip extracted the content of a PEP 723 '# /// script' block but tomllib.loads() raised an exception when parsing it. The block content must be valid TOML. The original exception is chained via 'from exc' for debugging.","triggerScenarios":"The TOML inside a '# /// script' block has a syntax error such as an unclosed array bracket, a missing value, or a duplicate key. For example: '# dependencies = [\"requests\"' without the closing bracket.","commonSituations":"Hand-editing the inline metadata block and introducing a typo. Quote mismatches (mixing single and double). Forgetting a comma in an array. Trailing content after a value.","solutions":["Copy the TOML from inside the '# /// script' block and validate it with a TOML linter or 'python -c \"import tomllib; tomllib.loads(open(\\\"x\\\").read())\"'","Check for unclosed brackets, missing commas, and mismatched quotes","Ensure the content is stripped of the '# ' comment prefix correctly — each line inside the block must start with '# ' or '#'"],"exampleFix":"# before\n# /// script\n# dependencies = [\"requests\"\n# ///\n\n# after\n# /// script\n# dependencies = [\"requests\"]\n# ///","handlingStrategy":"validation","validationCode":"import re\nfrom pip._internal.utils.compat import tomllib\n\nREGEX = r\"(?m)^# /// (?P<type>[a-zA-Z0-9-]+)$\\s(?P<content>(^#(| .*)$\\s)+)^# ///$\"\n\ndef validate_pep723_toml(filepath: str) -> bool:\n    with open(filepath) as f:\n        script = f.read()\n    matches = [m for m in re.finditer(REGEX, script) if m.group(\"type\") == \"script\"]\n    if not matches:\n        return False\n    content = \"\".join(\n        line[2:] if line.startswith(\"# \") else line[1:]\n        for line in matches[0].group(\"content\").splitlines(keepends=True)\n    )\n    try:\n        tomllib.loads(content)\n        return True\n    except Exception:\n        return False","typeGuard":"from pip._internal.utils.compat import tomllib\n\ndef is_valid_pep723_toml(content: str) -> bool:\n    \"\"\"Type guard: True if the extracted PEP 723 content is valid TOML.\"\"\"\n    try:\n        tomllib.loads(content)\n        return True\n    except tomllib.TOMLDecodeError:\n        return False","tryCatchPattern":"from pip._internal.req.pep723 import pep723_metadata, PEP723Exception\n\ntry:\n    meta = pep723_metadata(\"script.py\")\nexcept PEP723Exception as e:\n    if \"Failed to parse TOML\" in e.msg:\n        print(\"Fix the TOML syntax inside the '# /// script' block\")\n    else:\n        raise","preventionTips":["Validate TOML inside PEP 723 blocks with a TOML linter before running","Check for unclosed arrays and missing commas after editing dependency lists","Each line inside the block must start with '# ' or '#'"],"tags":["pip","pep723","toml","parsing","inline-script"],"backgroundTag":null,"analyzedSha":"f399c3718970b1b0e2478dac5296eb62679a9b86","analyzedAt":"2026-08-08T23:01:42.227Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}