How to Make a Key Optional in a Python TypedDict with NotRequired
A TypedDict expects every key it lists, so a dictionary that leaves one out gets an error from your type checker. Some keys really are optional, such as a book's subtitle. NotRequired marks such a key, and this guide uses it on Python 3.15.2, checked with mypy 2.4.1.
Syntax
from typing import NotRequired, TypedDict
class <Name>(TypedDict):
<key>: <type>
<optional_key>: NotRequired[<type>]
| Part | What it means |
|---|---|
<Name> | The name of the kind of dictionary you're describing |
<key>: <type> | A key that every such dictionary must have |
<optional_key>: NotRequired[<type>] | A key that a dictionary may leave out. When it's there, its value must be a <type> |
The typing documentation describes NotRequired as a "Special typing construct to mark a TypedDict key as potentially missing." It was "Added in version 3.11".
The Example
A Book always has a title, and only some books have a subtitle. The script makes two books, one without a subtitle and one with, and then asks Book which keys it requires and which it doesn't.
Prerequisites
- uv and Python 3.11 or newer.
NotRequiredcame to thetypingmodule in Python 3.11. This guide uses uv 0.13.10 with Python 3.15.2, in a project made withuv init.
Step 1: Write the Script
from typing import NotRequired, TypedDict
class Book(TypedDict):
title: str
subtitle: NotRequired[str]
dune: Book = {"title": "Dune"}
emma: Book = {"title": "Emma", "subtitle": "A Novel"}
print(dune)
print(emma)
print(Book.__required_keys__)
print(Book.__optional_keys__)

subtitle: NotRequired[str]says that a book may leave outsubtitle, and that when it has one, the subtitle is text.dunehas no subtitle, andemmahas one. Both are meant to be valid books.__required_keys__and__optional_keys__are two attributes that everyTypedDictclass has. They list its required and its optional keys.
Step 2: Run It
uv run python library.py
{'title': 'Dune'}
{'title': 'Emma', 'subtitle': 'A Novel'}
frozenset({'title'})
frozenset({'subtitle'})

Both books printed. The last two lines show how Book sees its keys: title is required, and subtitle is optional. A frozenset is a set that can't be changed. The documentation confirms that "keys marked with NotRequired will always appear in __optional_keys__".
Step 3: Check It with a Type Checker
Python doesn't check type hints when it runs your code, so a missing key never stops the script. A type checker, a program that reads your type hints and reports values that don't match them, is what enforces TypedDict. Check the file with mypy:
uvx mypy library.py
Success: no issues found in 1 source file

uvx runs a command from a Python package without adding that package to your project. uv's command reference says that such packages "are installed into an ephemeral virtual environment in the uv cache directory." mypy accepted both books: dune without a subtitle, and emma with one. Both commands in this guide work unchanged in PowerShell.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Leaving out a key that isn't marked NotRequired | In a copy of the script with subtitle: str, mypy reported library_strict.py:9: error: Missing key "subtitle" for TypedDict "Book" [typeddict-item] on the dune line | Mark the key as NotRequired[str] |
Writing subtitle: str | None to make the key optional | mypy still reported Missing key "subtitle" for TypedDict "Book" | Use NotRequired[str]. str | None allows the value None, but the key still has to be there |
Setting a NotRequired key to None | mypy reported Incompatible types (expression has type "None", TypedDict item "subtitle" has type "str") [typeddict-item] | Leave the key out, or write NotRequired[str | None] if None is a value you really want |
FAQ
Does Python itself reject a book without a required key?
No. Python doesn't check type hints when the code runs, so with subtitle: str, {"title": "Dune"} still printed {'title': 'Dune'}. Only a type checker, such as mypy or the one in your editor, reports the missing key. That's why Step 3 runs mypy.
What's the difference between NotRequired and str | None?
They answer different questions. NotRequired is about the key: may it be missing? str | None is about the value: may it be None? The documentation states the default that NotRequired changes: "By default, all keys must be present in a TypedDict."
Conclusion
subtitle: NotRequired[str] let dune leave out its subtitle while emma kept one, and mypy printed Success: no issues found in 1 source file. Book.__optional_keys__ listed subtitle as the one optional key.
Additional Resources
- typing.NotRequired — the construct and the version that added it.
- typing.TypedDict — required and optional keys, with examples.
- PEP 655 – Marking individual TypedDict items as required or potentially-missing — the proposal that added
NotRequired.
