datawhalechina/hello-agents · warning · ValueError

不支持的反序列化格式: {format}

Error message

不支持的反序列化格式: {format}

What it means

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.

Source

Thrown at Co-creation-projects/YYHDBL-HelloCodeAgentCli/utils/serialization.py:42

        raise ValueError(f"不支持的序列化格式: {format}")

def deserialize_object(data: Union[str, bytes], format: str = "json") -> Any:
    """
    反序列化对象
    
    Args:
        data: 序列化的数据
        format: 序列化格式
        
    Returns:
        反序列化后的对象
    """
    if format == "json":
        return json.loads(data)
    elif format == "pickle":
        return pickle.loads(data)
    else:
        raise ValueError(f"不支持的反序列化格式: {format}")

def save_to_file(obj: Any, filepath: Union[str, Path], format: str = "json") -> None:
    """保存对象到文件"""
    filepath = Path(filepath)
    data = serialize_object(obj, format)
    
    mode = "w" if format == "json" else "wb"
    with open(filepath, mode) as f:
        f.write(data)

def load_from_file(filepath: Union[str, Path], format: str = "json") -> Any:
    """从文件加载对象"""
    filepath = Path(filepath)
    mode = "r" if format == "json" else "rb"
    
    with open(filepath, mode) as f:
        data = f.read()
    

View on GitHub (pinned to 606a07d341)

Solutions

  1. Pass exactly 'json' or 'pickle', matching the format used at save time.
  2. Validate/normalize the format string at the CLI/config boundary: format in {'json','pickle'} or fail early with a clear message.
  3. For load_from_file, store the format alongside the data (e.g. file extension) instead of inferring it.

Example fix

# before
obj = load_from_file('data.json', format=os.environ['FMT'])  # FMT='JSON' -> ValueError

# after
fmt = os.environ['FMT'].lower().strip()
assert fmt in {'json', 'pickle'}, f'bad format: {fmt}'
obj = load_from_file('data.json', format=fmt)
Defensive patterns

Strategy: type-guard

Validate before calling

def normalize_format(fmt: str) -> str:
    fmt = fmt.strip().lower()
    if fmt not in {'json', 'pickle'}:
        raise ValueError(f"format must be 'json' or 'pickle', got {fmt!r}")
    return fmt

obj = deserialize_object(data, normalize_format(fmt))

Type guard

def is_supported_format(fmt: str) -> bool:
    return isinstance(fmt, str) and fmt.strip().lower() in {'json', 'pickle'}

Try / catch

try:
    obj = deserialize_object(data, fmt)
except ValueError as e:
    if '不支持的反序列化格式' in str(e):
        obj = deserialize_object(data, 'json')  # deliberate fallback
    else:
        raise

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of datawhalechina/hello-agents@606a07d341 (2026-08-14). Data as JSON: /api/errors/8fe9fba751760d7c. Report an issue: GitHub.