TOML error: table defined twice

A [table] header may appear only once in a TOML document. Writing the same header a second time, typically when pasting a snippet from documentation into Cargo.toml or pyproject.toml, is an error even if the keys underneath are different. Move the keys from the second section into the first and delete the duplicate header.

Seen as:

  • tomllib.TOMLDecodeError: Cannot declare ('dependencies',) twice (at line 8, column 14)
  • invalid table header duplicate key `dependencies` in document root
  • Trying to redefine an already defined table or value

Input

Settings

History

Load from URL

Common causes

1. The same section pasted twice

Installation instructions often show a complete [dependencies] section. 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"

2. A table header after an inline table of the same name

server = { host = "x" } already defines the table server, and inline tables cannot be extended later. Use one style for the whole table.

Before
server = { host = "0.0.0.0" }

[server]
port = 8080
After
[server]
host = "0.0.0.0"
port = 8080

3. Mixing [[array]] and [table] for the same name

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

Before
[[bin]]
name = "ingest"

[bin]
name = "export"
After
[[bin]]
name = "ingest"

[[bin]]
name = "export"

Frequently asked questions

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.

Why does tomllib show a tuple like ('dependencies',)?

tomllib describes table paths as Python tuples of key names. (‘tool’, ‘poetry’) would mean the [tool.poetry] table.

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