Visitar URL original
[Feature] Return token ids under /chat/completion endpoint (Prompt + Response) · Issue #18378 · sgl-project/sglang · GitHub
Skip to content

[Feature] Return token ids under /chat/completion endpoint (Prompt + Response) #18378

Closed

Description

@guapisolo

Checklist

Motivation

Context: For RL, we need token consistency between training and inference. Previously people used /generate to get the token ids + logprobs. However, in nowadays agentic workflows, many users prefer using /chat/completion endpoint.

Gap:

  1. Under /chat/completion, sglang currently only returns response token text + logprobs, but does not return the exact token ids. (Retokenize the text may not reproduce the exact token id).
  2. Sglang does not return any prompt token meta info.

Request:

  1. Please add support for returning token ids for both prompt and response in /chat/completion.
  2. Currently there is no need for returning prompt's token logprobs.

Thanks!

Related resources

No response

Activity

  1. guapisolo commented on Feb 6, 2026

    @guapisolo
    CollaboratorAuthor

    btw, I have a draft implementation here for this feature included in sglang.patch. It includes some additional changes that are not related to this request.

    https://github.com/radixark/miles/pull/502/changes#diff-c6217d91cb59fab575767e4171efdc3c43071fefcc800321289fba7a659108f0R77

  2. changed the title [-][Feature] Token-level Metadata for /chat/completion in sglang (Prompt + Response)[/-] [+][Feature] Return token ids under /chat/completion endpoint (Prompt + Response)[/+] on Feb 6, 2026
  3. CatherineSue commented on Feb 9, 2026

    @CatherineSue
    Collaborator

    I think we could support this with a default option to turn it off. As it is not OpenAI standard.

  4. guapisolo commented on Feb 10, 2026

    @guapisolo
    CollaboratorAuthor

    Good idea.

    I think we could support this with a default option to turn it off. As it is not OpenAI standard.

  5. CSWYF3634076 commented on Mar 9, 2026

    @CSWYF3634076
    Contributor

    May I ask if this feature has been implemented? I look forward to supporting it. If you don't have time, I would be more than happy to develop it @CatherineSue @guapisolo

  6. Superskyyy commented on Mar 18, 2026

    @Superskyyy

    Really this feature from Slime side, please merge it

  7. cafeii commented on Apr 5, 2026

    @cafeii

    is this feature added in the newest sglang?

  8. MLKoz2 commented on Apr 7, 2026

    @MLKoz2

    @cafeii rather not, and the PR is freezing for last two months

  9. billxbf commented on Apr 8, 2026

    @billxbf

    Same, can this feautre be merged?

  10. added a commit that references this issue on Apr 12, 2026
    a3472d1
  11. ianliuy commented on Apr 12, 2026

    @ianliuy
    Contributor

    I've opened PR #22610 (#22610) which implements this feature.

    What it does:
    Adds
    eturn_token_ids: bool = False to both /v1/chat/completions and /v1/completions. When enabled, the response includes prompt and completion token IDs in the sglext field.

    Usage:
    \\python
    response = client.chat.completions.create(
    model='...',
    messages=[{'role': 'user', 'content': 'Hello!'}],
    extra_body={'return_token_ids': True},
    )
    sglext = response.model_extra.get('sglext', {})
    print(sglext['prompt_token_ids']) # [1, 2, 3, ...]
    print(sglext['completion_token_ids']) # [[4, 5, 6, ...]] (one list per choice)
    \\

    Key design decisions:

    • Off by default (opt-in) — no impact on existing users
    • Returns IDs from the runtime directly (not re-tokenized from text) exact token consistency
    • Non-OAI-standard fields go into \sglext\ per existing convention
    • Works for both streaming and non-streaming
    • Also covers /v1/completions\ to match the existing symmetry between both endpoints
    • Backend unchanged \ okenizer_manager.py\ already tracks \output_ids\ on every request

    Files changed: \protocol.py, \serving_chat.py, \serving_completions.py, and 7 new unit tests in \ est_protocol.py.

  12. github-actions commented on Jun 12, 2026

    @github-actions
    Contributor

    This issue has been automatically closed due to inactivity. Please feel free to reopen it if needed.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions