From 737017a511c7fb7934899c0b01ad2cac1699f7e5 Mon Sep 17 00:00:00 2001 From: Roland Tannous Date: Thu, 2 Apr 2026 21:42:56 +0000 Subject: [PATCH] fix(studio): lazy-import extract_model_size_b in llama_cpp to fix transformers 5.x version switch MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit PR #4769 (e4d14992) added a module-level `from utils.models import extract_model_size_b` to llama_cpp.py. Since core/inference/__init__.py eagerly imports llama_cpp, the spawn subprocess now hits: core.inference.__init__ → llama_cpp → utils.models → model_config (line 8: `from transformers import AutoConfig`) …before _activate_transformers_version() has a chance to prepend .venv_t5/ to sys.path. Python caches transformers 4.57.6 in sys.modules, and no subsequent sys.path change can override it. Fix: replace the eager import with a thin wrapper that defers the import to first call. This breaks the transitive chain so transformers is not loaded until after version activation. --- studio/backend/core/inference/llama_cpp.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/studio/backend/core/inference/llama_cpp.py b/studio/backend/core/inference/llama_cpp.py index 44c700bf3d..045c4972e1 100644 --- a/studio/backend/core/inference/llama_cpp.py +++ b/studio/backend/core/inference/llama_cpp.py @@ -52,8 +52,15 @@ _REPROMPT_MAX_CHARS = 2000 _SHARD_FULL_RE = re.compile(r"^(.*)-(\d{5})-of-(\d{5})\.gguf$") _SHARD_RE = re.compile(r"^(.*)-\d{5}-of-\d{5}\.gguf$") -# Model size extraction (shared with routes/inference.py) -from utils.models import extract_model_size_b as _extract_model_size_b +# Model size extraction — lazy import to avoid pulling in transformers +# at module level (utils.models.model_config imports AutoConfig). +# This module is imported by core/inference/__init__.py, which runs +# before the subprocess version-activation code, so an eager import +# here would cache transformers 4.x in sys.modules and break the +# 5.x version switch. +def _extract_model_size_b(model_id: str): + from utils.models import extract_model_size_b + return extract_model_size_b(model_id) # ── Pre-compiled patterns for tool XML stripping ───────────── _TOOL_CLOSED_PATS = [