{"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":"exception","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/d7d0d0a39494e28ec1c407bd0680e4a4d1067791/src/pip/_internal/req/pep723.py#L17-L42","documentation":"Raised by pep723_metadata() when the content inside a PEP 723 `# /// script` block cannot be parsed as valid TOML (pep723.py:34). The block's comment-stripped content is fed to tomllib.loads; any TOMLDecodeError (or other parse exception) is caught and re-raised as a PEP723Exception.","triggerScenarios":"A script block with malformed TOML: missing quotes around values, unbalanced brackets, invalid keys, stray characters. e.g. `# dependencies = [requests]` (unquoted) or `# /// script` followed by `# bad = = toml`.","commonSituations":"Hand-editing the inline block and forgetting TOML quoting rules. Copy-pasting from a source that dropped quote characters. Tabs or unusual whitespace inside the block.","solutions":["Open the script and fix the TOML inside the `# /// script` block: quote strings, balance brackets, use valid keys.","Validate by extracting the block content and running `python -c \"import tomllib; tomllib.loads(open('block.toml').read())\"`.","Ensure every line inside the block starts with `# ` (hash space) so the stripper at pep723.py:29 works.","Avoid inline comments or TOML features your toolchain doesn't support."],"exampleFix":"# before\n# /// script\n# dependencies = [requests]\n# ///\n# after\n# /// script\n# dependencies = [\"requests\"]\n# ///","handlingStrategy":"validation","validationCode":"import re, tomllib\n\nPEP723_RE = r\"(?m)^# /// (?P<type>[a-zA-Z0-9-]+)$\\s(?P<content>(^#(| .*)$\\s)+)^# ///$\"\n\ndef validate_pep723_toml(scriptfile: str) -> dict:\n    with open(scriptfile, encoding=\"utf8\") as f:\n        script = f.read()\n    m = next((m for m in re.finditer(PEP723_RE, script) if m.group(\"type\") == \"script\"), None)\n    if m is None:\n        raise ValueError(\"no script block\")\n    content = \"\".join(line[2:] if line.startswith(\"# \") else line[1:]\n                       for line in m.group(\"content\").splitlines(keepends=True))\n    try:\n        return tomllib.loads(content)\n    except tomllib.TOMLDecodeError as e:\n        raise ValueError(f\"PEP 723 TOML invalid: {e}\") from e","typeGuard":"null","tryCatchPattern":"from pip._internal.req.pep723 import pep723_metadata, PEP723Exception\n\ntry:\n    meta = pep723_metadata(scriptfile)\nexcept PEP723Exception as e:\n    if \"Failed to parse TOML\" in e.msg:\n        # surface the block to the user for editing\n        ...\n    raise","preventionTips":["Quote all string values inside the PEP 723 block.","Keep each line prefixed with `# ` so the stripper works.","Validate the block's TOML in a pre-commit hook."],"tags":["pep723","toml","script","validation"],"analyzedSha":"d7d0d0a39494e28ec1c407bd0680e4a4d1067791","analyzedAt":"2026-08-04T20:55:04.259Z","schemaVersion":2}