python-poetry/poetry · error · TOMLError

Invalid TOML file {self.path.as_posix()}: {e}

Error message

Invalid TOML file {self.path.as_posix()}: {e}

What it means

TOMLFile.read wraps any tomlkit parse error (ValueError or TOMLKitError) into a TOMLError prefixed with the file path (toml/file.py:26-34). It indicates a syntax/semantic problem in the TOML — almost always pyproject.toml — such as duplicate keys, malformed tables, or invalid inline values.

Source

Thrown at src/poetry/toml/file.py:34

        super().__init__(path)
        self.__path = path

    @property
    def path(self) -> Path:
        return self.__path

    def exists(self) -> bool:
        return self.__path.exists()

    def read(self) -> TOMLDocument:
        from tomlkit.exceptions import TOMLKitError

        from poetry.toml import TOMLError

        try:
            return super().read()
        except (ValueError, TOMLKitError) as e:
            raise TOMLError(f"Invalid TOML file {self.path.as_posix()}: {e}")

    def __str__(self) -> str:
        return self.__path.as_posix()

View on GitHub (pinned to 92b74dcfe3)

Solutions

  1. Read the specific tomlkit error appended after the file path — it names the exact parse problem.
  2. Validate the file with a TOML linter or `python -c 'import tomlkit, pathlib; tomlkit.parse(pathlib.Path("pyproject.toml").read_text())'`.
  3. Fix the reported construct (duplicate key, bad inline table, unquoted value).
  4. Restore from version control and re-apply changes incrementally to isolate the bad edit.

Example fix

// before
[tool.poetry.dependencies]
python = "^3.11"
requests = "^2.31"
requests = "^2.32"   # duplicate key -> TOMLError
// after
[tool.poetry.dependencies]
python = "^3.11"
requests = "^2.32"
Defensive patterns

Strategy: validation

Validate before calling

import tomlkit, pathlib
from poetry.toml import TOMLError
p = pathlib.Path("pyproject.toml")
try:
    tomlkit.parse(p.read_text())
except Exception as e:
    raise TOMLError(f"Refusing to proceed; pyproject.toml is invalid: {e}") from e
doc = TOMLFile(p).read()

Try / catch

from poetry.toml import TOMLError
try:
    doc = TOMLFile(path).read()
except TOMLError as e:
    log.error("%s is not valid TOML; fix the parse error before continuing", path)
    raise

Prevention

When it happens

Trigger: Any operation that loads pyproject.toml (poetry install/lock/add/etc.) when the file contains a TOML syntax error; calling TOMLFile(path).read() on a malformed file.

Common situations: Hand-editing pyproject.toml and introducing a duplicate key, unclosed string, or bad table header; a merge/CI tool wrote invalid TOML; mixed tabs/indentation issues.

Related errors


AI-assisted analysis of python-poetry/poetry@92b74dcfe3 (2026-08-04). Data as JSON: /data/errors/2ab4a71669948f9f.json. Report an issue: GitHub.