Fine-tune LLMs from one YAML. Layer streaming trains an 8B model on a 4 GB laptop GPU.
Python cliconsumer-gpudpofine-tuningggufhuggingfacellmllmopslocal-ailocal-llmloralow-vrammachine-learningollamapeftpythonpytorchqlorasfttransformers
fix(cli): classify 401/403 from the exception object, not bare digits (#1267) (#1268)
Александр М.e1f6358385b5
diff --git a/changelog.d/0.75.1/1268.fixed.md b/changelog.d/0.75.1/1268.fixed.mdnew file mode 100644index 00000000..3fdf3b4c--- /dev/null+++ b/changelog.d/0.75.1/1268.fixed.md@@ -0,0 +1,11 @@+- **`401`/`403` digits inside an error message no longer masquerade as an auth failure (#1267 by @sup3dev in #1268).**+ A row number (`train row 401:`), tensor size (`(4013)`), checkpoint path+ (`checkpoint-4010`), date, or hash slice printed "Authentication failed." /+ "Access denied." and hid the real error. Auth status is now read from the+ exception object (`response.status_code`, `.code`, walking wrapped+ `__cause__`/`__context__`), with a text fallback only for explicit HTTP+ phrasing (`401 Unauthorized`, `HTTP Error 401`, `Error code: 401`); the+ bare-digit ERROR_MAP entries are gone. Genuine 401/403 from requests, urllib+ and huggingface_hub now get the specific hint instead of the generic+ "HTTP request failed.", and gated-repo hints still win for+ `GatedRepoError` / `RepositoryNotFoundError`.diff --git a/src/soup_cli/utils/errors.py b/src/soup_cli/utils/errors.pyindex ea3a91b3..afb1f882 100644--- a/src/soup_cli/utils/errors.py+++ b/src/soup_cli/utils/errors.py@@ -1,5 +1,6 @@"""Friendly error handling — maps raw exceptions to actionable messages."""+import reimport tracebackfrom rich.console import Console@@ -225,20 +226,81 @@ ERROR_MAP = ["if you trust the model source."),),+]- # Auth errors- (- "401",+# 401/403 must be decided from the exception OBJECT (or explicit HTTP+# phrasing), never from bare digits — "train row 401:", a checkpoint path+# holding "4010", or a hash slice were all reported as auth failures and the+# real message was hidden (#1267). The old bare "401"/"403" ERROR_MAP entries+# are gone; this rule runs before the map so a genuine status also beats the+# generic "HTTPError" entry.+_AUTH_HINTS = {+ 401: ("Authentication failed.","Check your API key or token (HF_TOKEN, OPENAI_API_KEY, WANDB_API_KEY).",),- (- "403",+ 403: ("Access denied.","Check your permissions. Some models require accepting a license on HuggingFace.",),-]+}++# Text fallback for stacks that only put the status in the message. Every+# pattern requires HTTP context around the digits, so a row number, tensor+# size, path component, or hash slice cannot match.+_AUTH_STATUS_TEXT = (+ (re.compile(r"401\s+(?:Unauthorized|Client Error)"), 401),+ (re.compile(r"HTTP\s+(?:Error\s+)?[:=]?\s*401\b"), 401),+ (re.compile(r"Error code:\s*401\b"), 401),+ (re.compile(r"403\s+(?:Forbidden|Client Error)"), 403),+ (re.compile(r"HTTP\s+(?:Error\s+)?[:=]?\s*403\b"), 403),+ (re.compile(r"Error code:\s*403\b"), 403),+)++# These carry 401/403 but must not become the generic auth hint: GatedRepoError+# has its own ERROR_MAP entry, and a top-level RepositoryNotFoundError reaches+# the "gated repo" text entry. The rule declines both outright, so a wrapped+# one keeps the wrapper's own message.+_STATUS_DECLINE_TYPES = ("GatedRepoError", "RepositoryNotFoundError")+++def _exception_chain(exc: Exception):+ """Yield exc and the exceptions it wraps, without looping."""+ seen = set()+ current = exc+ while current is not None and id(current) not in seen:+ seen.add(id(current))+ yield current+ current = current.__cause__ or current.__context__+++def _auth_status(exc: Exception) -> int | None:+ """Return 401/403 when the exception is genuinely an auth failure.++ The status is read from the exception object first — `response.status_code`+ (requests / httpx / huggingface_hub), `.code` (urllib), or a direct+ `.status_code` — walking `__cause__`/`__context__` because peft and+ transformers wrap hub errors. Only when no object in the chain carries a+ 401/403 status does the HTTP-phrasing text fallback apply; an object+ carrying some other status (a 404, say) still falls through to the text+ check.+ """+ chain = list(_exception_chain(exc))+ if any(type(e).__name__ in _STATUS_DECLINE_TYPES for e in chain):+ return None+ for e in chain:+ status = getattr(getattr(e, "response", None), "status_code", None)+ if isinstance(status, int) and status in _AUTH_HINTS:+ return status+ for attr in ("code", "status_code"):+ status = getattr(e, attr, None)+ if isinstance(status, int) and status in _AUTH_HINTS:+ return status+ for pattern, status in _AUTH_STATUS_TEXT:+ if pattern.search(str(exc)):+ return status+ return Nonedef format_friendly_error(exc: Exception, verbose: bool = False) -> None:@@ -250,22 +312,32 @@ def format_friendly_error(exc: Exception, verbose: bool = False) -> None:exc_str = str(exc)exc_type = type(exc).__name__+ matched = None+ status = _auth_status(exc)+ if status is not None:+ matched = _AUTH_HINTS[status]+# Search for known error patterns- for pattern, short_msg, fix in ERROR_MAP:- if pattern in exc_str or pattern in exc_type:- error_msg = short_msg or exc_str- console.print(f"\n[bold red]Error:[/] {for_terminal(error_msg)}")- console.print(f"[green]Fix:[/] {fix}")- if verbose:- console.print()- console.print(- Panel(- for_terminal(traceback.format_exc()),- title="[dim]Full Traceback[/]",- border_style="dim",- )+ if matched is None:+ for pattern, short_msg, fix in ERROR_MAP:+ if pattern in exc_str or pattern in exc_type:+ matched = (short_msg or exc_str, fix)+ break++ if matched is not None:+ error_msg, fix = matched+ console.print(f"\n[bold red]Error:[/] {for_terminal(error_msg)}")+ console.print(f"[green]Fix:[/] {fix}")+ if verbose:+ console.print()+ console.print(+ Panel(+ for_terminal(traceback.format_exc()),+ title="[dim]Full Traceback[/]",+ border_style="dim",)- return+ )+ return# Unknown error — show type + messageconsole.print(f"\n[bold red]Error:[/] {for_terminal(exc_type)}: {for_terminal(exc_str)}")diff --git a/tests/test_issue1267_auth_status_classification.py b/tests/test_issue1267_auth_status_classification.pynew file mode 100644index 00000000..cad96718--- /dev/null+++ b/tests/test_issue1267_auth_status_classification.py@@ -0,0 +1,254 @@+"""Tests for #1267: 401/403 must be classified from the exception, not bare digits."""++import urllib.error+from io import StringIO+from unittest.mock import patch++import pytest+from rich.console import Console++from soup_cli.utils.errors import format_friendly_error+++def _render(exc: Exception) -> str:+ buf = StringIO()+ console = Console(file=buf, stderr=False)+ with patch("soup_cli.utils.errors.console", console):+ format_friendly_error(exc, verbose=False)+ return buf.getvalue()+++class _FakeResponse:+ def __init__(self, status_code: int):+ self.status_code = status_code+++class _HTTPStatusError(Exception):+ """Minimal stand-in for requests/httpx/huggingface_hub status errors."""++ def __init__(self, message: str, status_code: int):+ super().__init__(message)+ self.response = _FakeResponse(status_code)+++# --- false positives: digits inside a message are not a status ---+++def test_row_number_401_is_not_an_auth_failure():+ out = _render(ValueError(+ "train row 401: no causal-loss target remains after tokenization/truncation "+ "at data.max_length=64; the assistant response is absent or fully truncated."+ ))+ assert "Authentication failed." not in out+ assert "train row 401" in out+++def test_row_number_403_is_not_an_access_denial():+ out = _render(ValueError("train row 403: no causal-loss target remains"))+ assert "Access denied." not in out+ assert "train row 403" in out+++def test_config_digits_in_message_are_not_an_access_denial():+ out = _render(ValueError(+ "train row 7: no causal-loss target remains at data.max_length=4030"+ ))+ assert "Access denied." not in out+ assert "max_length=4030" in out+++def test_tensor_size_with_401_digits_keeps_the_real_message():+ out = _render(RuntimeError(+ "The size of tensor a (4013) must match the size of tensor b (4096) "+ "at non-singleton dimension 0"+ ))+ assert "Authentication failed." not in out+ assert "tensor a (4013)" in out+++def test_matmul_shapes_with_403_digits_keep_the_real_message():+ out = _render(RuntimeError(+ "mat1 and mat2 shapes cannot be multiplied (2x4030 and 4096x8)"+ ))+ assert "Access denied." not in out+ assert "2x4030" in out+++def test_index_digits_keep_the_real_message():+ out = _render(IndexError(+ "index 40132 is out of bounds for dimension 0 with size 32000"+ ))+ assert "Authentication failed." not in out+ assert "40132" in out+++def test_adapter_path_digits_keep_the_real_message():+ out = _render(ValueError(+ "Can't find 'adapter_config.json' at 'output/checkpoint-4010'"+ ))+ assert "Authentication failed." not in out+ assert "checkpoint-4010" in out+++def test_permission_error_with_date_path_keeps_the_real_message():+ out = _render(PermissionError("[Errno 13] Permission denied: 'runs/20260401'"))+ assert "Authentication failed." not in out+ assert "Permission denied" in out+++def test_revision_sha_containing_401_keeps_the_real_message():+ exc = _HTTPStatusError(+ "404 Client Error: Not Found for url: "+ "https://huggingface.co/model/resolve/9c1f4012abc/config.json",+ 404,+ )+ out = _render(exc)+ assert "Authentication failed." not in out+ assert "9c1f4012" in out+++def test_request_id_containing_403_keeps_the_real_message():+ exc = _HTTPStatusError(+ "404 Client Error: Not Found for url (Request ID: Root=1-68d4b2c1-4a4039d2)",+ 404,+ )+ out = _render(exc)+ assert "Access denied." not in out+ assert "4a4039d2" in out+++# --- genuine 401/403 keep (or newly get) the specific hint ---+++def test_httpx_style_401_gets_the_authentication_hint():+ exc = _HTTPStatusError(+ "Client error '401 Unauthorized' for url 'https://api.openai.com/v1/chat/completions'",+ 401,+ )+ out = _render(exc)+ assert "Authentication failed." in out+++def test_status_403_with_401_digits_in_url_maps_to_access_denied():+ exc = _HTTPStatusError(+ "Client error '403 Forbidden' for url 'https://api.example.com/v1/projects/40172/x'",+ 403,+ )+ out = _render(exc)+ assert "Access denied." in out+ assert "Authentication failed." not in out+++def test_wrapped_exception_status_is_used():+ # peft/transformers wrap hub errors: the status lives on the __cause__.+ cause = _HTTPStatusError("401 Client Error: Unauthorized for url", 401)+ wrapper = RuntimeError("Failed to load adapter")+ wrapper.__cause__ = cause+ out = _render(wrapper)+ assert "Authentication failed." in out+++def test_urllib_http_error_401_gets_the_authentication_hint():+ exc = urllib.error.HTTPError(+ "https://huggingface.co/api/whoami-v2", 401, "Unauthorized", None, None+ )+ out = _render(exc)+ assert "Authentication failed." in out+++def test_http_phrasing_without_a_response_object_is_accepted():+ out = _render(Exception("401 Client Error: Unauthorized for url: https://x"))+ assert "Authentication failed." in out+++def test_gated_repo_error_keeps_its_own_hint():+ gated_cls = type("GatedRepoError", (Exception,), {})+ exc = gated_cls("403 Client Error: Forbidden for url (gated repo)")+ exc.response = _FakeResponse(403)+ out = _render(exc)+ assert "license acceptance" in out+ assert "Access denied." not in out+++# --- wrapped hub errors and object-status probes (#1268 review) ---+++def _named_error(name: str, message: str, status: int) -> Exception:+ """An exception whose class name matches a huggingface_hub error, carrying a status."""+ exc = type(name, (Exception,), {})(message)+ exc.response = _FakeResponse(status)+ return exc+++def test_wrapped_repository_not_found_keeps_the_real_message():+ # peft: PeftConfig.from_pretrained("output/checkpoint-4000") on a missing local+ # directory asks the Hub, which answers an unauthenticated user with a 401+ # RepositoryNotFoundError; peft re-raises a ValueError `from` it.+ cause = _named_error("RepositoryNotFoundError", "401 Client Error. Repository Not Found", 401)+ try:+ raise ValueError("Can't find 'adapter_config.json' at 'output/checkpoint-4000'") from cause+ except ValueError as exc:+ out = _render(exc)+ assert "Authentication failed." not in out+ assert "checkpoint-4000" in out+++def test_wrapped_gated_repo_error_keeps_the_gated_hint():+ # transformers: cached_files raises OSError("You are trying to access a gated+ # repo...") from the GatedRepoError.+ cause = _named_error("GatedRepoError", "401 Client Error. Cannot access gated repo", 401)+ try:+ raise OSError("You are trying to access a gated repo.") from cause+ except OSError as exc:+ out = _render(exc)+ assert "license acceptance" in out+ assert "Authentication failed." not in out+++def test_implicitly_chained_status_is_used():+ # Raised inside `except` without `from`: the status lives on __context__ only.+ try:+ try:+ raise _HTTPStatusError("boom", 403)+ except _HTTPStatusError:+ raise RuntimeError("upload failed")+ except RuntimeError as exc:+ out = _render(exc)+ assert "Access denied." in out+++def test_wrapped_urllib_error_uses_its_code():+ # The text fallback reads only the top message, so a wrapped urllib error is+ # recognised by its `.code` alone.+ cause = urllib.error.HTTPError("https://x/y", 403, "Forbidden", None, None)+ try:+ raise RuntimeError("download failed") from cause+ except RuntimeError as exc:+ out = _render(exc)+ assert "Access denied." in out+++def test_direct_status_code_attribute_is_used():+ # SDK errors (for example openai.APIStatusError) carry `.status_code` on the exception.+ exc = Exception("request failed")+ exc.status_code = 401+ assert "Authentication failed." in _render(exc)+++ ("message", "hint"),+ [+ ("Client error '401 Unauthorized' for url 'https://x'", "Authentication failed."),+ ("401 Client Error: Unauthorized for url: https://x", "Authentication failed."),+ ("HTTP Error 401: Unauthorized", "Authentication failed."),+ ("request failed: HTTP 401", "Authentication failed."),+ ("Error code: 401 - {'error': 'invalid_api_key'}", "Authentication failed."),+ ("Client error '403 Forbidden' for url 'https://x'", "Access denied."),+ ("403 Client Error: Forbidden for url: https://x", "Access denied."),+ ("HTTP Error 403: Forbidden", "Access denied."),+ ("request failed: HTTP 403", "Access denied."),+ ("Error code: 403 - {'error': 'forbidden'}", "Access denied."),+ ],+)+def test_each_http_phrasing_reaches_its_hint(message, hint):+ assert hint in _render(Exception(message))