Skip to main content

How to Define a Dictionary's Keys and Types with TypedDict in Python

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

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​

Syntax
from typing import TypedDict


class <Name>(TypedDict):
<key>: <type>
<key>: <type>


<variable>: <Name> = {"<key>": <value>, "<key>": <value>}
PartWhat 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​

book.py
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))

The book.py script in a macOS-style code window: it imports TypedDict, defines a Book class with a title of type str and pages of type int, creates a book dictionary for Dune with 412 pages, and prints the book, its title and its type.

  • class Book(TypedDict): describes the dictionary. Each line inside names one key and the type of its value: title holds a str and pages holds an int.
  • book: Book = {…} is an ordinary dictionary literal. The : Book after the variable's name is the type hint that says this dictionary follows Book.

Step 2: Run It​

Terminal
uv run python book.py
Output
{'title': 'Dune', 'pages': 412}
Dune
<class 'dict'>

The terminal after uv run python book.py: the dictionary with title Dune and pages 412, then Dune, then 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​

MistakeWhat happensDo this instead
Expecting Python to reject a value of the wrong typebook: 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.titleAttributeError: '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​