Skip to content

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.

Studio: render /openapi.json once per schema, off the event loop (#15684)

Daniel Hanb0bfa3cc34f0
diff --git a/studio/backend/main.py b/studio/backend/main.py
index 4591d11742e..a7a6ce409f9 100644
--- a/studio/backend/main.py
+++ b/studio/backend/main.py
@@ -1373,15 +1373,33 @@ def _openapi_once() -> dict:
app.openapi = _openapi_once
[email protected](app.openapi_url, include_in_schema = False)
-async def openapi_json(request: Request):
- schema = await asyncio.to_thread(app.openapi)
+# Rendered once per schema object, off the loop: a rebuilt schema (openapi_schema reset) is a new object.
+_openapi_body_cache: dict = {}
+
+
+def _openapi_body(root_path: str) -> bytes:
+ schema = app.openapi()
+ key = (root_path, app.root_path_in_servers)
+ cached = _openapi_body_cache.get(key)
+ if cached is not None and cached[0] is schema:
+ return cached[1]
+ served = schema
# As FastAPI's built-in route: name the prefix a path-stripping proxy hides.
- root_path = request.scope.get("root_path", "").rstrip("/")
if root_path and app.root_path_in_servers:
if root_path not in {server.get("url") for server in schema.get("servers", [])}:
- schema = {**schema, "servers": [{"url": root_path}, *schema.get("servers", [])]}
- return JSONResponse(schema)
+ served = {**schema, "servers": [{"url": root_path}, *schema.get("servers", [])]}
+ body = JSONResponse(served).body
+ if len(_openapi_body_cache) >= 8:
+ _openapi_body_cache.clear()
+ _openapi_body_cache[key] = (schema, body)
+ return body
+
+
[email protected](app.openapi_url, include_in_schema = False)
+async def openapi_json(request: Request):
+ root_path = request.scope.get("root_path", "").rstrip("/")
+ body = await asyncio.to_thread(_openapi_body, root_path)
+ return Response(content = body, media_type = JSONResponse.media_type)
if _DOCS_ASSETS_DIR.is_dir():
diff --git a/studio/backend/tests/test_openapi_body_cache.py b/studio/backend/tests/test_openapi_body_cache.py
new file mode 100644
index 00000000000..b03ee9b0bd8
--- /dev/null
+++ b/studio/backend/tests/test_openapi_body_cache.py
@@ -0,0 +1,69 @@
+# SPDX-License-Identifier: AGPL-3.0-only
+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+"""/openapi.json is rendered once per schema, off the loop, byte-identical to JSONResponse."""
+
+from __future__ import annotations
+
+import pytest
+from fastapi.testclient import TestClient
+from starlette.responses import JSONResponse
+
+
+def main_module():
+ import main
+ return main
+
+
+def test_body_and_headers_match_a_plain_json_response(main_module):
+ client = TestClient(main_module.app)
+ served = client.get("/openapi.json")
+ expected = JSONResponse(main_module.app.openapi())
+ assert served.status_code == 200
+ assert served.content == expected.body
+ assert served.headers["content-type"] == expected.headers["content-type"]
+ assert served.headers["content-length"] == expected.headers["content-length"]
+
+
+def test_repeat_fetches_render_once(main_module, monkeypatch):
+ client = TestClient(main_module.app)
+ client.get("/openapi.json")
+ renders = []
+ real = JSONResponse.render
+
+ def counted(self, content):
+ if isinstance(content, dict) and "paths" in content:
+ renders.append(1)
+ return real(self, content)
+
+ monkeypatch.setattr(JSONResponse, "render", counted)
+ for _ in range(3):
+ assert client.get("/openapi.json").status_code == 200
+ assert renders == []
+
+
+def test_a_rebuilt_schema_is_served_afresh(main_module):
+ app = main_module.app
+ client = TestClient(app)
+ client.get("/openapi.json")
+ saved = app.openapi_schema
+ path = "/api/__openapi_cache_probe"
+ app.add_api_route(path, lambda: {"ok": True}, methods = ["GET"])
+ try:
+ # Some FastAPI versions keep the cached schema after a route change: reset it explicitly.
+ app.openapi_schema = None
+ assert path in client.get("/openapi.json").json()["paths"]
+ assert client.get("/openapi.json").content == JSONResponse(app.openapi()).body
+ finally:
+ app.router.routes[:] = [r for r in app.router.routes if getattr(r, "path", None) != path]
+ app.openapi_schema = saved
+
+
+def test_root_path_variant_is_cached_separately(main_module):
+ app = main_module.app
+ plain = TestClient(app).get("/openapi.json").json()
+ prefixed = TestClient(app, root_path = "/studio").get("/openapi.json").json()
+ again = TestClient(app).get("/openapi.json").json()
+ assert "servers" not in plain and "servers" not in again
+ assert prefixed["servers"][0] == {"url": "/studio"}