Fosowl/agenticSeek · error · Exception

No choices in LM Studio response: {result}

Error message

No choices in LM Studio response: {result}

What it means

After parsing JSON, lm_studio_fn reads result.get('choices', []) and raises this if the list is missing or empty, embedding the full parsed result. LM Studio replied with valid JSON but not a chat-completion-shaped object.

Source

Thrown at sources/llm_provider.py:399

            "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:
            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

View on GitHub (pinned to ae57a23577)

Solutions

  1. Log the embedded `result` in the message to see what LM Studio actually returned
  2. If result contains an 'error' key, address that upstream cause (model not loaded, context overflow)
  3. Ensure route_start targets /v1/chat/completions, not /v1/completions or /v1/embeddings
  4. Confirm the model is fully loaded before sending requests
  5. Retry once — some LM Studio versions emit transient error JSON under load

Example fix

// before
client = requests.post("http://localhost:1234/v1/embeddings", json=payload)
// after
client = requests.post("http://localhost:1234/v1/chat/completions", json=payload)
Defensive patterns

Strategy: type-guard

Validate before calling

def check_lm_studio_shape(sample_fn, history):
    import json
    raw = None
    try:
        result = sample_fn(history)
    except Exception as e:
        if "No choices" in str(e):
            raise SystemExit("LM Studio not returning chat completions — check endpoint/model")

Type guard

def has_choices(result) -> bool:
    return isinstance(result, dict) and isinstance(result.get("choices"), list) and len(result["choices"]) > 0

Try / catch

try:
    content = provider.lm_studio_fn(history)
except Exception as e:
    if "No choices in LM Studio response" in str(e):
        payload = str(e).split(": ", 1)[-1]
        if '"error"' in payload:
            handle_upstream_error(payload)
        else:
            verify_endpoint_is_chat_completions()
    else:
        raise

Prevention

When it happens

Trigger: Parsed JSON lacks a non-empty 'choices' array — e.g. an OpenAI-style error object {"error": {...}}, an embeddings response, or a nonstandard server response.

Common situations: LM Studio returned a JSON error payload with status 200; the endpoint is /v1/completions (legacy shape with 'choices' sometimes missing or the model errored); misconfigured route in config.ini.

Related errors


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