Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.
Python agentaichatgptdeepseekfine-tuninggemmaimage-generationllamallmllmsopenaipythonqwenreinforcement-learningself-hostedstable-diffusiontext-to-speechttsuiunsloth
Studio: show an error when Gemini fails a reply partway through (#12242)
Nilay4a7741930305
diff --git a/studio/backend/core/inference/external_provider.py b/studio/backend/core/inference/external_provider.pyindex ea27e328290..f34cc5c5283 100644--- a/studio/backend/core/inference/external_provider.py+++ b/studio/backend/core/inference/external_provider.py@@ -4582,6 +4582,9 @@ class ExternalProviderClient:# 3.13 + httpcore 1.0.x GeneratorExit ordering).lines_gen = response.aiter_lines().__aiter__()final_finish_reason: Optional[str] = None+ bare_json = ""+ stream_error: Optional[str] = None+ stream_error_message = ""try:while True:try:@@ -4590,9 +4593,18 @@ class ExternalProviderClient:breakif not line.strip():continue- if not line.startswith("data:"):+ if line.startswith("data:"):+ data_str = line[len("data:") :].strip()+ elif bare_json or line.lstrip().startswith("{"):+ # Gemini sends a mid-stream error as bare multi-line JSON, not as a `data:` frame.+ bare_json += line+ try:+ _json.loads(bare_json)+ except ValueError:+ continue+ data_str, bare_json = bare_json, ""+ else:continue- data_str = line[len("data:") :].strip()if not data_str or data_str == "[DONE]":continuetry:@@ -4606,6 +4618,19 @@ class ExternalProviderClient:if not isinstance(event, dict):continue+ error = event.get("error")+ if isinstance(error, dict):+ code = error.get("code")+ stream_error = _error_sse_line(+ code if isinstance(code, int) else 502,+ _json.dumps(event),+ self.provider_type,+ )+ stream_error_message = str(+ error.get("message") or error.get("status") or code+ )+ break+# Latch usageMetadata across deltas -- the final fragment carries the complete totals.usage_meta = event.get("usageMetadata")if isinstance(usage_meta, dict):@@ -4987,12 +5012,20 @@ class ExternalProviderClient:"type": "tool_end","tool_call_id": web_search_tool_id,"result": (- "\n---\n".join(blocks) if blocks else "(search complete)"+ f"(search aborted: {stream_error_message})"+ if stream_error+ else "\n---\n".join(blocks)+ if blocks+ else "(search complete)"),})web_search_tool_ended = True+ if stream_error:+ yield stream_error+ return+if final_finish_reason:# Gemini emits "STOP" even for a pure functionCall turn; override to "tool_calls" so OAI# clients run the tool.diff --git a/studio/backend/tests/test_gemini_stream_error.py b/studio/backend/tests/test_gemini_stream_error.pynew file mode 100644index 00000000000..e6a5dbee6ea--- /dev/null+++ b/studio/backend/tests/test_gemini_stream_error.py@@ -0,0 +1,135 @@+# SPDX-License-Identifier: AGPL-3.0-only+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0++import json++import httpx+import pytest++from core import research_runs++from .test_gemini_provider import _drive, _make_gemini_client, _mock_http, _parse_chunks++_TEXT = {+ "candidates": [+ {"content": {"parts": [{"text": "The three causes are"}], "role": "model"}, "index": 0}+ ],+ "modelVersion": "gemini-2.5-flash",+}+++def _frame(event):+ return f"data: {json.dumps(event)}\r\n\r\n"+++def _bare_error(code, message, status):+ return (+ json.dumps({"error": {"code": code, "message": message, "status": status}}, indent = 2) + "\n"+ )+++_OVERLOADED = _bare_error(503, "The model is overloaded. Please try again later.", "UNAVAILABLE")+_OVERLOADED_SHOWN = {+ "message": "The model is overloaded. Please try again later. (UNAVAILABLE)",+ "code": "503",+}+++def _stream_lines(monkeypatch, body, **kwargs):+ def handler(request: httpx.Request) -> httpx.Response:+ return httpx.Response(+ 200,+ content = body.encode("utf-8"),+ headers = {"content-type": "text/event-stream"},+ )++ _mock_http(monkeypatch, handler)++ async def run():+ client = _make_gemini_client()+ lines = [+ line+ async for line in client.stream_chat_completion(+ messages = [{"role": "user", "content": "hi"}],+ model = "gemini-2.5-flash",+ temperature = 0.7,+ top_p = 0.95,+ max_tokens = 64,+ **kwargs,+ )+ ]+ await client.close()+ return lines++ return _drive(run())+++ ("body", "content", "expected"),+ [+ (+ _frame(_TEXT) + _OVERLOADED,+ "The three causes are",+ _OVERLOADED_SHOWN,+ ),+ (+ _OVERLOADED,+ "",+ _OVERLOADED_SHOWN,+ ),+ (+ _frame(_TEXT) + _bare_error(500, "An internal error has occurred.", "INTERNAL"),+ "The three causes are",+ {"message": "An internal error has occurred. (INTERNAL)", "code": "500"},+ ),+ (+ _frame(_TEXT)+ + _frame({"error": {"code": 503, "message": "Overloaded", "status": "UNAVAILABLE"}}),+ "The three causes are",+ {"message": "Overloaded (UNAVAILABLE)", "code": "503"},+ ),+ ],+ ids = [+ "bare-503-after-text",+ "bare-503-before-text",+ "bare-500-after-text",+ "data-503-after-text",+ ],+)+def test_midstream_error_ends_stream_with_error(monkeypatch, body, content, expected):+ lines = _stream_lines(monkeypatch, body)+ payloads = _parse_chunks(lines)++ assert payloads[-1:] == [+ {"error": {**expected, "type": "provider_error", "provider": "gemini"}}+ ]+ assert "data: [DONE]" not in lines+ combined = "".join(p["choices"][0]["delta"].get("content", "") for p in payloads[:-1])+ assert combined == content+++def test_midstream_rate_limit_is_retried_by_research(monkeypatch):+ lines = _stream_lines(+ monkeypatch,+ _bare_error(429, "Resource has been exhausted.", "RESOURCE_EXHAUSTED"),+ )++ assert _parse_chunks(lines)[-1]["error"]["code"] == "429"+ assert research_runs._stream_rate_limit_delay(lines[0]) == 0.0+++def test_midstream_error_marks_web_search_aborted(monkeypatch):+ lines = _stream_lines(monkeypatch, _frame(_TEXT) + _OVERLOADED, enabled_tools = ["web_search"])+ tool_end = [+ p["_toolEvent"]+ for p in _parse_chunks(lines)+ if p.get("_toolEvent", {}).get("type") == "tool_end"+ ]++ assert tool_end == [+ {+ "type": "tool_end",+ "tool_call_id": "gemini_web_search",+ "result": "(search aborted: The model is overloaded. Please try again later.)",+ }+ ]