14

This is not a duplicate of Notebook Validation Failed.

I have a Jupyter ntb which was working well until recently. Whatever I do, I am getting those kind of errors which are changing (I'm attaching a few examples) and it does not matter what I do with the ntb (I tried restarting kernel, restarting Jupyter). Also, it is happening ONLY in this ntb, not in others even if run at the same time from the same session. I tried to search but could not find anything. What I get in the Jupyter is: enter image description here

There are many of those, similar but different:

The save operation succeeded, but the notebook does not appear to be valid. The validation error was:
Notebook validation failed: Non-unique cell id 'geological-poker' detected. Corrected to 'front-hampshire'.:
"<UNKNOWN>"

or others:

Notebook validation failed: Non-unique cell id 'medieval-nebraska' detected. Corrected to 'stock-eating'.:
"<UNKNOWN>"
Notebook validation failed: Non-unique cell id 'intense-award' detected. Corrected to 'blocked-garage'.:
"<UNKNOWN>"

And what I get in the terminal is:

Notebook JSON is invalid: Non-unique cell id 'medieval-nebraska' detected. Corrected to 'convinced-vacation'.

or

Notebook JSON is invalid: Non-unique cell id 'medieval-nebraska' detected. Corrected to 'described-commerce'.
Notebook JSON is invalid: Non-unique cell id 'meaning-victoria' detected. Corrected to 'occasional-numbers'.
Notebook JSON is invalid: Non-unique cell id 'eastern-buyer' detected. Corrected to 'english-benchmark'.

Any idea what's going on and how to fix it?


Update: It got fixed somehow for a while but then it started doing the same again I still do not get what it was and how it got fixed and ruined again... One fix is to make a copy of the ntb and then discard the old one.


Update 9.10.21:

It looks that this is no more a problem for python v3.8.11. I have not managed to reproduce the issue since updating all packages so probably the easiest fix now is to update. I have:

jupyter core     : 4.7.1
jupyter-notebook : 6.4.3
ipython          : 7.26.0
ipykernel        : 6.2.0
My Work
  • 2,143
  • 2
  • 19
  • 47

4 Answers4

36

I also happened to find another solution that worked:

First select all the cells in the Jupyter Notebook, then press the "cut/scissors" button (don't panic, at this point your Notebook will be empty), and finally press the "paste" button.

This is from https://github.com/jupyter/notebook/issues/6001#issuecomment-959828688.

Elj
  • 557
  • 3
  • 13
  • 11
    You might have a reason to panic if the notebook is too large and the paste action takes too much time. It is always a good idea to make a duplicate of the notebook before applying the solution above. – aishik roy chaudhury Dec 27 '21 at 04:30
  • That's usually enough to re-create _only one or several_ cells you copied from different notebook (if you remember exactly which ones). – Samantra Apr 14 '23 at 18:52
  • @Samantra This is not true. You can copy and paste all the cells in your notebook. – Elj Apr 16 '23 at 01:36
10

I have the same issue and after reading about it, it seems the cause is copy/pasting cells from other notebooks that were created in other sessions. I haven't found a way to fix it, but at least you can prevent it from happening by copying the content of the cells and not the cells themselves.

julrods
  • 101
  • 4
  • 2
    Hi @julrods, thanks for your answer. I already forgot I asked it. I found another solution which is to make a copy of the ntb and then discard the old one, I'll update my question. I'm not entirely sure it happens exclusively in what you describe -- it happened to me when I was working in an ntb after some time and I did not copy cells at that time, it came out of blue. But would you have links and sources to what you found? – My Work May 03 '21 at 05:41
8

Simple Answer

STEP 1: Cut all the cells in the Jupyter-notebook using scissor option as shown. enter image description here

STEP 2: Save the notebook now, and paste back the cells cut in the previous step using paste option as shown. enter image description here

STEP 3: Save again and your bug should be fixed now! Hurry, Happy coding.

DataFramed
  • 1,491
  • 16
  • 21
  • Hi @DataFramed and thanks for your answer but it looks the same as the accepted answer just with graphics. Is there a difference that I missed? – My Work Mar 28 '22 at 14:54
  • @MyWork thanks for pointing that, you are correct. I happen to read the same github thread and reproduced the error and took screenshot of the steps. Hope it helps others. – DataFramed Mar 29 '22 at 07:55
3

Code to resave a notebook with the cell name issue resolved:

import nbformat as nbf
from glob import glob

import uuid
def get_cell_id(id_length=8):
    return uuid.uuid4().hex[:id_length]

# -- SETUP
nb_name = 'my_notebook'

# -- MAIN
# grab notebook
notebooks = list(filter(lambda x: nb_name in x, glob("./*.ipynb", recursive=True)))

# iterate over notebooks
for ipath in sorted(notebooks):
    # load notebook
    ntbk = nbf.read(ipath, nbf.NO_CONVERT)
    
    cell_ids = []
    for cell in ntbk.cells:
        cell_ids.append(cell['id'])

    # reset cell ids if there are duplicates
    if not len(cell_ids) == len(set(cell_ids)): 
        for cell in ntbk.cells:
            cell['id'] = get_cell_id()

        nbf.write(ntbk, ipath)

Source: https://github.com/jupyter/notebook/issues/6001#issuecomment-856303981

Guzman Ojero
  • 2,812
  • 1
  • 20
  • 20
Mark_Anderson
  • 1,229
  • 1
  • 12
  • 34
  • This solution worked for me too. I created a new folder. I put the script in a notebook in the folder, and put the problematic notebook in the same folder. I then changed 'my_notebook' (that was assigned to nb_name) to the filename of my notebook, with the extension. After running the script, the problematic notebook could save with no issues. – Elj Nov 26 '21 at 05:46