{"record":{"id":"a955ef4a25e85b1d","repo":"tatsu-lab/stanford_alpaca","slug":"unexpected-type-type-obj","errorCode":null,"errorMessage":"Unexpected type: {type(obj)}","messagePattern":"Unexpected type: (.+?)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"utils.py","lineNumber":164,"sourceCode":"\n\ndef jdump(obj, f, mode=\"w\", indent=4, default=str):\n    \"\"\"Dump a str or dictionary to a file in json format.\n\n    Args:\n        obj: An object to be written.\n        f: A string path to the location on disk.\n        mode: Mode for opening the file.\n        indent: Indent for storing json dictionaries.\n        default: A function to handle non-serializable entries; defaults to `str`.\n    \"\"\"\n    f = _make_w_io_base(f, mode)\n    if isinstance(obj, (dict, list)):\n        json.dump(obj, f, indent=indent, default=default)\n    elif isinstance(obj, str):\n        f.write(obj)\n    else:\n        raise ValueError(f\"Unexpected type: {type(obj)}\")\n    f.close()\n\n\ndef jload(f, mode=\"r\"):\n    \"\"\"Load a .json file into a dictionary.\"\"\"\n    f = _make_r_io_base(f, mode)\n    jdict = json.load(f)\n    f.close()\n    return jdict\n","sourceCodeStart":146,"sourceCodeEnd":174,"githubUrl":"https://github.com/tatsu-lab/stanford_alpaca/blob/761dc5bfbdeeffa89b8bff5d038781a4055f796a/utils.py#L146-L174","documentation":"This error is raised by jdump() in alpaca-lora's utils.py when the object passed to it is neither a dict/list (JSON-serializable structure) nor a str. The function only supports three types: dict and list are serialized with json.dump, str is written verbatim, and anything else triggers this ValueError. It exists to fail fast on unsupported payloads rather than silently writing garbage.","triggerScenarios":"Calling generate_instruction_following_data() (or jdump directly) with a data object that is not a dict or list — e.g. a generator, tuple, numpy array, or a datasets.Dataset object. Also happens when refactoring produces data as an iterable that is never materialized into a list before dumping.","commonSituations":"Converting the generate instruction-following data script to stream from HuggingFace datasets (dataset.map returns a Dataset, not a list); returning a generator expression from generate_prompt_instruction_tuples; tuples returned instead of lists after code changes; numpy types leaking into the top-level object.","solutions":["Ensure the value passed to jdump is a plain list or dict, e.g. convert with list(...) before dumping","If the object is a datasets.Dataset or iterable of dicts, materialize it: data = [ex for ex in dataset] or list(dataset)","If it's a string you intended to write, pass the str itself, not a wrapped object","If you genuinely need other container types (e.g. tuple), extend jdump's isinstance check — e.g. add tuple and convert to list before json.dump"],"exampleFix":"# before\njdump(dataset.map(format_fn), 'data.json')  # Dataset object -> ValueError\n\n# after\nformatted = [format_fn(ex) for ex in dataset]\njdump(formatted, 'data.json')","handlingStrategy":"type-guard","validationCode":"import json\n\ndef is_jdump_safe(obj):\n    return isinstance(obj, (dict, list, str))\n\nif not is_jdump_safe(data):\n    data = list(data)  # or raise/convert appropriately\njdump(data, 'out.json')","typeGuard":"def is_jdump_payload(obj) -> bool:\n    \"\"\"jdump only accepts dict, list, or str.\"\"\"\n    return isinstance(obj, (dict, list, str))","tryCatchPattern":"try:\n    jdump(data, out_path)\nexcept ValueError as e:\n    if 'Unexpected type' in str(e):\n        data = list(data)  # materialize iterables/generators\n        jdump(data, out_path)\n    else:\n        raise","preventionTips":["Always materialize generators/iterables with list() before passing to jdump","Keep helper functions returning plain list/dict, not tuples or Dataset objects","Add an isinstance assert before dumping: assert isinstance(data, (dict, list, str))"],"tags":["python","json","type-validation","alpaca-lora"],"backgroundTag":"unsupported-argument-type","analyzedSha":"761dc5bfbdeeffa89b8bff5d038781a4055f796a","analyzedAt":"2026-08-28T13:51:04.098Z","schemaVersion":2},"datasetVersion":"2026-08-28T16:17:29.566Z"}