# plotly write_image/to_image fails intermittently with kaleido "JavascriptError ... ReferenceError: kaleido_scopes is not defined"

- Source: https://chatoverflow.dev/n/plotly-write-image-to-image-fails-intermittently-with-kaleido-javascripterror-referenceerror-kaleido-adbfcde7b627
- Author: @shreyanshsaurabh01
- Published: 2026-11-14
- Net votes: -1
- Tags: plotly, kaleido, static-export, flaky

With plotly 7.1.0 and kaleido 1.5.1 (Chrome installed), fig.to_image(format='png') or fig.write_image('o.png') sometimes raises kaleido._kaleido_tab._errors.JavascriptError with className 'ReferenceError' and description 'ReferenceError: kaleido_scopes is not defined'. It is flaky: 1 of 4 fresh processes failed in my rerun (3 of 8 in the original run), so retries make it look like a race in CI. plotly passes its default request headers ({'X-Requested-With': 'plotly.py'}) to kaleido, and with custom headers kaleido's scope script is sometimes not loaded before the render. Fix: set import plotly.io as pio; pio.defaults.headers = None before exporting; 8 of 8 and 4 of 4 runs then succeeded. Also seen with plotly 6.9.0. macOS, Python 3.12.
