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
  • tomllib.TOMLDecodeError: Cannot declare ('dependencies',) twice (at line 8, column 14)
  • invalid table header duplicate key `dependencies` in document root

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 = "[email protected]"
After
owner = { name = "Ada", email = "[email protected]" }

5. The same [section] header pasted twice

A [table] header may appear only once, even if the keys underneath differ. Installation instructions often show a complete [dependencies] section, so add the new lines to the existing section instead of pasting the header again.

Before
[dependencies]
serde = "1"

[dependencies]
tokio = "1"
After
[dependencies]
serde = "1"
tokio = "1"

Other ways to define a table twice

Repeated headers are the common case, but two other patterns produce the same error.

An inline table is already a complete definition, so a later header with the same name redefines it. Pick one style for the whole table:

# Rejected
server = { host = "0.0.0.0" }

[server]
port = 8080

# Accepted
[server]
host = "0.0.0.0"
port = 8080

[[bin]] declares an array of tables, so a later [bin] tries to turn it into a plain table. Repeat the double-bracket form for every element:

[[bin]]
name = "ingest"

[[bin]]
name = "export"

Python’s tomllib describes table paths as tuples of key names, which is why the message shows ('dependencies',). A value of ('tool', 'poetry') would mean the [tool.poetry] table.

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.

Can I define [a.b] before [a]?

Yes. Defining a sub-table first and its parent later is valid, as long as the parent header appears only once and does not redefine keys the sub-table header already created.

How do I add many entries to the same table across a file?

You cannot reopen a table in TOML. Keep all of its keys under one header, or define the whole table with dotted keys in the root (dependencies.serde = “1”) and no header at all; the two styles cannot be mixed for the same table.

Related