nikivdev/code · error

LM Studio returned status {}: {}

Error message

LM Studio returned status {}: {}

What it means

quick_prompt sends a JSON POST to the local LM Studio server. If the HTTP response status is not a success (4xx/5xx), it bails including the status code and the response body text. Connection failures are a separate error ('failed to connect...'); this one means the server answered but rejected or failed the request.

Source

Thrown at src/lmstudio.rs:67

    let url = format!("http://localhost:{port}/v1/chat/completions");

    let body = ChatRequest {
        model: model.to_string(),
        messages: vec![ChatMessage {
            role: "user".to_string(),
            content: prompt.to_string(),
        }],
        temperature: 0.1, // Low temperature for deterministic task matching
    };

    let resp = client
        .post(&url)
        .json(&body)
        .send()
        .with_context(|| format!("failed to connect to LM Studio at localhost:{port}"))?;

    if !resp.status().is_success() {
        anyhow::bail!(
            "LM Studio returned status {}: {}",
            resp.status(),
            resp.text().unwrap_or_default()
        );
    }

    let text_body = resp.text().context("failed to read LM Studio response")?;
    let parsed: ChatResponse =
        serde_json::from_str(&text_body).context("failed to parse LM Studio response")?;

    let text = parsed
        .choices
        .first()
        .and_then(|c| c.message.as_ref())
        .map(|m| m.content.trim().to_string())
        .unwrap_or_default();

    Ok(text)

View on GitHub (pinned to a747e741ae)

Solutions

  1. Check the response body echoed in the error for the server's reason (e.g. 'model not found')
  2. Load the intended model in LM Studio and use its exact model id in the request
  3. Confirm the URL/port matches the LM Studio API server (default http://localhost:1234/v1/...) and restart/retry

Example fix

// before (model id guess)
let body = json!({"model": "llama", "prompt": prompt});
// after
let body = json!({"model": "local-model", "prompt": prompt}); // exact id from LM Studio's loaded model
Defensive patterns

Strategy: try-catch

Validate before calling

let port = 1234;
let healthy = reqwest::blocking::get(format!("http://localhost:{port}/v1/models"))
    .and_then(|r| async { r.error_for_status() })
    .is_ok();
if !healthy { eprintln!("LM Studio API not healthy on port {port}"); }

Type guard

fn is_success(resp: &reqwest::Response) -> bool { resp.status().is_success() }

Try / catch

match quick_prompt(prompt).await {
    Err(e) if e.to_string().contains("LM Studio returned status") => {
        eprintln!("Inspect status/body in the error; check the model id and that the model is loaded");
    }
    other => other?,
}

Prevention

When it happens

Trigger: Calling quick_prompt when LM Studio returns e.g. 404 (wrong path/model endpoint), 400 (malformed request body/invalid model), or 500 (server-side model load failure).

Common situations: LM Studio server running but the requested model is not loaded or the model id doesn't match; API server started on a different path/version; prompt exceeding context limits causing a 4xx/5xx; wrong port hitting a different service.

Understand the failure class

Background: "API error: {status}" and "HTTP 401/403/404/429/5xx" errors: non-2xx HTTP responses explained — this error's family across 27 libraries.

Related errors


AI-assisted analysis of nikivdev/code@a747e741ae (2026-09-01). Data as JSON: /api/errors/756243b5f4dd29ed. Report an issue: GitHub.