Skip to content

fix: Fix JSON serialization error of ApiModel in print_obj#1040

Merged
stevemessick merged 1 commit into
Kaggle:mainfrom
PythonicVarun:fix/obj-print
Jun 1, 2026
Merged

fix: Fix JSON serialization error of ApiModel in print_obj#1040
stevemessick merged 1 commit into
Kaggle:mainfrom
PythonicVarun:fix/obj-print

Conversation

@PythonicVarun

Copy link
Copy Markdown
Contributor

When running model-related CLI commands such as:

kaggle models get kienngx/nemotron-nano-30b-trained

The command fails with the following traceback:

Traceback (most recent call last):
  File "<frozen runpy>", line 198, in _run_module_as_main
  File "<frozen runpy>", line 88, in _run_code
  File "C:\PythonicVarun\tmp\.venv\Scripts\kaggle.exe\__main__.py", line 10, in <module>
  File "C:\PythonicVarun\tmp\.venv\Lib\site-packages\kaggle\cli.py", line 75, in main
    out = args.func(**command_args)
          ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\PythonicVarun\tmp\.venv\Lib\site-packages\kaggle\api\kaggle_api_extended.py", line 4800, in model_get_cli
    self.print_obj(model)
  File "C:\PythonicVarun\tmp\.venv\Lib\site-packages\kaggle\api\kaggle_api_extended.py", line 5877, in print_obj
    pretty = json.dumps(obj, indent=indent)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Development\Python\Python312\Lib\json\__init__.py", line 238, in dumps
    **kw).encode(obj)
          ^^^^^^^^^^^
  File "C:\Development\Python\Python312\Lib\json\encoder.py", line 202, in encode
    chunks = list(chunks)
             ^^^^^^^^^^^^
  File "C:\Development\Python\Python312\Lib\json\encoder.py", line 439, in _iterencode
    o = _default(o)
        ^^^^^^^^^^^
  File "C:\Development\Python\Python312\Lib\json\encoder.py", line 180, in default
    raise TypeError(f'Object of type {o.__class__.__name__} '
TypeError: Object of type ApiModel is not JSON serializable

This error occurs because the kaggle_api_extended.py helper method in kaggle_api_extended.py attempts to pass raw generated models (such as ApiModel and ApiModelInstance) directly to json.dumps().

def print_obj(self, obj, indent=2):
pretty = json.dumps(obj, indent=indent)
print(pretty)

Solution

Updated the kaggle_api_extended.py helper method to call .to_dict() on the object before serialization:

def print_obj(self, obj, indent=2):
    pretty = json.dumps(obj.to_dict(), indent=indent)
    print(pretty)

Affected Commands

This resolves crashes in the following CLI commands (when running without the -p/--path option):

Both ApiModel and ApiModelInstance implement .to_dict(), allowing the standard json.dumps library to successfully serialize the resulting dict format.

@stevemessick

Copy link
Copy Markdown
Contributor

/gcbrun

@stevemessick
stevemessick self-requested a review June 1, 2026 16:09

@stevemessick stevemessick left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@stevemessick
stevemessick merged commit 5c0672e into Kaggle:main Jun 1, 2026
11 checks passed
stevemessick added a commit that referenced this pull request Jun 1, 2026
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.

2 participants