Skip to content

Studio: harden OpenAI-compatible GGUF streaming - #6950

Merged
oobabooga merged 11 commits into
unslothai:mainfrom
Apoze:studio-openai-gguf-streaming
Jul 9, 2026
Merged

oobabooga merged 11 commits into
unslothai:mainfrom
Apoze:studio-openai-gguf-streaming

Conversation

@Apoze

@Apoze Apoze commented Jul 7, 2026 •

Copy link
Copy Markdown
Contributor

Summary

This PR tightens Studio's OpenAI-compatible local GGUF chat serving path so streamed responses are easier for strict OpenAI-compatible clients to consume.

Main changes:

  • preserve reasoning_content while also adding content: "" to reasoning-only stream deltas;
  • normalize OpenAI passthrough SSE forwarding around data: payloads and [DONE];
  • close in-band stream errors with a separate data: [DONE] sentinel;
  • convert terminal non-streaming fallback responses into OpenAI-style SSE chunks when needed;
  • add a local implicit output-token cap only when clients omit both max_tokens and max_completion_tokens;
  • preserve explicit client token limits unless the operator opts into a service-level ceiling.

Motivation

Some OpenAI-compatible clients are strict about streamed chat delta shape and terminal SSE markers. Studio could forward chunks that were valid for llama.cpp but awkward for those clients, especially reasoning-only deltas and error/final chunks.

Local serving also needs a practical default when a client omits token limits. Without one, small auxiliary requests can unexpectedly become long generations. This PR keeps explicit client values intact and only applies a local default when no OpenAI token-limit field is present.

This does not disable reasoning and does not change model quality settings.

Configuration

  • UNSLOTH_OPENAI_COMPAT_IMPLICIT_MAX_TOKENS
    • default: 1024
    • used only when the request omitted both max_tokens and max_completion_tokens.
  • UNSLOTH_OPENAI_COMPAT_MAX_TOKENS_CEILING
    • disabled by default
    • optional service-level ceiling that can cap explicit client values.
  • UNSLOTH_OPENAI_COMPAT_STREAM_STALL_TIMEOUT
    • default: 30
    • caps post-first-chunk upstream silence.
  • UNSLOTH_OPENAI_COMPAT_STREAM_PREHEADER_FALLBACK_TIMEOUT
    • disabled by default
    • positive values opt into non-streaming fallback for pre-header stalls.

These are backend-only environment hooks. They can be surfaced in Studio settings later without changing the API behavior added here.

Validation

Automated:

python -m py_compile studio/backend/routes/inference.py studio/backend/core/inference/llama_cpp.py studio/backend/tests/test_openai_tool_passthrough.py studio/backend/tests/test_llama_cpp_stream_cancel.py
python -m pytest studio/backend/tests/test_openai_tool_passthrough.py -q --tb=short
python -m pytest studio/backend/tests/test_llama_cpp_stream_cancel.py -q --tb=short
git diff --check

Targeted local result:

224 passed, 1 warning
2 passed

Source-clone LAN API checks:

  • server launched from studio/backend/run.py;
  • URL: http://192.168.20.80:8899;
  • command shape: --host 0.0.0.0 --port 8899 --parallel 1 --api-only --no-cloudflare --disable-tools --silent;
  • model: unsloth/Qwen3.6-27B-MTP-GGUF;
  • variant: UD-Q4_K_XL;
  • loaded context: 129536;
  • standard non-stream small request: HTTP 200;
  • standard stream small request: HTTP 200, one [DONE], no malformed JSON chunks;
  • tools passthrough stream: HTTP 200, one [DONE], no malformed JSON chunks;
  • reasoning remains enabled and reasoning_content is preserved.

Real external-client checks:

  • OpenCode CLI 1.17.14 with an @ai-sdk/openai-compatible provider:

    • simple run: RC=0, expected marker received;

    • three consecutive continued turns: RC=0;

    • large attached-file prompt: RC=0, expected marker received;

    • short log excerpt:

      RC=0
      JSON parsing failed: absent
      terminated: absent
      malformed SSE/JSON chunks: absent
      
  • Codex CLI 0.142.5 with wire_api = "responses" and a local model catalog entry:

    • simple run: RC=0, expected marker received;

    • file/tool run: RC=0, expected marker received;

    • large prompt: RC=0, expected marker received;

    • short log excerpt:

      RC=0
      JSON parsing failed: absent
      Unexpected non-whitespace: absent
      TypeValidationError: absent
      NoSuchModel: absent
      timeout: absent
      

Compatibility Notes

  • Explicit client max_tokens / max_completion_tokens values are preserved unless the optional local ceiling is configured.
  • Queued/admission behavior is intentionally not part of this PR.
  • No frontend settings are added in this PR.

@gemini-code-assist

Copy link
Copy Markdown
Contributor

Warning

Gemini encountered an error creating the review. You can try again by commenting /gemini review.

@Apoze
Apoze force-pushed the studio-openai-gguf-streaming branch from f3b63e5 to 929df3b Compare July 7, 2026 21:21
@Apoze

Apoze commented Jul 7, 2026

Copy link
Copy Markdown
Contributor Author

/gemini review

@gemini-code-assist

Copy link
Copy Markdown
Contributor

Warning

Gemini encountered an error creating the review. You can try again by commenting /gemini review.

@Apoze
Apoze marked this pull request as ready for review July 7, 2026 21:36
@Apoze
Apoze requested a review from danielhanchen as a code owner July 7, 2026 21:36

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 929df3b862

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread studio/backend/routes/inference.py Resolved
Comment thread studio/backend/routes/inference.py Resolved
Comment thread studio/backend/routes/inference.py Resolved
Comment thread studio/backend/routes/inference.py Resolved
@oobabooga oobabooga self-assigned this Jul 8, 2026
@Apoze
Apoze force-pushed the studio-openai-gguf-streaming branch from 929df3b to c35d223 Compare July 8, 2026 07:23
@Apoze

Apoze commented Jul 8, 2026

Copy link
Copy Markdown
Contributor Author

Addressed the review comments in c35d223:

  • Applied UNSLOTH_OPENAI_COMPAT_MAX_TOKENS_CEILING to /v1/completions as well as chat completions.
  • Changed mid-stream stalls after partial output to emit an in-band SSE error instead of synthesizing finish_reason: "stop".
  • Allowed requests with a tools catalog and tool_choice: "none" to continue on non-tool GGUF templates when there is no tool-call history.
  • Added index fields to fallback streaming delta.tool_calls.

Validation:

  • python -m py_compile studio/backend/routes/inference.py studio/backend/core/inference/llama_cpp.py
  • python -m pytest studio/backend/tests/test_openai_tool_passthrough.py -q --tb=short -> 227 passed
  • python -m pytest studio/backend/tests/test_llama_cpp_stream_cancel.py -q --tb=short -> 2 passed
  • git diff --check

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: c35d2233c0

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread studio/backend/routes/inference.py Resolved
Comment thread studio/backend/routes/inference.py Resolved
Comment thread studio/backend/routes/inference.py Resolved
@Apoze
Apoze force-pushed the studio-openai-gguf-streaming branch 3 times, most recently from bffbbf1 to cf37939 Compare July 8, 2026 08:13

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: cf379391dc

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread studio/backend/routes/inference.py Resolved
Comment thread studio/backend/routes/inference.py Resolved
Comment thread studio/backend/routes/inference.py Resolved
@Apoze
Apoze force-pushed the studio-openai-gguf-streaming branch 3 times, most recently from 18c5554 to ef13d1c Compare July 8, 2026 10:46
@Apoze
Apoze force-pushed the studio-openai-gguf-streaming branch from b81ebe9 to e600a69 Compare July 8, 2026 10:50
@Apoze

Apoze commented Jul 8, 2026

Copy link
Copy Markdown
Contributor Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. You're on a roll.

Reviewed commit: e600a6915f

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@Apoze

Apoze commented Jul 8, 2026 •

Copy link
Copy Markdown
Contributor Author

@oobabooga @danielhanchen
Not sure if I have the right to ping someone. Sorry if I don't.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 22d7d9d428

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread studio/backend/routes/inference.py Resolved
@oobabooga

oobabooga commented Jul 9, 2026 •

Copy link
Copy Markdown
Member

Verified your streaming hardening live against a local Qwen3.6-27B GGUF, diffing main against your branch across an edge-case matrix and live spec-compliance checks on the OpenAI and Anthropic surfaces. The broken-on-main behavior checks out: killing llama-server mid-stream left the connection hanging with no terminator, and reasoning-only deltas carried a non-spec field alone. Your branch fixes both. Real spec-compliance gains, no regressions.

The streaming path is your work; the follow-ups are behavior fixes and cleanup on top:

  • Made the max_tokens cap opt-in (it was truncating reasoning models at 1024 tokens).
  • Restored tool calls on DiffusionGemma, silently broken by the routing rewrite (Studio: heal DiffusionGemma tool calls into structured tool_calls #6851).
  • Accepted max_tokens: 0 on /v1/completions (spec-valid, was rejected).
  • Dropped two default-off knobs that could silently cap a client's request or freeze a stream for minutes.
  • Trimmed dead code and duplicate parsing left over from the original approach.

@Apoze could you check if you agree with the changes and with the codex review above before I merge?

@Apoze

Apoze commented Jul 9, 2026

Copy link
Copy Markdown
Contributor Author

Hi,
I agree with the changes and with the codex review. @oobabooga

@oobabooga

Copy link
Copy Markdown
Member

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Already looking forward to the next diff.

Reviewed commit: 5c934972be

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

@oobabooga

Copy link
Copy Markdown
Member

Codex is clean on the latest commit and CI is green on the branch merged with current main. Thanks @Apoze! Merging.

@oobabooga
oobabooga merged commit 6a9b77e into unslothai:main Jul 9, 2026
1 check passed
@Apoze
Apoze deleted the studio-openai-gguf-streaming branch July 11, 2026 14:51
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.

2 participants