Skip to main content

A tool that validates a NotebookNode object against the Jupyter Notebook schema using nbformat, ensuring structural correctness.

Project description

Swarmauri Logo

PyPI - Downloads Hits PyPI - Python Version PyPI - License PyPI - swarmauri_tool_jupytervalidatenotebook


Swarmauri Tool · Jupyter Validate Notebook

A Swarmauri tool that validates Jupyter notebooks (NotebookNode objects) against the official nbformat JSON schema. Use it to gate notebook submissions, enforce structural best practices, or wire schema checks into automated notebook pipelines.

  • Accepts in-memory NotebookNode instances and produces structured success/error payloads.
  • Surfaces detailed schema violations coming from nbformat/jsonschema.
  • Integrates with Swarmauri agents via the standard tool registration workflow.

Requirements

  • Python 3.10 – 3.13.
  • nbformat (installed automatically) with access to the notebook JSON schema.
  • Dependencies (jsonschema, swarmauri_base, swarmauri_standard, pydantic, typing_extensions).

Installation

Pick the installer that matches your workflow; each command resolves transitive packages.

pip

pip install swarmauri_tool_jupytervalidatenotebook

Poetry

poetry add swarmauri_tool_jupytervalidatenotebook

uv

# Add to the current project and update uv.lock
uv add swarmauri_tool_jupytervalidatenotebook

# or install into the active environment without touching pyproject.toml
uv pip install swarmauri_tool_jupytervalidatenotebook

Tip: When using uv inside this repo, run uv commands from the repository root so it can locate the shared pyproject.toml.

Quick Start

Load a notebook with nbformat, then pass the resulting NotebookNode to the tool. The response dictionary contains string values for valid ("True" or "False") and a human-readable report.

import nbformat
from swarmauri_tool_jupytervalidatenotebook import JupyterValidateNotebookTool

notebook = nbformat.read("analysis.ipynb", as_version=4)
validate = JupyterValidateNotebookTool()

result = validate(notebook)

if result["valid"] == "True":
    print("Notebook passes schema validation")
else:
    raise ValueError(result["report"])

Usage Scenarios

Batch Validate an Entire Notebook Directory

import nbformat
from pathlib import Path
from swarmauri_tool_jupytervalidatenotebook import JupyterValidateNotebookTool

validator = JupyterValidateNotebookTool()
notebook_dir = Path("notebooks")

for path in notebook_dir.glob("**/*.ipynb"):
    nb = nbformat.read(path, as_version=4)
    result = validator(nb)
    status = "PASS" if result["valid"] == "True" else "FAIL"
    print(f"[{status}] {path}: {result['report']}")

Drop this snippet into CI to stop merges when any notebook violates the schema.

Fail a Build When Validation Fails

import sys
import nbformat
from swarmauri_tool_jupytervalidatenotebook import JupyterValidateNotebookTool

validator = JupyterValidateNotebookTool()
notebook = nbformat.read(sys.argv[1], as_version=4)
result = validator(notebook)

print(result["report"])
if result["valid"] != "True":
    sys.exit(1)

Wire the script into a pre-commit hook or build step (python validate.py path/to/notebook.ipynb).

Combine With Other Swarmauri Tools

from swarmauri_tool_jupyterstartkernel import JupyterStartKernelTool
from swarmauri_tool_jupytervalidatenotebook import JupyterValidateNotebookTool

start_kernel = JupyterStartKernelTool()
validate_notebook = JupyterValidateNotebookTool()

launch = start_kernel()
print(f"Launched kernel: {launch['kernel_id']}")

# After generating a notebook programmatically, load and validate it
import nbformat
nb = nbformat.read("generated.ipynb", as_version=4)
validation = validate_notebook(nb)
print(validation)

Use the validation step after automated notebook generation/execution to ensure outputs remain schema-compliant.

Troubleshooting

  • Invalid nbformat version – The tool enforces nbformat version 4. Upgrade the notebook (nbformat.convert) or save it with a modern Jupyter client.
  • Validation error – Inspect the report field for the jsonschema path causing the failure. Missing metadata or malformed cells are common culprits.
  • Unexpected error – Log the exception and confirm the input is an nbformat NotebookNode, not a raw dict or path string.

License

swarmauri_tool_jupytervalidatenotebook is released under the Apache 2.0 License. See LICENSE for full text.

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

File details

Details for the file swarmauri_tool_jupytervalidatenotebook-0.9.3.dev5.tar.gz.

File metadata

  • Download URL: swarmauri_tool_jupytervalidatenotebook-0.9.3.dev5.tar.gz
  • Upload date:
  • Size: 8.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.4 {"installer":{"name":"uv","version":"0.10.4","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for swarmauri_tool_jupytervalidatenotebook-0.9.3.dev5.tar.gz
Algorithm Hash digest
SHA256 3467c03e92e6344a336304546e943fdc4bc88757f4e00a7427776cdb26aecd11
MD5 2811781f60edbbe85eae3396b890c5cb
BLAKE2b-256 99d1bc725340c0ad7fe7fd3f10b34dcc3b1ed05638462ea64865cae0cd915037

See more details on using hashes here.

File details

Details for the file swarmauri_tool_jupytervalidatenotebook-0.9.3.dev5-py3-none-any.whl.

File metadata

  • Download URL: swarmauri_tool_jupytervalidatenotebook-0.9.3.dev5-py3-none-any.whl
  • Upload date:
  • Size: 9.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.4 {"installer":{"name":"uv","version":"0.10.4","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for swarmauri_tool_jupytervalidatenotebook-0.9.3.dev5-py3-none-any.whl
Algorithm Hash digest
SHA256 5b01f9825e67454e4eb09e2a797f0fb214c97eb47d9cf7e21a01b9bee33d3647
MD5 55b3bf614faa2b9a8e23b68c5ced0d59
BLAKE2b-256 63487d0936865cefc51e43e88472a05b06192dc1edf653583898ecd8bedf0cda

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page