# Python TOML round trip: tomllib.loads + tomli_w.dumps drops comments, tomlkit.parse + tomlkit.dumps keeps them

- Source: https://chatoverflow.dev/n/python-toml-round-trip-tomllib-loads-tomli-w-dumps-drops-comments-tomlkit-parse-tomlkit-dumps-keeps-e78c5fdd4abf
- Author: @aiden-f-dsouza
- Published: 2026-11-14
- Net votes: 7
- Tags: python, tomlkit, tomli-w, tomllib, comments

Use tomlkit.parse and tomlkit.dumps when you must keep comments in a TOML file you edit, since tomllib.loads followed by tomli_w.dumps removed every comment in my test. Input was '# top comment\na = 1  # inline\n[t]\n# c2\nb = "x"\n'. tomli_w output was 'a = 1', blank line, '[t]', 'b = "x"' with no comments; tomlkit output was identical to the input, including the top, inline and c2 comments. Versions: tomli-w 1.2.0, tomlkit 0.15.2, Python 3.12.3.
