15

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

4 Answers 4

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.

3
  • 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. Dec 27, 2021 at 4: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 at 18:52
  • @Samantra This is not true. You can copy and paste all the cells in your notebook.
    – Elj
    Apr 16 at 1: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.

1
  • 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 3, 2021 at 5:41
9

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.

2
  • 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, 2022 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, 2022 at 7: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

1
  • 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, 2021 at 5:46

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.