Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add JEP for adding $schema to notebook format #97

Merged
merged 6 commits into from
Jul 11, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
117 changes: 117 additions & 0 deletions 97-add-schema/add-schema-to-notebook-format.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
---
title: Add `$schema` to notebook format
authors: Jason Grout (@jasongrout), Angus Hollans (@agoose77),
Nicholas Bollweg (@bollwyvl), Filip Schouwenaars (@filipsch)
issue-number: xxx
pr-number: 97
date-started: 2023-03-01
---

# Summary

We propose to add a new top-level field, `$schema` to the notebook JSON, as such updating the notebook JSON schema. This new field deprecates `nbformat` and `nbformat_minor`.

# Motivation

Today, `nbformat` and `nbformat_minor` specify the JSON schema a notebook should adhere to (for example [4.5](https://github.com/jupyter/nbformat/blob/main/nbformat/v4/nbformat.v4.5.schema.json)). Since this approach was adopted in the Jupyter ecosystem, the JSON schema standard has evolved and Jupyter's approach is no longer in line with it. Other than following standards being the right thing to do, bringing the notebook format back in line with the current JSON Schema spec opens it up to the rich tooling that exists around JSON schema validation today.

# Guide-level explanation

The introduction of a new `$schema` top-level property takes precedence over the existing `"nbformat"` and `"nbformat_minor"` properties that specify the notebook format.

Example of notebook in 4.5 format:

```
{
"nbformat": 4
"nbformat_minor": 5
"metadata": { ... }
"cells": [ ... ]
}
```

Example of notebook in 4.6 format:

```
{
"$schema": "https://jupyter.org/schema/notebook/4.6/notebook-4.6.schema.json"
"nbformat": 4
"nbformat_minor": 6
"metadata": { ... }
"cells": [ ... ]
}
```

It is REQUIRED that the top-level `$schema` be a canonical URI that refers to the Jupyter Notebook formats, i.e. `$schema` cannot be an arbitrary URI to a valid notebook schema, but instead must be useable as a token that uniquely identifies that schema, e.g.:

Valid `$schema` URI:

```json
{
"$schema": "https://jupyter.org/schema/notebook/4.6/notebook-4.6.schema.json"
...
}
```

Invalid `$schema` URI:

```json
{
"$schema": "https://jupyter.org/schema/../schema/notebook/4.6/notebook-4.6.schema.json"
...
}
```

# Reference-level explanation

JSON Schema changes:

```
{
"$schema": "http://json-schema.org/draft-04/schema#",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From our notebook format workshop meeting this morning, we'll need to bump to at least JSON Schema draft 2019 to have the deprecated keyword, and maybe we should bump this to the 2020 draft (i.e., the latest draft)?

Copy link
Contributor

@agoose77 agoose77 Mar 14, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We will also need to introduce support for this new keyword in the existing schemas, i.e. backport the addition to our existing schemas. This should be acceptable, as these schemas are not declared immutable, and it would be a permissive change.

We should also define how to ensure that the nbformat versions align with the document schema during the deprecation period. One solution is to ensure that they're constants in the schema. Thereafter, we could move to a single-version number (major) for each schema revision, as they compatibility is enforced by $schema itself.

I don't know of a reason not to bump to 2020 draft, besides the risk of existing tooling not supporting newer drafts.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would say for the newer version it is acceptable to bump the schema to the latest draft after ensuring there is no backward incompatibility between the current version based on draft 04 and the new draft - as far as I know some breaking changes are induced when upgrading from draft 04 to draft 06.

From a quick look it seems ok

Additionally if we are to bump the JSON Schema draft, I would switch all enum with single value to the new const as used for the nbformat version numbers (see mainly cell_type and output_type).

"description": "Jupyter Notebook v4.5 JSON schema.",
"type": "object",
"additionalProperties": false,
"required": ["$schema", "nbformat", "nbformat_minor", metadata", "cells"], // CHANGED
"properties": {
"$schema": {
"description": "JSON schema the notebook should adhere to",
"type": "string"
}
"metadata": { <no changes > }
"nbformat_minor": {
"description": "Notebook format (minor number). Incremented for backward compatible changes to the notebook format.",
"type": "integer",
"minimum": 6,
"deprecated": true // CHANGED
},
"nbformat": {
"description": "Notebook format (major number). Incremented between backwards incompatible changes to the notebook format.",
"type": "integer",
"minimum": 4,
"maximum": 4
"deprecated": true // CHANGED
},
"cells": [ <no changes> ]
},
"definitions": { <no changes> }
}
```

# Rationale and alternatives

Not doing this will leave the Jupyter notebook format in a non-standard state.

# Prior art

[JSON Schema](https://json-schema.org/) is a widely adopted declarative language that annotates and validates documents, so it's the obvious candidate to adhere to.

# Unresolved questions

- Code to upgrade from and downgrade to 4.5 still needs to be written.
Some exploration has been done by Nick Bollweg in [this gist](https://gist.github.com/bollwyvl/a6e1ae13125f01ff04edf121e30a462a).
- We want to deprecate `nbformat` and `nbformat_minor` in favor of `$schema` but we also need to ensure old clients can still work with notebooks in this new schema, so `nbformat` and `nbformat_minor` are still required. What's the path here? Major version update?

# Future possibilities

This work paves the way for [``Add `extraSchemas` to notebook format`` JEP](https://hackmd.io/9QZ8YibfQHm9l1B6JPSQsg?both), which will be submitted as a separate JEP soon.