python encoding_error ai_generated true

UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte

ID: python/starlette-json-response-encoding

Also available as: JSON · Markdown · 中文
80%Fix Rate
82%Confidence
0Evidence
2025-03-12First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
3.x active

Root Cause

尝试将非 UTF-8 编码的数据作为 JSON 响应返回。

generic

中文

尝试将非 UTF-8 编码的数据作为 JSON 响应返回。

Workarounds

  1. 95% success
    data = data.encode('utf-8').decode('utf-8')
    return JSONResponse({'data': data})

Dead Ends

Common approaches that don't work:

  1. 80% fail

    会导致数据损坏,且不兼容 JSON

  2. 90% fail

    会丢失数据,且客户端无法解析