Registry / data / nbformat

nbformat

JSON →
library5.11.1pypypi✓ verified 24d ago

nbformat is the reference implementation of the Jupyter Notebook format, providing Python APIs for programmatically creating, reading, modifying, and validating Jupyter Notebook files (.ipynb). It is currently at version 5.10.4 and follows the Jupyter project's release cadence, with major versions aligning with significant changes to the notebook format specification.

pip install nbformat
INSTALL
IMPORT
SIG · NBFORMAT
N
nbformat
datapythonv5.11.1
Install
3.2s avg
Import
350ms
Disk
22MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v5.11.1 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.364s · 23.7MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 3.2s · import 0.336s · 24MB
22MB installed
● package 22MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

nbformat
import nbformat
new_notebook
from nbformat.v4 import new_notebook
from nbformat.current import new_notebook
`nbformat.current` is deprecated since before nbformat 3.0. Always import from specific version modules like `nbformat.v4` for composing new notebooks.

This quickstart demonstrates how to create a new Jupyter Notebook programmatically, add markdown and code cells, save it to a `.ipynb` file, and then read the content of an existing notebook. It uses the `nbformat.v4` module for creating notebook components, ensuring compatibility with the current Jupyter Notebook format specification version 4.

import nbformat from nbformat.v4 import new_notebook, new_markdown_cell, new_code_cell import os # 1. Create a new notebook nb = new_notebook() # Add a markdown cell nb.cells.append(new_markdown_cell("## My First Notebook (nbformat)")) # Add a code cell code_cell_content = "print('Hello from nbformat!')\nx = 1 + 2\nprint(f'The sum is: {x}')" nb.cells.append(new_code_cell(code_cell_content)) # Define output path notebook_filename = 'example_notebook.ipynb' # 2. Write the notebook to a file with open(notebook_filename, 'w', encoding='utf-8') as f: nbformat.write(nb, f) print(f"Notebook '{notebook_filename}' created successfully.") # 3. Read an existing notebook read_nb = None if os.path.exists(notebook_filename): with open(notebook_filename, 'r', encoding='utf-8') as f: # Specify as_version=4 to ensure it's read as the V4 format read_nb = nbformat.read(f, as_version=4) print(f"\nNotebook '{notebook_filename}' read successfully.") print(f"Notebook format version: {read_nb.nbformat}.{read_nb.nbformat_minor}") for i, cell in enumerate(read_nb.cells): print(f"\n--- Cell {i+1} ({cell.cell_type}) ---") print(cell.source) else: print(f"Error: '{notebook_filename}' not found for reading.")
Debug
Known issues
breakingnbformat 5.0.0 dropped support for Python 2.x. It now requires Python 3.5+ (currently >= 3.8). If you are on an older Python 3 version, check the specific `nbformat` 5.x patch release for minimum Python requirements.
fix
Upgrade your Python environment to Python 3.8 or newer.
affects: >=5.0.0
deprecatedThe `nbformat.current` module is deprecated. It was intended for backward compatibility but can lead to confusion regarding notebook versions.
fix
Directly import from specific version modules like `nbformat.v4` (e.g., `from nbformat.v4 import new_notebook`) for programmatic notebook creation and manipulation, and use `nbformat.read()` or `nbformat.write()` for file I/O.
affects: All versions >=3.0 (deprecated before 3.0)
gotchaThe `validate()` function in nbformat 5.5.0 and later no longer attempts to fix notebook errors during validation. It will now raise a `ValidationError` for invalid notebooks rather than silently modifying them.
fix
Ensure your notebooks strictly conform to the expected schema *before* calling `validate()`. If you need to upgrade older notebooks, use `nbformat.convert(nb_node, to_version=4)` explicitly, or rely on `nbformat.read(..., as_version=4)` which performs conversion if needed.
affects: >=5.5.0
gotchaWhen reading or writing notebooks, `nbformat.NO_CONVERT` can be passed to the `as_version` or `version` parameters to prevent any version conversion. If you omit `version` during writing, the notebook's own version will be used without conversion.
fix
Be explicit about desired notebook format versions. If you want to ensure a specific output format, always set `version=4` (or another target version) in `nbformat.write()`. Similarly, when reading, use `as_version=4` if you want the returned object to be converted to that version.
affects: All versions
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'nbformat'
The `nbformat` package is not installed in the currently active Python environment, or the environment where it is installed is not the one being used by the application (e.g., VS Code, Jupyter).
fix
Ensure `nbformat` is installed in the correct environment by running: `pip install nbformat` or `conda install nbformat`. If using an IDE like VS Code, verify the selected Python interpreter matches the environment where `nbformat` was installed.
ValueError: Mime type rendering requires nbformat>=4.2.0 but it is not installed
This error typically occurs when a library (e.g., Plotly) attempts to render rich output in a Jupyter Notebook, but the installed version of `nbformat` is older than the required minimum version (e.g., 4.2.0) or is not correctly detected.
fix
Upgrade `nbformat` to the latest version: `pip install --upgrade nbformat`. After upgrading, it is often necessary to restart the kernel or the entire Jupyter/VS Code session.
MissingIDFieldWarning: Code cell is missing an id field, this will become a hard error in future nbformat versions.
This warning indicates that one or more cells in a Jupyter notebook are missing a unique 'id' field, which became mandatory in `nbformat` 4.5. This often happens with older notebooks, or when cells are copied and pasted, leading to an incomplete notebook structure.
fix
Use the `nbformat.validator.normalize()` function to add missing ID fields and normalize the notebook structure. For example: `import nbformat; from nbformat import validator; with open('problematic.ipynb', 'r') as f: nb_corrupted = nbformat.read(f, as_version=4); nb_fixed = validator.normalize(nb_corrupted); with open('fixed.ipynb', 'w') as f_out: nbformat.write(nb_fixed, f_out)`. Newer versions of nbformat (>=5.1.4) provide this `normalize` method.
nbformat.ValidationError: ('Notebook failed to convert.',) or NBFormatError
A `ValidationError` (or `NBFormatError` in older versions) indicates that the notebook file does not conform to the expected Jupyter Notebook format specification, meaning its JSON structure is invalid. This can be due to file corruption, manual edits that introduce errors, or attempting to read/write a notebook with an incompatible `nbformat` version or schema.
fix
Ensure the notebook file is well-formed JSON. If the issue is due to a version mismatch, explicitly specify the `as_version` parameter when reading (`nbformat.read(fp, as_version=4)`), or ensure the notebook's internal `nbformat` and `nbformat_minor` metadata fields correctly reflect its structure. For deep corruption, manual inspection and correction of the `.ipynb` file as plain text might be necessary, or attempting to convert it with a compatible `nbconvert` version.
Upgrade
Version history
5.11.1latest on PyPI · released Aug 17, 2026
Audit
Dependencies
jsonschemarequiredUsed for validating notebook structure against the schema.
fastjsonschemaoptionalOptional faster JSON schema validator, enabled via environment variable NBFORMAT_VALIDATOR.
Agent activity
16 hits · last 30 days
node
14
Resources
nbformat — pip install nbformat · libregistry