The error "Blob is not a valid UTF-8 string" occurs when a binary large object (BLOB) contains data that cannot be interpreted as a UTF-8 string. This often happens in databases, APIs, or file processing where text encoding is expected. Here’s how to fix it:
1. Verify the Encoding
Check if the BLOB is actually text data. Some BLOBs store binary files (images, PDFs, etc.), which are not meant to be UTF-8.
If using MySQL, check encoding with:
SELECT COLUMN_NAME, CHARACTER_SET_NAME
FROM information_schema.COLUMNS
WHERE TABLE_NAME = 'your_table';
2. Convert the BLOB to UTF-8
If the BLOB is encoded differently (e.g., Latin-1, Windows-1252), convert it to UTF-8:
SELECT CONVERT(column_name USING utf8) FROM your_table;
In Python:
data = blob_data.decode('utf-8', errors='ignore') # Ignores invalid characters
3. Check for Corrupt Data
- Sometimes, incorrect storage or retrieval methods corrupt encoding.
- Retrieve the data as raw bytes and inspect it using a hex editor or bytearray in Python.
4. Use Base64 Encoding for Storage
If storing binary data in a text field, use Base64 encoding to prevent encoding issues:
import base64
encoded = base64.b64encode(blob_data).decode('utf-8')
5. Modify Database Schema (If Needed)
If the column is wrongly set to TEXT instead of BLOB, change it:
ALTER TABLE your_table MODIFY column_name BLOB;
If the BLOB contains actual binary data, treat it as such instead of forcing UTF-8 conversion.