Skip to content

sgl.function can't use the specified chat template #239

Closed

Description

@TideDra

I launch a finetuned Mixtral model which uses the chatml template:

python -m sglang.launch_server --model-path Nous-Hermes-2-Mixtral-8x7B-DPO --tp 4 --chat-template chatml

But the sgl.function seems not to apply this template:

@function
def test(s,question):
    s += user(question)
    s += assistant(gen("answer"))

s = test.run(question="Is 1+1 = 2?")
print(s.text())

which prints:

USER:Is 1+1 = 2?
ASSISTANT: Yes, in basic arithmetic, 1 + 1 is equal to

Using a wrong template absolutely harms model's performance.

Activity

  1. ArrichM commented on Feb 27, 2024

    @ArrichM

    The chat template specified via the --chat-template option is only applied to the OpenAI compatible server, as described in the parameter help text.
    If you want to use a certain chat template when using the the /generate endpoint, you can just set it as the chat_template for your endpoint:

    import sglang as sgl
    from sglang.lang.chat_template import get_chat_template
    
    endpoint = sgl.RuntimeEndpoint(
        base_url="http://localhost:8000",
    )
    endpoint.chat_template = get_chat_template("chatml")
    
    @sgl.function
    def test(s, question):
        s += sgl.user(question)
        s += sgl.assistant(sgl.gen("answer"))
    
    s = test.run(question="Is 1+1 = 2?", backend=endpoint)
    print(s.text())

    Which prints:

    <|im_start|>user
    Is 1+1 = 2?
    <|im_end|>
    <|im_start|>assistant
    Yes, 1+1 is equal to 2 in mathematics. In
    <|im_end|>
    
    
  2. TideDra commented on Feb 28, 2024

    @TideDra
    ContributorAuthor

    The chat template specified via the --chat-template option is only applied to the OpenAI compatible server, as described in the parameter help text. If you want to use a certain chat template when using the the /generate endpoint, you can just set it as the chat_template for your endpoint:

    import sglang as sgl
    from sglang.lang.chat_template import get_chat_template
    
    endpoint = sgl.RuntimeEndpoint(
        base_url="http://localhost:8000",
    )
    endpoint.chat_template = get_chat_template("chatml")
    
    @sgl.function
    def test(s, question):
        s += sgl.user(question)
        s += sgl.assistant(sgl.gen("answer"))
    
    s = test.run(question="Is 1+1 = 2?", backend=endpoint)
    print(s.text())

    Which prints:

    <|im_start|>user
    Is 1+1 = 2?
    <|im_end|>
    <|im_start|>assistant
    Yes, 1+1 is equal to 2 in mathematics. In
    <|im_end|>
    

    Thanks for your help!

  3. added a commit that references this issue on Apr 7, 2026
  4. added 2 commits that reference this issue on Sep 28, 2026
  5. 37 remaining items

  6. added 10 commits that reference this issue on Sep 29, 2026
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