Skip to content

Handle missing widget state metadata in WidgetsDataTypeFilter#2288

Open
hpy1968 wants to merge 2 commits into
jupyter:mainfrom
hpy1968:fix-missing-widget-state
Open

Handle missing widget state metadata in WidgetsDataTypeFilter#2288
hpy1968 wants to merge 2 commits into
jupyter:mainfrom
hpy1968:fix-missing-widget-state

Conversation

@hpy1968

@hpy1968 hpy1968 commented Jun 3, 2026

Copy link
Copy Markdown

Summary

This fixes a KeyError when notebook metadata contains the widget-state mimetype object but that object does not include a nested "state" key.

Reproducer shape

metadata = {
    "widgets": {
        "application/vnd.jupyter.widget-state+json": {
            "version_major": 2,
            "version_minor": 0,
        }
    }
}

When an output includes application/vnd.jupyter.widget-view+json, WidgetsDataTypeFilter currently indexes:

metadata["widgets"][WIDGET_STATE_MIMETYPE]["state"]

and raises:

KeyError: "state"

Fix

This change treats missing "state" the same as unavailable widget state. The filter skips the widget-view mimetype and falls back to the next available display format, such as text/plain.

Tests

Tests added:

  • widget metadata without "state" falls back to text/plain
  • widget metadata with matching state still prefers widget-view

Tested locally:

  • tests/filters/test_widgetsdatatypefilter.py tests/filters/test_datatypefilter.py: 5 passed
  • tests/filters: 54 passed, 5 skipped
  • full suite: 290 passed, 41 skipped

Related issues

Debugging / reproduction note

We reproduced this failure using Retrace, our Python record/replay debugger, to inspect the runtime state at the failing call site.

The replay showed that the notebook metadata contained the widget-state mimetype object, but that object only had version_major and version_minor; the nested "state" key was absent. That is the shape covered by the new test.

The fix and tests above do not depend on Retrace, but the replay was useful for confirming the exact runtime metadata shape behind the KeyError.

For context, we documented the replay/debugging workflow here:
https://github.com/retracesoftware/retracesoftware/tree/main/open-source-debugging/examples/001-nbconvert-1731

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

"KeyError: 'state'" when trying to convert a notebook from Google Colab

1 participant