datawhalechina/hello-agents · warning · ValueError

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

Error message

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

What it means

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.

Source

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

from pathlib import Path

def serialize_object(obj: Any, format: str = "json") -> Union[str, bytes]:
    """
    序列化对象
    
    Args:
        obj: 要序列化的对象
        format: 序列化格式 ("json" 或 "pickle")
        
    Returns:
        序列化后的数据
    """
    if format == "json":
        return json.dumps(obj, ensure_ascii=False, indent=2)
    elif format == "pickle":
        return pickle.dumps(obj)
    else:
        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}")

View on GitHub (pinned to 606a07d341)

Solutions

  1. Use exactly 'json' or 'pickle' (lowercase).
  2. Normalize input: format=str(format).lower().strip() before calling.
  3. For other formats, serialize with the appropriate library directly instead of this helper.

Example fix

# before
data = serialize_object(obj, format='JSON')  # ValueError

# after
data = serialize_object(obj, format='json')
# or normalize: serialize_object(obj, format='JSON'.lower())
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

data = serialize_object(obj, 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:
    data = serialize_object(obj, fmt)
except ValueError as e:
    if '不支持的序列化格式' in str(e):
        data = serialize_object(obj, 'json')  # explicit fallback choice
    else:
        raise

Prevention

When it happens

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

Common situations: Config-driven serialization where users set format: yaml; case-sensitivity surprises; refactors that rename the parameter's expected values.

Related errors


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