TOML error: duplicate key / Cannot overwrite a value

Unlike JSON and many YAML parsers, TOML makes duplicate keys a hard error: every key may be defined only once in its table. The parser stops at the second definition and reports its line. The first definition is usually still correct, so decide which value you want and delete the other line.

Seen as:

  • tomllib.TOMLDecodeError: Cannot overwrite a value (at line 5, column 18)
  • duplicate key `version` in table `project`
  • Trying to redefine an already defined table or value

Input

Settings

History

Load from URL

Common causes

1. The same key written twice in a table

Bumping a version or a setting by adding a new line instead of editing the old one is the usual source. Keep one line.

Before
[project]
version = "1.2.0"
name = "invoice-tool"
version = "1.3.0"
After
[project]
version = "1.3.0"
name = "invoice-tool"

2. A dotted key that extends a plain value

server = "web-1" makes server a string, so server.port cannot add a key to it. Turn server into a table and give the old value its own key.

Before
server = "web-1"
server.port = 80
After
server.name = "web-1"
server.port = 80

3. Bare and quoted spellings of the same key

Quotes do not change a key: name and "name" are identical, so the second line redefines the first. Keys are case-sensitive, so Name would be a different key.

Before
name = "invoice-tool"
"name" = "invoice-cli"
After
name = "invoice-cli"

4. A key that also appears inside an inline table

Inline tables are complete when they close. Adding a key to owner afterwards, with a dotted key or a section, redefines it. Put every key in the inline table or switch to a [owner] section.

Before
owner = { name = "Ada" }
owner.email = "ada@example.com"
After
owner = { name = "Ada", email = "ada@example.com" }

Frequently asked questions

Why does TOML reject duplicates when JSON allows them?

TOML was designed to map unambiguously to a hash table, so the spec explicitly says defining a key multiple times is invalid. That makes config errors visible instead of silently keeping one value.

Where does Cargo report the duplicate?

Cargo prints “TOML parse error at line N, column M” with the offending line, followed by “duplicate key name in table package”. The line shown is the second definition.

Is the same key allowed in different tables?

Yes. version under [project] and version under [tool.poetry] are separate keys because they live in different tables.

Related