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.

Fix Studio crash on Anaconda/conda-forge Python (#4484)

Daniel Han100b8857f226
diff --git a/studio/backend/_platform_compat.py b/studio/backend/_platform_compat.py
new file mode 100644
index 00000000000..490e1820d71
--- /dev/null
+++ b/studio/backend/_platform_compat.py
@@ -0,0 +1,59 @@
+# SPDX-License-Identifier: AGPL-3.0-only
+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+"""
+Compatibility shim for Anaconda/conda-forge Python builds.
+
+Anaconda modifies sys.version to include distributor metadata between pipe
+characters, e.g. '3.12.4 | packaged by Anaconda, Inc. | (main, ...) [MSC ...]'.
+Python's platform._sys_version() has a hardcoded regex that cannot parse this,
+raising ValueError. CPython closed this as "not planned" (cpython#102396).
+
+This module seeds platform._sys_version_cache so the stdlib parser never sees
+the problematic string, fixing the import chain:
+ structlog -> rich.pretty -> attrs._compat -> platform.python_implementation()
+
+Import this module before any library imports that may trigger the above chain.
+Safe to import multiple times (no-op if cache is already seeded or no pipes).
+"""
+
+import platform
+import re
+import sys
+
+
+def _seed_sys_version_cache() -> None:
+ """One-shot cache prime: parse a cleaned sys.version and seed the cache."""
+ raw = sys.version
+
+ # Strip paired |...| segments (Anaconda, conda-forge metadata)
+ cleaned = re.sub(r"\s*\|[^|]*\|\s*", " ", raw).strip()
+
+ # Format B: "ver (build) | label | (build_dup) \n[compiler]"
+ # After pipe-strip, two consecutive (...) groups remain; drop the second.
+ cleaned = re.sub(r"(\([^)]*\))\s+\([^)]*\)", r"\1", cleaned)
+
+ if "|" in cleaned:
+ # Unpaired pipe remaining -- keep version + everything from "(" onward
+ m = re.match(r"([\w.+]+)\s*", cleaned)
+ p = cleaned.find("(")
+ if m and p > 0:
+ cleaned = m.group(0) + cleaned[p:]
+
+ if cleaned == raw:
+ return # Nothing to fix
+
+ # Parse the cleaned string through the real stdlib parser
+ try:
+ result = platform._sys_version(cleaned)
+ except ValueError:
+ return # Cleaning didn't produce a parseable string; don't make things worse
+
+ # Seed the cache so future calls with the raw string skip parsing entirely
+ cache = getattr(platform, "_sys_version_cache", None)
+ if isinstance(cache, dict):
+ cache[raw] = result
+
+
+if "|" in sys.version:
+ _seed_sys_version_cache()
diff --git a/studio/backend/colab.py b/studio/backend/colab.py
index 7345de8c462..25a9408ccbe 100644
--- a/studio/backend/colab.py
+++ b/studio/backend/colab.py
@@ -9,6 +9,14 @@ Uses Colab's built-in proxy - no external tunneling needed!
from pathlib import Path
import sys
+# Fix for Anaconda/conda-forge Python: seed platform._sys_version_cache before
+# any library imports that trigger attrs -> rich -> structlog -> platform crash.
+# See: https://github.com/python/cpython/issues/102396
+_backend_dir = str(Path(__file__).parent)
+if _backend_dir not in sys.path:
+ sys.path.insert(0, _backend_dir)
+import _platform_compat # noqa: F401
+
def _bootstrap_studio_venv() -> None:
"""Expose the Studio venv's site-packages to the current interpreter.
@@ -35,11 +43,6 @@ def _bootstrap_studio_venv() -> None:
_bootstrap_studio_venv()
-# Add backend to path early so local modules like loggers can be imported
-backend_path = str(Path(__file__).parent)
-if backend_path not in sys.path:
- sys.path.insert(0, backend_path)
-
from loggers import get_logger
logger = get_logger(__name__)
diff --git a/studio/backend/main.py b/studio/backend/main.py
index 23c868b5358..7134c5a7839 100644
--- a/studio/backend/main.py
+++ b/studio/backend/main.py
@@ -6,12 +6,24 @@ Main FastAPI application for Unsloth UI Backend
"""
import os
+import sys
+from pathlib import Path as _Path
# Suppress annoying C-level dependency warnings globally
os.environ["PYTHONWARNINGS"] = "ignore"
+# Ensure backend dir is on sys.path so _platform_compat is importable when
+# main.py is launched directly (e.g. `uvicorn main:app`).
+_backend_dir = str(_Path(__file__).parent)
+if _backend_dir not in sys.path:
+ sys.path.insert(0, _backend_dir)
+
+# Fix for Anaconda/conda-forge Python: seed platform._sys_version_cache before
+# any library imports that trigger attrs -> rich -> structlog -> platform crash.
+# See: https://github.com/python/cpython/issues/102396
+import _platform_compat # noqa: F401
+
import shutil
-import sys
import warnings
from contextlib import asynccontextmanager
diff --git a/studio/backend/run.py b/studio/backend/run.py
index 2d86f705783..d1ee93ed640 100644
--- a/studio/backend/run.py
+++ b/studio/backend/run.py
@@ -8,17 +8,21 @@ Works independently and can be moved to any directory.
import os
import sys
+from pathlib import Path
# Suppress annoying C-level dependency warnings globally (e.g. SwigPyPacked)
os.environ["PYTHONWARNINGS"] = "ignore"
-from pathlib import Path
-
-# Add the backend directory to Python path
+# Add the backend directory to Python path early so local modules are importable
backend_dir = Path(__file__).parent
if str(backend_dir) not in sys.path:
sys.path.insert(0, str(backend_dir))
+# Fix for Anaconda/conda-forge Python: seed platform._sys_version_cache before
+# any library imports that trigger attrs -> rich -> structlog -> platform crash.
+# See: https://github.com/python/cpython/issues/102396
+import _platform_compat # noqa: F401
+
from loggers import get_logger
logger = get_logger(__name__)