Fix unicode decode
Diagnose and fix UnicodeDecodeError, UnicodeEncodeError, mojibake, and corrupted Chinese text in Codex coding tasks. Use when Python or another tool cannot decode a file or subprocess stream; Chinese text displays as replacement characters or strings such as "䏿–‡" or "涓枃"; file, terminal, CSV, JSON, HTTP, Git, or Windows encoding behavior is unclear; or a repository needs a safe migration to UTF-8.From its SKILL.md
npx -y skills add hachiwar/codex-skills --skill fix-unicode-decodeAssembled from the repository path, not quoted from the project. Check it against their README if it does not work.
2 things to look at
- no licenseNo license file was found in the repository. Code published without one is not open source by default, so using it at work is a question for whoever answers licensing questions where you are.
- 2 stars2 stars. Stars are a popularity signal and not a quality one, but at this level it is likely that nobody has read this closely except its author, and you would be relying on your own review.
SKILL.md
4.0 KB, 815 tokens by cl100k_base, as published. Nobody here has run it
Fix Unicode Decode
Treat encoding as a byte-to-text contract. Identify the byte source and actual encoding before changing code or files; never make an error disappear with errors="ignore" or an unverified bulk rewrite.
Workflow
-
Preserve the original bytes. Check Git status before editing and make a backup only when Git cannot restore the file.
-
Capture the full exception, including codec, byte offset, operation, platform, and the code that opened or decoded the data.
-
Identify the boundary that converts bytes to text:
open,Path.read_text, CSV/JSON parser, subprocess, HTTP response, database driver, or terminal. -
Inspect bytes without first decoding them. Check BOM and run:
python scripts/diagnose_encoding.py path/to/file -
Select an encoding using provenance first, then BOM/specification, then strict decoding and content plausibility. A successful decode alone is not proof;
latin-1andgb18030accept many byte sequences. -
Reproduce with strict decoding and a focused test containing Chinese, ASCII, punctuation, and an emoji where the format supports it.
-
Fix the narrowest owning boundary. Specify
encoding=at file boundaries, configure subprocess or terminal encoding, or correct the producer when its declared charset is wrong. -
Verify semantic text, byte round-trip behavior when required, tests, and the diff. Do not normalize unrelated files.
Decision Rules
- Prefer UTF-8 for new repository text and explicit
encoding="utf-8"in Python. - Use
utf-8-sigonly when consuming or intentionally producing a UTF-8 BOM, commonly for Excel interoperability. - Use
gb18030for legacy Simplified Chinese data only when provenance or byte evidence supports it. Prefer it overgbkfor broader compatibility. - Use
utf-16when a BOM is present. Without a BOM, establish endianness from the format or producer before choosingutf-16-leorutf-16-be. - Keep
bytesas bytes until the correct boundary. Decode once and encode once. - Use
errors="replace"only for deliberate lossy display or telemetry and report the data loss. Never use it to migrate source data. - Do not attempt to repair text containing
U+FFFDunless the original bytes are available; the replacement may already have destroyed information.
Common Fixes
from pathlib import Path
text = Path(path).read_text(encoding="utf-8")
Path(path).write_text(text, encoding="utf-8", newline="\n")
result = subprocess.run(
command,
capture_output=True,
text=True,
encoding="utf-8", # Match the child process, not a guess.
errors="strict",
check=True,
)
For CSV, JSON, HTTP, Windows console, Git, and mojibake repair patterns, read references/encoding-playbook.md. Load only the relevant section.
Verification
- Confirm the exact failing input now decodes under
errors="strict". - Assert representative Chinese text such as
中文,编码测试。rather than only checking that no exception occurs. - Re-run the smallest relevant test suite, then inspect
git diff --word-diffor a byte-level diff for unexpected churn. - State the detected source encoding, chosen target encoding, evidence, changed boundary, and whether any decoding was lossy.
Diagnostic Script
Run scripts/diagnose_encoding.py on one or more files. Add --json for machine-readable output or --encodings to test known producer-specific codecs. The script is read-only, uses strict decoding, and ranks candidates heuristically; treat its ranking as evidence, not certainty.
What ships with it: 3 files
10.9 KB alongside SKILL.md, 1 of them executable
agents/
- openai.yaml256 B
references/
- encoding-playbook.md5.1 KB
scripts/
- diagnose_encoding.pyruns5.5 KB