Fosowl/agenticSeek · error · Exception

Invalid JSON from LM Studio: {response.text[:200]}

Error message

Invalid JSON from LM Studio: {response.text[:200]}

What it means

lm_studio_fn calls response.json() inside a try and, on ValueError (invalid JSON), raises this message including the first 200 chars of the raw body. It means LM Studio returned non-JSON content with a 200 status — often an HTML error page or truncated output.

Source

Thrown at sources/llm_provider.py:393

            url = addr
        route_start = f"{url}/v1/chat/completions"
        payload = {
            "messages": history,
            "temperature": 0.7,
            "max_tokens": 4096,
            "model": self.model
        }

        try:
            response = requests.post(route_start, json=payload, timeout=30)
            if response.status_code != 200:
                raise Exception(f"LM Studio returned status {response.status_code}: {response.text}")
            if not response.text.strip():
                raise Exception("LM Studio returned empty response")
            try:
                result = response.json()
            except ValueError as json_err:
                raise Exception(f"Invalid JSON from LM Studio: {response.text[:200]}") from json_err

            if verbose:
                print("Response from LM Studio:", result)
            choices = result.get("choices", [])
            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:

View on GitHub (pinned to ae57a23577)

Solutions

  1. Inspect the 200-char body in the message — HTML indicates you're hitting the wrong endpoint or a portal
  2. Verify route_start points at the OpenAI-compatible endpoint (http://<host>:1234/v1/chat/completions)
  3. Print the full raw response (curl the same URL) to see the complete non-JSON payload
  4. Restart LM Studio server and retry to rule out a truncated response
  5. Update LM Studio; ensure Content-Type application/json is sent (the library does this)

Example fix

// before
url = "http://localhost:1234"  # web UI, returns HTML
// after
url = "http://localhost:1234/v1/chat/completions"
Defensive patterns

Strategy: type-guard

Validate before calling

def assert_chat_completions_url(url):
    assert url.rstrip('/').endswith('/v1/chat/completions'), f"'{url}' is not the chat completions endpoint"

Type guard

def is_json_object(text: str):
    import json
    try:
        obj = json.loads(text)
        return obj if isinstance(obj, dict) else None
    except json.JSONDecodeError:
        return None

Try / catch

try:
    content = provider.lm_studio_fn(history)
except Exception as e:
    if "Invalid JSON from LM Studio" in str(e):
        body = str(e).split(": ", 1)[-1]
        if body.lstrip().startswith("<"):
            fix_endpoint_url()  # HTML => wrong endpoint or portal
        else:
            restart_server_and_retry()
    else:
        raise

Prevention

When it happens

Trigger: response.json() raises ValueError: body is HTML (server error page), truncated streaming output, or binary/garbage instead of the expected OpenAI-compatible JSON.

Common situations: An intermediate proxy or auth captive portal returning HTML with 200; LM Studio serving a partial body after crash; pointing route_start at a wrong endpoint (e.g. the web UI) instead of /v1/chat/completions.

Understand the failure class

Background: "Invalid JSON response" and "Failed to parse response" errors: when an API answers 200 but the body isn't the JSON your library expected — this error's family across 28 libraries.

Related errors


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