Repository navigation
fix: read and write text files as UTF-8 - #192
Open
OsamaAnsar wants to merge 1 commit into
Open
OsamaAnsar wants to merge 1 commit into
OsamaAnsar wants to merge 1 commit into
Conversation
`Path.read_text` and `Path.write_text` without an explicit encoding use the locale's preferred encoding, which is cp1252 on most Windows installations (unless Python's UTF-8 mode is enabled). That breaks the call sites that handle UTF-8 data: - `document_to_markdown` raised a UnicodeDecodeError when reading a UTF-8 .md or .txt file (or a file that pandoc cannot convert) containing, e.g., curly quotes. - `raglite mcp install` raised a UnicodeDecodeError on a desktop app MCP config containing such characters, or silently decoded other characters (e.g., e-acute) into mojibake and wrote that back to the config. - The OpenAI vector store benchmark wrote documents to temporary files in the locale encoding (or raised a UnicodeEncodeError for characters that the locale encoding cannot represent) and then uploaded those bytes. Pass `encoding="utf-8"` at those call sites.
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Path.read_text()/Path.write_text()withoutencoding=use the locale's preferred encoding, which is cp1252 on typical Windows installations (unless Python's UTF-8 mode is on). Several call sites handle UTF-8 data and break there:_markdown.py:211(.md/.txt)UnicodeDecodeError, or silently wrong text_markdown.py:225(fallback when pandoc rejects the format)_cli.py:76/:103(raglite mcp install)_bench.py:268(OpenAIVectorStoreEvaluator.insert_documents)UnicodeEncodeErrorfor characters outside cp1252, otherwise non-UTF-8 bytes are uploadedReproduction (on
main, Windows 11, Python 3.12,PYTHONUTF8=0 python -X utf8=0,locale.getpreferredencoding(False) == 'cp1252')document_to_markdownon a UTF-8note.mdcontaining# Note\n\nHe said “hello” — it’s fine.\n(same for.txt, and for a file with an unsupported extension, which goes through the pandoc fallback):raglite mcp installwith an existing UTF-8 config containing another server entry with"/home/José/tool"(and, in a second run,"“x”"in its args), driven through the TyperCliRunner:With
PYTHONUTF8=1the same inputs work onmain, so this only affects non-UTF-8 default encodings.Fix
Pass
encoding="utf-8"at those five call sites. I checked each: the MCP config is a JSON file the desktop app writes as UTF-8,json.dumpsin the write path already emits ASCII (ensure_ascii=True) so the write change is only for consistency, and the benchmark temp file is created by this function and uploaded as bytes.Not changed:
_bench.py:70(TREC run file; contains ids, ranks and scores) and_markdown.py:175(splitlineson a regex match, unrelated to encoding).pypandoc.convert_filealready defaults to UTF-8.I found no existing issue or PR about this (open PR #190 only touches
_embed.py).Tests
Because the default encoding is already UTF-8 on Linux/macOS, the tests use a small fixture (
cp1252_default_text_encodingintests/conftest.py) that makesPath.read_text/Path.write_textdefault to cp1252 when no encoding is passed, so they fail on every platform without the fix:tests/test_markdown.py:.md,.txtand the pandoc-rejects-the-format fallback (the latter is skipped ifpypandocis not installed).tests/test_cli.py(new):raglite mcp installagainst a UTF-8 config with non-ASCII characters. The test installs a stub for the module thatmcp installimports to locate the config directory; with the latestfastmcpthat I resolved (4.1.1) that module no longer exists, soraglite mcp installcurrently fails there withModuleNotFoundErrorregardless of this PR. I did not touch that.tests/test_bench.py(new): a mocked OpenAI client records the bytes of the files that are uploaded; skipped if thebenchdependencies are not installed. I installed onlyir_datasets,ir_measures,python-slugifyandopenai, not the fullbenchextra (faiss, llama-index).Results:
src/restored tomain): the 5 new tests fail (3UnicodeDecodeErrorintest_markdown.py, the CLI exits with aUnicodeDecodeError,UnicodeEncodeErrorin the benchmark test); the existing PDF test passes.PYTHONUTF8=0 python -X utf8=0cp1252 locale on Windows: 5 fail without the fix, 6 pass with it.What I ran
pytest src testswithCI=1(skipsslow) andHF_HUB_OFFLINE=1: 51 passed (46 onmain, +5 new). The same 9 failures and 30 errors occur onmainand on this branch; all are tests that need to download the Qwen3/bge-m3 llama.cpp models (OfflineModeIsEnabled). 97 tests were skipped (PostgreSQL / OpenAI key not available).pre-commit runon the changed files (ruff check, ruff format, mypy, commitizen, the other hooks): passed.pre-commit run --all-filesfails onmainand on this branch alike withICN001insrc/raglite/_database.pyunder ruff 0.17.1 (the version I had installed); I did not touch that.lowest-directresolution, PostgreSQL, a real OpenAI vector store upload, a real desktop app config file (only a synthetic one).