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
Read the Responses event type from the SSE event field (#8608)
Daniel Hanb3376300e5a5
diff --git a/studio/backend/core/inference/external_provider.py b/studio/backend/core/inference/external_provider.pyindex d72aba48dc4..47e626edb48 100644--- a/studio/backend/core/inference/external_provider.py+++ b/studio/backend/core/inference/external_provider.py@@ -5453,13 +5453,21 @@ class ExternalProviderClient:}return f"data: {_json.dumps(chunk)}"+ # The type may live only in the SSE `event:` field, not in the data object.+ sse_event_name = ""try:while True:try:line = await lines_gen.__anext__()except StopAsyncIteration:break- if not line or line.startswith("event:"):+ if not line:+ # A name never carries past the blank line that ends its event,+ # or a stale `response.failed` would fail a later frame.+ sse_event_name = ""+ continue+ if line.startswith("event:"):+ sse_event_name = line[len("event:") :].strip()continueif not line.startswith("data:"):continue@@ -5498,7 +5506,14 @@ class ExternalProviderClient:except _json.JSONDecodeError:continue- event_type = response_event_type(event)+ try:+ event_type = response_event_type(event, sse_event_name)+ except ValueError:+ # An OpenAI-compatible endpoint behind this base URL can answer+ # a Responses request with Chat Completions frames, so skipping+ # an unrecognisable frame beats failing the whole completion.+ # The ChatGPT path stays strict, where the shape is guaranteed.+ continue_record_openai_response_id(event)if event_type == "response.output_text.delta":diff --git a/studio/backend/tests/test_external_provider_usage_chunk.py b/studio/backend/tests/test_external_provider_usage_chunk.pyindex e0830f90400..54db68a974c 100644--- a/studio/backend/tests/test_external_provider_usage_chunk.py+++ b/studio/backend/tests/test_external_provider_usage_chunk.py@@ -635,3 +635,76 @@ def test_kimi_no_search_fallback_requests_usage(monkeypatch):assert "tools" in search_bodyassert "tools" not in fallback_bodyassert fallback_body["stream_options"] == {"include_usage": True}+++def test_a_type_carried_only_by_the_sse_event_field_is_honoured(monkeypatch):+ """Only the SSE ``event:`` line carries the type here, and a type-less frame is+ skipped rather than fatal, so the usage would vanish silently. Built raw, since+ _openai_sse always repeats the type in data."""+ body = (+ b"event: response.completed\n"+ b'data: {"response":{"usage":{"input_tokens":7,"output_tokens":3}}}\n'+ b"\n"+ )++ def handler(request: httpx.Request) -> httpx.Response:+ return httpx.Response(200, content = body, headers = {"content-type": "text/event-stream"})++ _mock_http_client(monkeypatch, handler)++ async def run():+ client = _make_openai_client()+ return await _collect(+ client._stream_openai_responses(+ messages = [{"role": "user", "content": "ping"}],+ model = "gpt-5.5",+ temperature = 0.7,+ top_p = 0.95,+ max_tokens = 1024,+ enable_thinking = None,+ reasoning_effort = None,+ )+ )++ usages = _usage_chunks(_drive(run()))+ assert len(usages) == 1, usages+++def test_an_sse_event_name_does_not_carry_past_its_blank_line(monkeypatch):+ """Held past its blank line, a stale ``response.failed`` would claim the next+ type-less frame, emit a 502 and break the loop before the real usage."""+ body = (+ b"event: response.failed\n"+ b"data:\n"+ b"\n"+ b'data: {"choices":[{"delta":{"content":"ok"}}]}\n'+ b"\n"+ b"event: response.completed\n"+ b'data: {"response":{"usage":{"input_tokens":7,"output_tokens":3}}}\n'+ b"\n"+ b"data: [DONE]\n"+ b"\n"+ )++ def handler(request: httpx.Request) -> httpx.Response:+ return httpx.Response(200, content = body, headers = {"content-type": "text/event-stream"})++ _mock_http_client(monkeypatch, handler)++ async def run():+ client = _make_openai_client()+ return await _collect(+ client._stream_openai_responses(+ messages = [{"role": "user", "content": "ping"}],+ model = "gpt-5.5",+ temperature = 0.7,+ top_p = 0.95,+ max_tokens = 1024,+ enable_thinking = None,+ reasoning_effort = None,+ )+ )++ lines = _drive(run())+ assert not [line for line in lines if '"provider_error"' in line], lines+ assert len(_usage_chunks(lines)) == 1, lines