Script fails with KeyError when Optional Fields with defaults are missing from API payload #11762

Closed
opened 2025-12-29 21:49:35 +01:00 by adam · 1 comment
Owner

Originally created by @rizlas on GitHub (Oct 21, 2025).

Originally assigned to: @arthanson on GitHub.

NetBox Edition

NetBox Community

NetBox Version

v4.4.4

Python Version

3.10

Steps to Reproduce

Define a script in NetBox with fields like:

from extras.scripts import Script, BooleanVar, MultiObjectVar
from dcim.models import Device

class MyScript(Script):
    debug_mode = BooleanVar("Activate debug mode", default=False)

    def run(self, data, commit):
        debug = data["debug_mode"]  # Fails if not explicitly in payload
        return f"Debug mode: {debug}"

Run the script via:

curl -X POST \
-H "Authorization: Token TOKEN" \
-H "Content-Type: application/json" \
-H "Accept: application/json; indent=4" \
https://netbox/api/extras/scripts/MyScript/ \
--data '{
  "data": {},
  "schedule_at": "2025-10-28 10:15:00",
  "interval": 360,
  "commit": true
}'

Expected Behavior

The script should run successfully, applying default values to omitted fields: debug_mode should default to False

Observed Behavior

The script crashes with:

KeyError: 'debug_mode'
Traceback (most recent call last):
  File "/opt/netbox/netbox/extras/jobs.py", line 46, in run_script
    script.output = script.run(data, commit)
                    ^^^^^^^^^^^^^^^^^^^^^^^^
  File "<string>", line 145, in run
KeyError: 'debug_mode'

Originally created by @rizlas on GitHub (Oct 21, 2025). Originally assigned to: @arthanson on GitHub. ### NetBox Edition NetBox Community ### NetBox Version v4.4.4 ### Python Version 3.10 ### Steps to Reproduce Define a script in NetBox with fields like: from extras.scripts import Script, BooleanVar, MultiObjectVar from dcim.models import Device ```python class MyScript(Script): debug_mode = BooleanVar("Activate debug mode", default=False) def run(self, data, commit): debug = data["debug_mode"] # Fails if not explicitly in payload return f"Debug mode: {debug}" ``` Run the script via: ```bash curl -X POST \ -H "Authorization: Token TOKEN" \ -H "Content-Type: application/json" \ -H "Accept: application/json; indent=4" \ https://netbox/api/extras/scripts/MyScript/ \ --data '{ "data": {}, "schedule_at": "2025-10-28 10:15:00", "interval": 360, "commit": true }' ``` ### Expected Behavior The script should run successfully, applying default values to omitted fields: debug_mode should default to False ### Observed Behavior The script crashes with: ```python KeyError: 'debug_mode' Traceback (most recent call last): File "/opt/netbox/netbox/extras/jobs.py", line 46, in run_script script.output = script.run(data, commit) ^^^^^^^^^^^^^^^^^^^^^^^^ File "<string>", line 145, in run KeyError: 'debug_mode' ```
adam added the type: bugstatus: acceptednetboxseverity: low labels 2025-12-29 21:49:36 +01:00
adam closed this issue 2025-12-29 21:49:36 +01:00
Author
Owner

@arthanson commented on GitHub (Oct 29, 2025):

This is actually not a bug - the data is a mechanism to pass in data for the script to use for the script params - it is not the script parameters themselves. This same mechanism is used for event rules to pass data to scripts. If you want to get it this way you can use the get operator with a default: debug = data.get("debug_mode", False)

@arthanson commented on GitHub (Oct 29, 2025): This is actually not a bug - the data is a mechanism to pass in data for the script to use for the script params - it is not the script parameters themselves. This same mechanism is used for event rules to pass data to scripts. If you want to get it this way you can use the `get` operator with a default: `debug = data.get("debug_mode", False)`
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#11762