{"record":{"id":"92f8ac025b1f7acd","repo":"datawhalechina/hello-agents","slug":"format","errorCode":null,"errorMessage":"不支持的序列化格式: {format}","messagePattern":"不支持的序列化格式: (.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"warning","filePath":"Co-creation-projects/YYHDBL-HelloCodeAgentCli/utils/serialization.py","lineNumber":24,"sourceCode":"from pathlib import Path\n\ndef serialize_object(obj: Any, format: str = \"json\") -> Union[str, bytes]:\n    \"\"\"\n    序列化对象\n    \n    Args:\n        obj: 要序列化的对象\n        format: 序列化格式 (\"json\" 或 \"pickle\")\n        \n    Returns:\n        序列化后的数据\n    \"\"\"\n    if format == \"json\":\n        return json.dumps(obj, ensure_ascii=False, indent=2)\n    elif format == \"pickle\":\n        return pickle.dumps(obj)\n    else:\n        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}\")","sourceCodeStart":6,"sourceCodeEnd":42,"githubUrl":"https://github.com/datawhalechina/hello-agents/blob/606a07d341a47be773fab7f4b71177f53f96b2c3/Co-creation-projects/YYHDBL-HelloCodeAgentCli/utils/serialization.py#L6-L42","documentation":"ValueError from utils.serialization.serialize_object when the format argument is neither 'json' nor 'pickle'. Only those two formats are compiled in; anything else (including case variants like 'JSON') is rejected before any data is touched.","triggerScenarios":"Calling serialize_object(obj, format='yaml'/'msgpack'/'JSON'); passing the format positionally in the wrong slot so another value lands in format; save_to_file propagates the same error via its format parameter.","commonSituations":"Config-driven serialization where users set format: yaml; case-sensitivity surprises; refactors that rename the parameter's expected values.","solutions":["Use exactly 'json' or 'pickle' (lowercase).","Normalize input: format=str(format).lower().strip() before calling.","For other formats, serialize with the appropriate library directly instead of this helper."],"exampleFix":"# before\ndata = serialize_object(obj, format='JSON')  # ValueError\n\n# after\ndata = serialize_object(obj, format='json')\n# or normalize: serialize_object(obj, format='JSON'.lower())","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\ndata = serialize_object(obj, 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    data = serialize_object(obj, fmt)\nexcept ValueError as e:\n    if '不支持的序列化格式' in str(e):\n        data = serialize_object(obj, 'json')  # explicit fallback choice\n    else:\n        raise","preventionTips":["Normalize and whitelist format strings at the config/CLI boundary.","Use an Enum for formats instead of free strings.","Remember pickle output is bytes — pair mode='wb' correctly on save."],"tags":["serialization","validation","utils"],"backgroundTag":null,"analyzedSha":"606a07d341a47be773fab7f4b71177f53f96b2c3","analyzedAt":"2026-08-14T22:57:27.446Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}