Fosowl/agenticSeek · error · Exception

Unexpected error: {str(e)}

Error message

Unexpected error: {str(e)}

What it means

The last-resort except block in lm_studio_fn: any exception whose message does not contain 'LM Studio' is re-wrapped as 'Unexpected error: <details>'. This catches failures from non-request code in the function, such as JSON parsing of the response, missing attributes, or SDK errors raised before the request-specific handlers saw them.

Source

Thrown at sources/llm_provider.py:416

            if not choices:
                raise Exception(f"No choices in LM Studio response: {result}")

            message = choices[0].get("message", {})
            content = message.get("content", "")
            if not content:
                raise Exception(f"Empty content in LM Studio response: {result}")
            return content

        except requests.exceptions.Timeout:
            raise Exception("LM Studio request timed out - check if server is responsive")
        except requests.exceptions.ConnectionError:
            raise Exception(f"Cannot connect to LM Studio at {route_start} - check if server is running")
        except requests.exceptions.RequestException as e:
            raise Exception(f"HTTP request failed: {str(e)}") from e
        except Exception as e:
            if "LM Studio" in str(e):
                raise  # Re-raise our custom exceptions
            raise Exception(f"Unexpected error: {str(e)}") from e

    def openrouter_fn(self, history, verbose=False):
        """
        Use OpenRouter API to generate text.
        """
        client = OpenAI(api_key=self.api_key, base_url="https://openrouter.ai/api/v1")
        if self.is_local:
            # This case should ideally not be reached if unsafe_providers is set correctly
            # and is_local is False in config for openrouter
            raise Exception("OpenRouter is not available for local use. Change config.ini")
        try:
            response = client.chat.completions.create(
                model=self.model,
                messages=history,
            )
            if response is None:
                raise Exception("OpenRouter response is empty.")
            thought = response.choices[0].message.content

View on GitHub (pinned to ae57a23577)

Solutions

  1. Print/log the chained exception (raise ... from e preserves the cause) to see the real underlying error
  2. Verify the configured model name exists in LM Studio's loaded models
  3. Confirm the endpoint returns JSON (curl http://localhost:1234/v1/chat/completions)
  4. If the response is non-JSON, check for proxy/HTML interference and bypass it
  5. Update LM Studio to a current version if its API response shape changed

Example fix

// before
try:
    result = lm_studio_fn(history)
except Exception as e:
    print(e)  # loses the chained cause
// after
try:
    result = lm_studio_fn(history)
except Exception as e:
    import traceback; traceback.print_exception(type(e), e, e.__traceback__)  # shows original cause via __cause__
Defensive patterns

Strategy: try-catch

Validate before calling

def validate_lm_studio(url):
    import requests
    try:
        r = requests.get(url + '/models' if url.endswith('/v1') else url.rsplit('/',1)[0] + '/models', timeout=5)
        return r.ok and 'data' in r.json()
    except Exception:
        return False

Try / catch

try:
    out = provider.lm_studio_fn(history)
except Exception as e:
    if 'Unexpected error' in str(e) and e.__cause__:
        log.error("Underlying cause: %s", repr(e.__cause__))
    raise

Prevention

When it happens

Trigger: lm_studio_fn raises an exception not produced by requests and not containing 'LM Studio' in its message — e.g. response.json() failing on an HTML error page, KeyError/IndexError on an unexpected response body, or an OpenAI client error.

Common situations: LM Studio returns an HTML error page (proxy/captive portal) instead of JSON; model name invalid so server responds with an unexpected structure; concurrent modification of the response stream; a bug in custom prompt handling code.

Related errors


AI-assisted analysis of Fosowl/agenticSeek@ae57a23577 (2026-08-30). Data as JSON: /api/errors/b100c0e41a6e07c9. Report an issue: GitHub.