How to Define a Dictionary's Keys and Types with TypedDict in Python
A type hint of dict says that something is a dictionary, but not which keys it holds. TypedDict lets you list the keys, and the type of each value, so anyone reading the code, and your editor, knows what the dictionary should contain. This guide uses it on Python 3.15.2.
Syntax
from typing import TypedDict
class <Name>(TypedDict):
<key>: <type>
<key>: <type>
<variable>: <Name> = {"<key>": <value>, "<key>": <value>}
| Part | What it means |
|---|---|
<Name> | The name of the kind of dictionary you're describing, written like a class name |
<key>: <type> | One line per key: the key's name, then the type its value should have |
<variable>: <Name> | A type hint saying that this variable holds such a dictionary |
{"<key>": <value>, …} | An ordinary dictionary with those keys |
The typing documentation calls TypedDict a "Special construct to add type hints to a dictionary" and adds: "At runtime “TypedDict instances” are simply dicts."
The Example
A Book has two keys: title, which holds text, and pages, which holds a whole number. The script makes one book, then prints the whole dictionary, its title, and its type.
Prerequisites
- uv and Python 3. This guide uses uv 0.13.10 with Python 3.15.2, in a project made with
uv init.
Step 1: Write the Script
from typing import TypedDict
class Book(TypedDict):
title: str
pages: int
book: Book = {"title": "Dune", "pages": 412}
print(book)
print(book["title"])
print(type(book))

class Book(TypedDict):describes the dictionary. Each line inside names one key and the type of its value:titleholds astrandpagesholds anint.book: Book = {…}is an ordinary dictionary literal. The: Bookafter the variable's name is the type hint that says this dictionary followsBook.
Step 2: Run It
uv run python book.py
{'title': 'Dune', 'pages': 412}
Dune
<class 'dict'>

The book prints like any dictionary, and book["title"] reads a value the usual way. The last line is the point to notice: type(book) is <class 'dict'>, a plain dictionary. TypedDict doesn't change the dictionary at all. It's information for the people and tools that read your code. The documentation says the expected keys and types are "not checked at runtime but is only enforced by type checkers": programs, and many editors, that read your type hints and point out values that don't match. The command works unchanged in PowerShell.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
| Expecting Python to reject a value of the wrong type | book: Book = {"title": "Dune", "pages": "many"} ran without an error and printed {'title': 'Dune', 'pages': 'many'} | Check values yourself where they come in, or let a type checker or your editor flag the mismatch before you run the code |
Reading a key with a dot, as on an object: book.title | AttributeError: 'dict' object has no attribute 'title' | Use square brackets: book["title"] |
FAQ
Can I make a book by calling Book?
Yes. Book(title="Dune", pages=412) printed {'title': 'Dune', 'pages': 412}, the same dictionary as the literal in Step 1. The documentation's own example shows the same thing, Point2D(x=1, y=2, label='first') == dict(x=1, y=2, label='first').
Do I have to fill in every key?
The type hint says so. The documentation puts it as "By default, all keys must be present in a TypedDict", and it shows how to mark a key as optional with NotRequired. Python itself still doesn't stop you from leaving one out when the code runs: book: Book = {"title": "Dune"} printed {'title': 'Dune'} without an error, for the same reason it accepted "many" as a number of pages. A type checker reports the missing key.
Conclusion
class Book(TypedDict) listed the two keys a book needs and the type of each value, and book: Book attached that description to an ordinary dictionary. When the script ran, type(book) was still <class 'dict'>: the description is for readers and tools, not something Python enforces.
Additional Resources
- typing.TypedDict — the class, its rules and examples.
- PEP 589 – TypedDict — the proposal that added it, with the reasons behind it.
