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: keep health checks responsive during Anthropic generation (#9201)
Lee Jackson92d3f87055ba
diff --git a/studio/backend/routes/inference.py b/studio/backend/routes/inference.pyindex 3aaf1b7768d..8de4327795f 100644--- a/studio/backend/routes/inference.py+++ b/studio/backend/routes/inference.py@@ -20526,6 +20526,7 @@ async def anthropic_messages(model_name,disable_parallel_tool_use = _disable_parallel,openai_tools = openai_tools,+ cancel_event = cancel_event,))@@ -20566,6 +20567,7 @@ async def anthropic_messages(_run_plain_gen,message_id,model_name,+ cancel_event = cancel_event,))@@ -20842,11 +20844,19 @@ def _anthropic_map_generation_error(e: Exception) -> HTTPException:return HTTPException(status_code = 500, detail = _friendly_error(e))-def _collect_anthropic_events(run_gen) -> list:+async def _collect_anthropic_events(run_gen, cancel_event = None) -> list:"""Drain the generator into a list, mapping an upstream 4xx / contextoverflow to a clean Anthropic 400 instead of leaking a 500."""- try:++ def _drain():return list(run_gen())++ drain_task = asyncio.create_task(asyncio.to_thread(_drain))+ try:+ return await asyncio.shield(drain_task)+ except asyncio.CancelledError:+ await _drain_pending_next_task(drain_task, cancel_event)+ raiseexcept HTTPException:raiseexcept Exception as e:@@ -20878,6 +20888,7 @@ async def _anthropic_tool_non_streaming(model_name,disable_parallel_tool_use = False,openai_tools = None,+ cancel_event = None,):"""Non-streaming response for the tool-calling path.@@ -20903,7 +20914,7 @@ async def _anthropic_tool_non_streaming(# trailing text. See the stop_reason mapping below.ends_on_tool_use = False- events = _collect_anthropic_events(run_gen)+ events = await _collect_anthropic_events(run_gen, cancel_event)for event in events:etype = event.get("type", "")@@ -20975,14 +20986,19 @@ async def _anthropic_tool_non_streaming()-async def _anthropic_plain_non_streaming(run_gen, message_id, model_name):+async def _anthropic_plain_non_streaming(+ run_gen,+ message_id,+ model_name,+ cancel_event = None,+):"""Non-streaming response for the no-tool path."""text_parts = []usage = {}prev_text = ""captured_finish_reason = None- events = _collect_anthropic_events(run_gen)+ events = await _collect_anthropic_events(run_gen, cancel_event)for cumulative in events:if isinstance(cumulative, dict):diff --git a/studio/backend/tests/test_anthropic_messages.py b/studio/backend/tests/test_anthropic_messages.pyindex cc1e536558c..e1daa1fc084 100644--- a/studio/backend/tests/test_anthropic_messages.py+++ b/studio/backend/tests/test_anthropic_messages.py@@ -41,6 +41,7 @@ from routes.inference import (_select_anthropic_server_tools,_anthropic_requested_studio_tools,_anthropic_passthrough_stream,+ _anthropic_plain_non_streaming,_anthropic_tool_non_streaming,_monitor_anthropic_sse_line,anthropic_messages,@@ -936,6 +937,64 @@ class TestAnthropicStreamEmitter:class TestAnthropicToolNonStreaming:+ @pytest.mark.parametrize(+ ("helper", "event"),+ [+ pytest.param(+ _anthropic_tool_non_streaming,+ {"type": "content", "text": "ok"},+ id = "tools",+ ),+ pytest.param(_anthropic_plain_non_streaming, "ok", id = "plain"),+ ],+ )+ def test_generator_is_drained_off_the_event_loop(self, helper, event):+ loop_thread = threading.current_thread()+ generator_threads = []++ def _run_gen():+ generator_threads.append(threading.current_thread())+ yield event++ response = asyncio.run(helper(_run_gen, "msg_1", "m"))++ assert response.status_code == 200+ assert len(generator_threads) == 1+ assert generator_threads[0] is not loop_thread++ @pytest.mark.parametrize(+ ("helper", "event"),+ [+ pytest.param(+ _anthropic_tool_non_streaming,+ {"type": "content", "text": "ok"},+ id = "tools",+ ),+ pytest.param(_anthropic_plain_non_streaming, "ok", id = "plain"),+ ],+ )+ def test_cancellation_waits_for_generator_worker(self, helper, event):+ generator_started = threading.Event()+ generator_stopped = threading.Event()+ cancel_event = threading.Event()++ def _run_gen():+ generator_started.set()+ assert cancel_event.wait(1.0)+ generator_stopped.set()+ yield event++ async def _cancel_generation():+ task = asyncio.create_task(helper(_run_gen, "msg_1", "m", cancel_event = cancel_event))+ assert await asyncio.to_thread(generator_started.wait, 1.0)+ task.cancel()+ with pytest.raises(asyncio.CancelledError):+ await task+ assert cancel_event.is_set()+ assert generator_stopped.is_set()++ asyncio.run(_cancel_generation())+def test_duplicate_tool_start_replaces_provisional_tool_block(self):def _run_gen():yield {