[serve][llm] Add tokenize/detokenize to SGLang example engine#61446
Open
eureka928 wants to merge 1 commit intoray-project:masterfrom
Open
[serve][llm] Add tokenize/detokenize to SGLang example engine#61446eureka928 wants to merge 1 commit intoray-project:masterfrom
eureka928 wants to merge 1 commit intoray-project:masterfrom
Conversation
Contributor
There was a problem hiding this comment.
Code Review
This pull request adds tokenize and detokenize endpoints to the SGLang example engine, along with corresponding tests. The implementation is mostly correct, but it misses a check for whether the tokenizer is available, which could lead to a runtime error if SGLang is initialized with --skip-tokenizer-init. I've added comments to address this by adding checks for the tokenizer's existence in both new methods.
e1eef59 to
23df4e0
Compare
eicherseiji
reviewed
Mar 3, 2026
Contributor
eicherseiji
left a comment
There was a problem hiding this comment.
Running release test: https://buildkite.com/ray-project/release/builds/82637#
…engine Implement tokenize() and detokenize() methods on SGLangServer using the tokenizer from self.engine.tokenizer_manager.tokenizer. Tokenize supports both completion and chat request formats. Add round-trip tests and update the readme. Closes remaining items from ray-project#61113. Signed-off-by: leonace924 <meobius123@gmail.com>
23df4e0 to
2f3345c
Compare
Contributor
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.
Description
Add
tokenize()anddetokenize()endpoints to the SGLang example engine, completing the remaining items from #61113 (follow-up to #61159 which added embeddings).Changes:
tokenize()— usesself.engine.tokenizer_manager.tokenizer.encode(), supports bothTokenizeCompletionRequest(prompt) andTokenizeChatRequest(messages). Returns tokens, count, and max_model_len.detokenize()— usestokenizer.decode(request.tokens), returns the decoded prompt string.readme.mdto reflect the new supported endpoints.test_sglang_tokenizeandtest_sglang_detokenizetests with round-trip verification.Related issues
Closes remaining checklist items from #61113.
Follow-up to #61159 (embeddings).
Additional information
httpxfor direct HTTP calls since the OpenAI Python client doesn't expose tokenize/detokenize endpoints.max_model_lenis read fromself.engine.server_args.context_length._render_chat_prompt()for chat tokenize requests, consistent with the chat completions flow.