{"record":{"id":"8fe9fba751760d7c","repo":"datawhalechina/hello-agents","slug":"format-8fe9fb","errorCode":null,"errorMessage":"不支持的反序列化格式: {format}","messagePattern":"不支持的反序列化格式: (.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"warning","filePath":"Co-creation-projects/YYHDBL-HelloCodeAgentCli/utils/serialization.py","lineNumber":42,"sourceCode":"        raise ValueError(f\"不支持的序列化格式: {format}\")\n\ndef deserialize_object(data: Union[str, bytes], format: str = \"json\") -> Any:\n    \"\"\"\n    反序列化对象\n    \n    Args:\n        data: 序列化的数据\n        format: 序列化格式\n        \n    Returns:\n        反序列化后的对象\n    \"\"\"\n    if format == \"json\":\n        return json.loads(data)\n    elif format == \"pickle\":\n        return pickle.loads(data)\n    else:\n        raise ValueError(f\"不支持的反序列化格式: {format}\")\n\ndef save_to_file(obj: Any, filepath: Union[str, Path], format: str = \"json\") -> None:\n    \"\"\"保存对象到文件\"\"\"\n    filepath = Path(filepath)\n    data = serialize_object(obj, format)\n    \n    mode = \"w\" if format == \"json\" else \"wb\"\n    with open(filepath, mode) as f:\n        f.write(data)\n\ndef load_from_file(filepath: Union[str, Path], format: str = \"json\") -> Any:\n    \"\"\"从文件加载对象\"\"\"\n    filepath = Path(filepath)\n    mode = \"r\" if format == \"json\" else \"rb\"\n    \n    with open(filepath, mode) as f:\n        data = f.read()\n    ","sourceCodeStart":24,"sourceCodeEnd":60,"githubUrl":"https://github.com/datawhalechina/hello-agents/blob/606a07d341a47be773fab7f4b71177f53f96b2c3/Co-creation-projects/YYHDBL-HelloCodeAgentCli/utils/serialization.py#L24-L60","documentation":"ValueError from utils.serialization.deserialize_object when the format argument is neither 'json' nor 'pickle' — the mirror of the serialize-side check. It fires before any bytes are parsed, so it never corrupts or partially consumes data; load_from_file inherits it through its format parameter.","triggerScenarios":"Calling deserialize_object(data, format='xml'); loading a file saved as json but read with format='pickle' or vice versa would instead raise a parse error — the format error only fires for unknown format strings; case/format drift between save and load calls.","commonSituations":"Round-trip code where the save call used 'json' and load used a variable that evaluated to something else; user-supplied format strings from CLI flags without validation; trailing whitespace in the format value.","solutions":["Pass exactly 'json' or 'pickle', matching the format used at save time.","Validate/normalize the format string at the CLI/config boundary: format in {'json','pickle'} or fail early with a clear message.","For load_from_file, store the format alongside the data (e.g. file extension) instead of inferring it."],"exampleFix":"# before\nobj = load_from_file('data.json', format=os.environ['FMT'])  # FMT='JSON' -> ValueError\n\n# after\nfmt = os.environ['FMT'].lower().strip()\nassert fmt in {'json', 'pickle'}, f'bad format: {fmt}'\nobj = load_from_file('data.json', format=fmt)","handlingStrategy":"type-guard","validationCode":"def normalize_format(fmt: str) -> str:\n    fmt = fmt.strip().lower()\n    if fmt not in {'json', 'pickle'}:\n        raise ValueError(f\"format must be 'json' or 'pickle', got {fmt!r}\")\n    return fmt\n\nobj = deserialize_object(data, normalize_format(fmt))","typeGuard":"def is_supported_format(fmt: str) -> bool:\n    return isinstance(fmt, str) and fmt.strip().lower() in {'json', 'pickle'}","tryCatchPattern":"try:\n    obj = deserialize_object(data, fmt)\nexcept ValueError as e:\n    if '不支持的反序列化格式' in str(e):\n        obj = deserialize_object(data, 'json')  # deliberate fallback\n    else:\n        raise","preventionTips":["Derive the load format from the same constant/extension used at save time.","Never pickle.loads untrusted data — prefer json for anything external.","Validate format strings once at the entry point, not deep in call stacks."],"tags":["serialization","validation","utils"],"backgroundTag":null,"analyzedSha":"606a07d341a47be773fab7f4b71177f53f96b2c3","analyzedAt":"2026-08-14T22:57:27.446Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}