Repository navigation
fix(server): don't 500 on partial UTF-8 in /extras/detokenize - #2452
Open
sarankumar1325 wants to merge 2 commits into
Open
sarankumar1325 wants to merge 2 commits into
sarankumar1325 wants to merge 2 commits into
Conversation
…as/detokenize A token list that ends inside a multi-byte UTF-8 character (for example a single byte-fallback token) made the endpoint raise UnicodeDecodeError and return HTTP 500. Decode with errors="replace" and add server tests. Signed-off-by: sarankumar1325 <120080287+sarankumar1325@users.noreply.github.com>
Signed-off-by: sarankumar1325 <120080287+sarankumar1325@users.noreply.github.com>
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.
What changed
POST /extras/detokenizedecoded the detokenized bytes with a strict.decode("utf-8").When the token list ends inside a multi-byte UTF-8 character, for example a single byte-fallback token such as
<0xE2>, the endpoint raisedUnicodeDecodeErrorand returned HTTP 500.This decodes with
errors="replace", so the response is still valid and the incomplete bytes show up as U+FFFD instead of failing the request.The rest of the library already decodes detokenized bytes leniently (
errors="ignore"inLlamacompletions);replaceis used here so a client can still see that bytes were present.Tests
Added
tests/test_server.py, which mounts the server router on a FastAPI app with a vocab-onlyggml-vocab-llama-spm.ggufmodel through dependency overrides:test_detokenize_endpoint: normal round trip.test_detokenize_endpoint_partial_utf8: the<0xE2>token returns 200 with"\ufffd"(fails with a 500 before this change).Ran locally on Linux x86_64 with a CPU build:
python -m pytest -q: 30 passedmake lint(ruff check and format check): cleanI'll add the
CHANGELOG.mdentry in a follow-up commit once the PR number is known.