AI Code Migration Tools: Move from Tomli to tomllib (2026)

AI code migration tools can replace a Python import in seconds. Check whether Python 3.10 users still need Tomli before you remove the dependency.

Give an AI code migration tool your oldest supported Python version before asking it to replace Tomli. Python 3.11 includes the tomllib parser, but a package that still supports Python 3.10 needs a parser available on that version too.

The Short Answer

What the project supportsPatch to review
Python 3.11 and newerImport tomllib. Check the project's configuration files with the built-in parser before dropping Tomli.
Python 3.10 and newerUse tomllib on newer interpreters and keep Tomli as a conditional dependency for 3.10.
Writing changes back to a TOML fileKeep the writer. Python's tomllib reads files.

What should the migration tool change?

Suppose a command-line tool reads a timeout from a settings file. Its package metadata promises Python 3.10 support. A coding assistant finds the import at the top of the settings reader and proposes replacing it with import tomllib. You run the command on Python 3.12. It works.

Now try importing that reader on 3.10. The program stops before it opens a file because the interpreter can't find the new module. A reviewer could catch the mistake without running anything if the patch showed the minimum Python version beside the new import.

Give the assistant that context up front. Try this prompt when working with a coding agent: “Our package supports Python 3.10 and newer. Find where we read TOML and show the dependency declaration alongside the import you would change. Preserve the supported versions.”

The project promises Python 3.10 support

How do you keep Python 3.10 working?

Use the same local name for both parsers. The settings reader can then keep one call to load the file, whichever interpreter starts the program.

import sys

if sys.version_info >= (3, 11):

    import tomllib

else:

    import tomli as tomllib

The import is half the patch. If the project declares runtime dependencies in the dependencies list under [project] in pyproject.toml, add the version condition to its existing Tomli requirement. For a project that previously declared plain tomli, the list entry becomes:

"tomli; python_version < '3.11'"

Keep any existing Tomli version bounds. Run the installation test in a clean environment because a developer who already has Tomli installed can run the fallback even when the package forgot to declare it.

Both import paths feed the same settings reader

What should you test before removing a dependency?

Start with a file small enough to read in the review. Save this as settings.toml:

[server]

timeout = 30

Put the import block above in a file called check_settings.py, then append these lines:

with open("settings.toml", "rb") as settings:

    timeout = tomllib.load(settings)["server"]["timeout"]

assert type(timeout) is int and timeout == 30

print(timeout)

Install the changed package in fresh environments for Python 3.10 and 3.11. Run python check_settings.py from the directory with both files in each environment. Both should print 30. Installing the project exercises its dependency declaration. Installing Tomli by hand would hide a missing requirement on 3.10.

The tiny file tells you the example works. Follow it with a configuration file your application actually accepts, especially if the project uses syntax introduced after TOML 1.0, the version supported by Python 3.11's built-in parser. For more on choosing an assertion, see how to test an exact expected value.

The expected timeout is the integer 30 on both versions

In Unstoppable Code you can keep the migration patch in its own Git worktree, a separate checkout for the edit. Create the Python environments separately. Changing the checkout alone doesn't change which interpreter runs the test.

Frequently Asked Questions

Can I remove Tomli if the package requires Python 3.11 or newer? Yes, if the built-in parser handles the files you support and nothing else in your project needs Tomli. Test that before removing the requirement.

Why open settings.toml with rb? The load function expects a binary file. If you already have TOML text in a Python string, pass it to loads instead.

Can tomllib save the updated timeout? No. Keep your existing TOML writer if the application edits settings on disk.

Sources

Cookie preferences

We use optional cookies to improve the site