MemPalace/mempalace · error · LLMError

HTTP {e.code} from {url}: {detail or e.reason}

Error message

HTTP {e.code} from {url}: {detail or e.reason}

What it means

LLMError raised by the shared _http_post_json helper when the LLM provider's HTTP endpoint returns an error status. The message includes the status code, URL, and up to 500 bytes of the response body (or the HTTP reason phrase), so auth failures, model-not-found, and rate limits are distinguishable.

Source

Thrown at mempalace/llm_client.py:195


def _http_post_json(url: str, body: dict, headers: dict, timeout: int) -> dict:
    """POST JSON and return the parsed response. Raises LLMError on any failure."""
    req = Request(
        url,
        data=json.dumps(body).encode("utf-8"),
        headers={"Content-Type": "application/json", **headers},
    )
    try:
        with urlopen(req, timeout=timeout) as resp:
            return json.loads(resp.read())
    except HTTPError as e:
        detail = ""
        try:
            detail = e.read().decode("utf-8", errors="replace")[:500]
        except Exception:
            pass
        raise LLMError(f"HTTP {e.code} from {url}: {detail or e.reason}") from e
    except (URLError, OSError) as e:
        raise LLMError(f"Cannot reach {url}: {e}") from e
    except json.JSONDecodeError as e:
        raise LLMError(f"Malformed response from {url}: {e}") from e


# ==================== OLLAMA ====================


class OllamaProvider(LLMProvider):
    name = "ollama"
    DEFAULT_ENDPOINT = "http://localhost:11434"

    def __init__(
        self,
        model: str,
        endpoint: Optional[str] = None,
        timeout: int = 180,

View on GitHub (pinned to 06cb6987f0)

Solutions

  1. Read the status code: 401/403 → fix the API key; 404 → fix endpoint URL or model name; 429 → slow down/back off; 5xx → check the local server logs
  2. curl the endpoint directly to reproduce outside the library
  3. For Ollama run `ollama list` / `ollama pull <model>` to confirm the model exists
  4. Add exponential-backoff retry around 429/5xx responses

Example fix

# before
resp = provider.classify(system, user)  # LLMError: HTTP 404 from http://localhost:11434/v1/chat/completions: model not found

# after
from mempalace.llm_client import LLMError
try:
    resp = provider.classify(system, user)
except LLMError as e:
    if "HTTP 4" in str(e):
        raise  # config problem — fix endpoint/model/key
    raise  # or backoff-retry on 429/5xx
Defensive patterns

Strategy: retry

Try / catch

import time
from mempalace.llm_client import LLMError

for attempt in range(3):
    try:
        return provider.classify(system, user)
    except LLMError as e:
        msg = str(e)
        if "HTTP 429" in msg or "HTTP 5" in msg:
            time.sleep(2 ** attempt)
            continue
        raise  # 4xx config errors are not retryable

Prevention

When it happens

Trigger: Any provider classify()/chat call where the server returns 4xx/5xx: 401/403 for a bad API key, 404 for a wrong endpoint URL or unknown model, 429 for rate limiting, 500/503 for an overloaded Ollama/vLLM/LM Studio instance.

Common situations: Typo in --llm-endpoint or model name; expired or missing OPENAI_API_KEY/ANTHROPIC_API_KEY; Ollama not serving the requested model (pull it first); hitting provider rate limits; local server crashed mid-request.

Related errors


AI-assisted analysis of MemPalace/mempalace@06cb6987f0 (2026-08-15). Data as JSON: /api/errors/313fb938559836ad. Report an issue: GitHub.