Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why would opening a jupyter notebook get "'outputs' were unexpected" error?

I am trying download the code on this post.

download seems to be successful though, when i open the notebook, an error shows up.

enter image description here

Notebook validation failed

An invalid notebook may not function properly. The validation error was:

Notebook validation failed: Additional properties are not allowed ('execution_count', 'outputs' were unexpected):

why would opening a jupyter notebook get "'outputs' were unexpected" error?

is there a method i could use to examine which part of the notebook is invalid?


2 Answers

A Jupyter notebook is actually stored as a JSON file, so you can just open it in a text editor. Each cell is a JSON object that gets decoded to a Python dict.

The reason you get this error is that cell_type is markdown, which signifies that this is a Markdown cell.

Since Markdown cells are rendered instead of executed, it doesn't make sense for them to have the outputs and execution_count keys, which apply only to code cells.

You could probably write a simple script to examine your Jupyter notebook; something like this:

import json

valid_keys = ['cell_type', 'metadata', 'source']
filename = ...  # specify filename here

with open(filename) as f:
    data = json.load(f)

for index, cell in enumerate(data['cells'], 1):
    if cell['cell_type'] == 'markdown':
        extra_keys = [key for key in cell.keys() if key not in valid_keys]
        if extra_keys:    
            print(f'Cell {index} has the following keys which are invalid for a markdown cell: {extra_keys}')
like image 100
gmds Avatar answered Sep 04 '25 05:09

gmds


In addition to the previous reply, if the error verifiably stems from markdown cells with empty outputs properties, you can use a regex to strip those. It won't affect any other legit outputs.
Here's a python script to do just that:

"""
This script fixes the auto-corrupted jupyter notebooks, specifically removes empty outputs properties (which are not legal for markdown cells).
"""

import re

with open('MyNotebook.ipynb', 'r') as file:
    # Read original notebook file to string
    jupyter = file.read()

    # Run a regex based search and replace, wipe all empty outputs properties.
    outputs_tag_removed = re.sub(',\n\s+\"outputs\":\ \[\]', '', jupyter)

    # Overwrite original notebook file
    print(outputs_tag_removed, file=open('MyNotebook.ipynb', 'w'))

There seem to be similar issues, related to markdown cells with illegal execution count and code cells without the mandatory execution count property. See discussion on GitHub.

like image 27
m5c Avatar answered Sep 04 '25 06:09

m5c



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!