Skip to content

[Bug] OpenAI-compatible /v1/chat/completions ignores "stream": false and returns an SSE stream #6570

Closed

Description

@mtbitcoin

▎ Note: this is an inference/serving issue with the OpenAI-compatible /v1/chat/completions endpoint, not a training run — so the notebook/trainer questions are N/A. Added a "Serving command" line since the template doesn't have one.

  1. Did you update? pip install --upgrade unsloth unsloth_zoo →
  2. Colab or Kaggle or local / cloud → Local — self-hosted OpenAI-compatible server at http://localhost:8888 (<local box or cloud VM?>)
  3. Number GPUs used, use nvidia-smi → 1
  4. Which notebook? Please link! → N/A
  5. Which Unsloth version, TRL version, transformers version, PyTorch version? → unsloth v0.1.47-beta
  6. Which trainer? SFTTrainer, GRPOTrainer etc → N/A — no trainer; bug is in the OpenAI-compatible /v1/chat/completions serving endpoint.

Serving command / method (added — essential for this bug): <exact command or Docker image+tag used to serve unsloth/gemma-4-12b-it-GGUF on :8888, e.g. the unsloth serve / llama.cpp / vLLM invocation>

Minimal repro: a NON-streaming request still comes back as an SSE stream.

No Hugging Face token needed (this is a serving call). Redact any local server key.

import requests

BASE_URL = "http://localhost:8888/v1" # unsloth.ai-served endpoint
API_KEY = "REDACTED" # local server key if required — do NOT paste a HF token

payload = {
"model": "unsloth/gemma-4-12b-it-GGUF",
"messages": [{"role": "user", "content": "ping"}],
"max_tokens": 1,
"stream": False, # <-- explicitly non-streaming
}

resp = requests.post(
f"{BASE_URL}/chat/completions",
headers={"Authorization": f"Bearer {API_KEY}", "Content-Type": "application/json"},
json=payload,
)

print("Content-Type:", resp.headers.get("content-type"))
print("Body:\n", resp.text)
print(resp.json()) # <-- raises JSONDecodeError: body is an SSE stream, not a JSON object

Summary

With "stream": false, the OpenAI-compatible endpoint still returns a Server-Sent Events stream (data: {…} chunks of object: "chat.completion.chunk", ending in data: [DONE]) instead of a single chat.completion JSON object.
Non-streaming clients/SDKs that call resp.json() fail, breaking OpenAI API compatibility.

Actual behavior

data: {"id":"chatcmpl-15d67ad1b5a5","object":"chat.completion.chunk","created":1782132847,"model":"unsloth/gemma-4-12b-it-GGUF","choices":[{"index":0,"delta":{"role":"assistant"}}]}

data: {"id":"chatcmpl-15d67ad1b5a5","object":"chat.completion.chunk","created":1782132847,"model":"unsloth/gemma-4-12b-it-GGUF","choices":[{"index":0,"delta":{"content":"**"}}]}

data: {"type": "tool_status", "content": ""}

data: {"id":"chatcmpl-15d67ad1b5a5","object":"chat.completion.chunk","created":1782132847,"model":"unsloth/gemma-4-12b-it-GGUF","choices":[{"index":0,"delta":{},"finish_reason":"length"}]}

data: [DONE]

Content-Type appears to be text/event-stream (<confirm with curl -i>).

Expected behavior

With "stream": false, a single JSON object with Content-Type: application/json and object: "chat.completion", per the OpenAI Chat Completions spec (stream defaults to false):

{"id":"chatcmpl-...","object":"chat.completion","model":"unsloth/gemma-4-12b-it-GGUF",
"choices":[{"index":0,"message":{"role":"assistant","content":"..."},"finish_reason":"length"}],
"usage":{"prompt_tokens":0,"completion_tokens":1,"total_tokens":0}}

Impact

Breaks every non-streaming OpenAI-compatible integration (SDKs with stream=False, gateways, health checks). Streaming clients are unaffected, which masks the issue during normal chat use.

Additional note (possibly related)

A non-standard event is emitted that isn't part of the OpenAI streaming schema (no id/object/choices): data: {"type": "tool_status", "content": ""}. Strict parsers may reject it even in streaming mode.

Activity

  1. oobabooga commented on Jun 23, 2026

    @oobabooga
    Member

    I can't reproduce the issue, see this example showing that stream: false gets respected:

    #!/usr/bin/env python3
    # Quick check for issue #6570: "stream: false returns an SSE stream".
    #
    # On current main I couldn't reproduce it -- with stream=false the server
    # replies with Content-Type: application/json and a single JSON object,
    # exactly per the OpenAI spec. This little script shows that end to end.
    #
    # Point BASE_URL at your server, set API_KEY if you use one, and run it:
    #     python3 repro_6570.py
    # stdlib only, no pip install needed.
    
    import json
    import urllib.request
    
    BASE_URL = "http://127.0.0.1:8888"          # <- your server
    API_KEY = ""                                # <- your key, if any
    MODEL = "unsloth/gemma-4-12b-it-GGUF"       # <- the model you loaded
    
    req = urllib.request.Request(
        f"{BASE_URL}/v1/chat/completions",
        data=json.dumps({
            "model": MODEL,
            "messages": [{"role": "user", "content": "Say hello in one word."}],
            "stream": False,
            "max_tokens": 20,
        }).encode(),
        headers={
            "Content-Type": "application/json",
            "Authorization": f"Bearer {API_KEY}",
        },
    )
    
    resp = urllib.request.urlopen(req, timeout=120)
    ctype = resp.headers.get("Content-Type", "")
    body = resp.read().decode()
    
    print(f"Content-Type: {ctype}")
    print(f"Body starts with: {body[:60]!r}\n")
    
    if "text/event-stream" in ctype or body.lstrip().startswith("data:"):
        print("Got an SSE stream -- issue reproduced. :(")
    else:
        data = json.loads(body)  # parses fine == it's a single JSON object
        print("Got a single JSON object, no streaming. Working as expected! :)")
        print("Assistant said:", data["choices"][0]["message"]["content"])

    Running it here against gemma-4-12b:

    Content-Type: application/json
    Body starts with: '{"id":"chatcmpl-a676713f099d","object":"chat.completion",...'
    
    Got a single JSON object, no streaming. Working as expected! :)
    

    This looks like it was already fixed (#5047 / #5524), and you're on v0.1.47-beta -- could you update to the latest and check again? If it still happens, run the script and let me know what Content-Type you get.

  2. mtbitcoin commented on Jun 23, 2026

    @mtbitcoin
    Author

    After digging around a little bit (and testing different runtime settings) this is what i observed. Passing in the model parameter like the below breaks the streaming :

    unsloth studio run --model 'unsloth/gemma-4-12b-it-GGUF:UD-Q5_K_XL' (breaks the stream=false settings)
    unsloth studio -H 0.0.0.0 -p 8888 (then switching the model to the above model from inside unsloth studio, works)

    In the past (before the recent updates) i was able to pass in the run --model 'xxx' without issues. But with with the recent releases this breaks. Its not really a big issue but the health check features from certain application like LiteLLM etc which expect a non streaming chat shows it the as an unhealthy instance

    I am currently able to get around this by just running without the model parameter and then switching it manually from unsloth studio gui.

  3. self-assigned this
    on Jun 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions