Skip to content

Menu

TOML Cheat Sheet: Keys, Strings, Tables and Arrays of Tables

Checked against TOML TOML 1.0.0 spec (1.1.0 changes noted); examples parsed with Python 3.12.8 tomllib on 28 November 2026. Each section links to the official docs it was checked against.

This sheet is for anyone editing pyproject.toml, Cargo.toml, a Hugo config or any other TOML file and getting parse errors they don't understand. It follows the TOML 1.0.0 spec and notes what TOML 1.1.0 (released 18 Dec 2025) relaxes. Every valid and invalid snippet below was fed to Python 3.12.8's tomllib, which implements TOML 1.0, and the error messages quoted are the ones it printed.

When to use what

Pick TOML when people edit the file by hand and the data is mostly flat sections of settings. It has real types (dates, integers, floats) and no indentation rules, so it avoids the classic YAML traps covered in the YAML syntax mistakes guide. It gets awkward when data is deeply nested or is a long list of objects; at that point JSON or YAML reads better.

Inside TOML, the choices are mostly about strings and tables. Use literal strings ('...') for Windows paths and regexes, since backslashes mean nothing there. Use basic strings ("...") when you need an escape such as \n or \u00E9. For tables, use a [header] when a section has more than two or three keys, dotted keys (server.port = 8080) for one-off nested values, and inline tables only for tiny objects like { x = 1, y = 2 }. A list of similar objects (dependencies, products, routes) is what [[double brackets]] are for.

To check a file or see what a parser makes of it, paste it into the TOML formatter or the TOML to JSON converter; seeing the JSON tree is the fastest way to spot a key that landed in the wrong table. The TOML configuration guide covers project layout in more depth.

Bare keys are limited to ASCII letters, digits, _ and -. Anything else needs quotes.

name = "api"

A key/value pair; the value must have a type, so bare words like hello are an error

"quoted key" = 1

Quote a key that contains spaces, dots or non-ASCII characters

site.name = "docs"

Dotted key: creates the table site and sets name inside it

1234 = "x"

A digits-only bare key is allowed but is always a string key, never a number

From the docs, not run by us
a = 1 a = 2

Defining the same key twice is an error (Cannot overwrite a value)

# comment

Comments start with # and run to the end of the line

From the docs, not run by us

Only the escapes \b \t \n \f \r \" \\ \uXXXX \UXXXXXXXX exist in TOML 1.0. Any other backslash sequence in a basic string is an error, which is why "C:\Users" fails.

greeting = "Tab\there"

Basic string: double quotes, escapes are processed

From the docs, not run by us
path = 'C:\Users\me'

Literal string: single quotes, backslashes kept as typed; best for Windows paths and regexes

long = """ Roses \ are red"""

Multi-line basic string; the first newline is dropped and a line-ending backslash joins lines, giving Roses are red

raw = ''' first line second line'''

Multi-line literal string: no escapes at all, newlines kept, the first newline dropped

From the docs, not run by us
e = "caf\u00E9"

Unicode escape with four hex digits (\U takes eight)

From the docs, not run by us
big = 1_000_000

Underscores between digits are allowed for readability; parsed as 1000000

hex = 0xDEAD_BEEF oct = 0o755 bin = 0b1101

Hex, octal and binary integers with prefixes; 0o755 gives 493

n = 0755

Leading zeros on a decimal integer are an error; write 0o755 if you meant octal

exp = 5e+22 pi = 3.14

Floats need digits on both sides of the point; .7 and 7. are invalid

x = inf y = nan

Special floats are lowercase only; Inf fails with Invalid value

enabled = true

Booleans are lowercase true and false

From the docs, not run by us
odt = 1979-05-27T07:32:00Z

Offset date-time; tomllib returns an aware datetime in UTC

odt2 = 1979-05-27 07:32:00-07:00

A space may replace the T, as RFC 3339 allows

ldt = 1979-05-27T07:32:00 ld = 1979-05-27 lt = 07:32:00.999999

Local date-time, local date and local time: no offset, so they become a naive datetime, a date and a time

mixed = [1, "two", 3.0]

Arrays may mix value types in TOML 1.0 and may span lines with a trailing comma

[server] host = "0.0.0.0"

Table header: every key until the next header belongs to server

[server.tls] enabled = true

Sub-table; parent tables are created for you if they don't exist yet

[a] x = 1 [a] y = 2

Declaring a table twice is an error (Cannot declare ('a',) twice)

point = { x = 1, y = 2 }

Inline table: must sit on one line in 1.0, with no trailing comma

p = { x = 1 } [p.q]

Inline tables are closed: you can't add keys or sub-tables to them later

a.b = 1 [a]

A table made by dotted keys can't be reopened with a [a] header

[[products]] name = "Hammer"

Each [[products]] header appends a new table to the products array

[[products]] name = "Nail" [products.meta] sku = 284

A sub-table header after [[products]] attaches to the most recent element

a = [] [[a]]

You can't append to an array defined with =, even an empty one (Cannot mutate immutable namespace)

[fruit] [[fruit]]

A table and an array of tables can't share a name, in either order

From the docs, not run by us

tomllib only reads. To write TOML the docs point to the third-party Tomli-W, and to TOML Kit for edits that keep comments and layout.

with open("pyproject.toml", "rb") as f: data = tomllib.load(f)

Parse a file; load needs a file opened in binary mode, text mode raises TypeError

tomllib.loads(text)

Parse a string you already have and get a plain dict

tomllib.loads(text, parse_float=decimal.Decimal)

Read floats as Decimal to avoid binary rounding in money or version-like values

From the docs, not run by us
except tomllib.TOMLDecodeError as e:

Catch invalid TOML; the message includes line and column, e.g. (at line 1, column 6)

All of these failed on Python 3.12.8 tomllib (TOML 1.0), which is exactly what an older parser in your toolchain will do.

p = { x = 1, y = 2, }

1.1 allows newlines and a trailing comma inside inline tables

s = "\x41\e"

1.1 adds \xHH for code points up to 255 and \e for the escape character

t = 07:32

1.1 makes seconds optional in times and date-times

Worked example: Turn a list of products into TOML and check what the parser sees

Say you are writing a small deploy config: a title, a server section with a nested TLS table, and a list of products. Write it with one header per table and [[products]] for the list:

title = "Deploy"

[server]
host = "0.0.0.0"

[server.tls]
enabled = true

[[products]]
name = "Hammer"

[[products]]
name = "Nail"

[products.meta]
sku = 284

Then load it with tomllib.loads() and print the result. On Python 3.12.8 we got:

'title' => 'Deploy'
'server' => {'host': '0.0.0.0', 'tls': {'enabled': True}}
'products' => [{'name': 'Hammer'}, {'name': 'Nail', 'meta': {'sku': 284}}]

Two things are worth noticing. The [server.tls] header nested tls inside server even though it came after a separate header, because TOML builds the tree from the dotted name, not from the order or indentation. And [products.meta] went into the second product only, since a sub-table header always attaches to the latest [[products]] element. If you wanted meta on the first product, it has to come before the second [[products]] line. Indenting the block does nothing; TOML ignores indentation completely.

Common mistakes

  • Writing a Windows path in double quotes, like "C:\Users\me".

    \U starts an eight-digit Unicode escape, so the parser fails with an invalid hex error. Use single quotes ('C:\Users\me') or double every backslash.

  • Writing mode = 0755 for file permissions.

    Leading zeros are not allowed on decimal integers. Write 0o755, which tomllib reads as 493, or store the value as a string if the program expects text.

  • Opening the file in text mode before tomllib.load().

    tomllib.load wants a binary file object. Use open(path, "rb"), or read the text yourself and call tomllib.loads().

  • Splitting an inline table over several lines or leaving a trailing comma in it.

    That is only legal in TOML 1.1. Keep inline tables on one line without the final comma, or switch to a [table] header, until every parser you use supports 1.1.

  • Repeating a [section] header further down the file to add one more key.

    A table can be declared once. Move the key into the first block, or use a dotted key under a different parent.

  • Expecting a [header] placed after [[items]] to apply to the whole list.

    A plain header such as [items.meta] attaches to the latest element only. Repeat it under each [[items]] entry that needs it.

Sources

How we research: where our facts come from, how often we re-check them, and why no one can pay for a review or a verdict.

What changed in AI dev tools, once a month

Renames, shutdowns, pricing changes and new MCP setups, each with a source.

Subscribe
Reviews and commentsShow

Comments

Sign in to leave a comment.

Loading comments…