Skip to content

fix(server): don't 500 on partial UTF-8 in /extras/detokenize - #2452

Open
sarankumar1325 wants to merge 2 commits into
abetlen:mainfrom
sarankumar1325:fix/server-detokenize-partial-utf8
Open

sarankumar1325 wants to merge 2 commits into
abetlen:mainfrom
sarankumar1325:fix/server-detokenize-partial-utf8

Conversation

@sarankumar1325

Copy link
Copy Markdown

What changed

POST /extras/detokenize decoded 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 raised UnicodeDecodeError and 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" in Llama completions); replace is 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-only ggml-vocab-llama-spm.gguf model 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 passed
  • make lint (ruff check and format check): clean

I'll add the CHANGELOG.md entry in a follow-up commit once the PR number is known.

…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

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant