zed-industries/zed · error

Failed to connect to Ollama API: {} {}

Error message

Failed to connect to Ollama API: {} {}

What it means

Raised by stream_chat_completion (crates/ollama/src/ollama.rs:326) when the POST to {api_url}/api/chat returns a non-success HTTP status. Despite the wording, the TCP connection succeeded; Ollama (or a proxy in front of it) answered with an error status and the raw response body is appended to the message, e.g. '404 Not Found {"error":"model \"llama3\" not found"}'. The status plus body usually identify the exact cause: 404 model not present, 400 malformed request, 401/403 wrong api_key or proxy auth, or an HTML block page when api_url points at a non-Ollama server.

Source

Thrown at crates/ollama/src/ollama.rs:326

        })
        .extra_headers(extra_headers)
        .body(AsyncBody::from(serde_json::to_string(&request)?))?;

    let mut response = client.send(request).await?;
    if response.status().is_success() {
        let reader = BufReader::new(response.into_body());

        Ok(reader
            .lines()
            .map(|line| match line {
                Ok(line) => serde_json::from_str(&line).context("Unable to parse chat response"),
                Err(e) => Err(e.into()),
            })
            .boxed())
    } else {
        let mut body = String::new();
        response.body_mut().read_to_string(&mut body).await?;
        anyhow::bail!(
            "Failed to connect to Ollama API: {} {}",
            response.status(),
            body,
        );
    }
}

pub async fn get_models(
    client: &dyn HttpClient,
    api_url: &str,
    api_key: Option<&str>,
    extra_headers: &CustomHeaders,
) -> Result<Vec<LocalModelListing>> {
    let uri = format!("{api_url}/api/tags");
    let request = HttpRequest::builder()
        .method(Method::GET)
        .uri(uri)
        .header("Accept", "application/json")

View on GitHub (pinned to f4178619ac)

Solutions

  1. Run `ollama list` on the host api_url points to, then make the model name and tag in settings match exactly; `ollama pull <model>` if it is missing.
  2. Verify reachability with `curl <api_url>/api/tags` from the same machine; fix api_url (typically http://localhost:11434) or start `ollama serve`.
  3. Read the status code in the message: 404 → model/route problem, 400 → request fields, 401/403 → api_key or proxy auth, 429 → back off.
  4. Upgrade Ollama to a release that supports the /api/chat request fields you send.

Example fix

// before (settings.json)
"language_models": { "ollama": { "api_url": "http://localhost:11434", "available_models": [{ "name": "llama3" }] } }
// error: Failed to connect to Ollama API: 404 Not Found {"error":"model \"llama3\" not found"}

// after: `ollama pull llama3:8b`, then
"language_models": { "ollama": { "api_url": "http://localhost:11434", "available_models": [{ "name": "llama3:8b" }] } }
Defensive patterns

Strategy: try-catch

Validate before calling

// Rust caller: verify server + model before streaming
let models = ollama::get_models(&*client, api_url, api_key, &extra_headers).await?;
anyhow::ensure!(
    models.iter().any(|m| m.name == request.model),
    "model {} not present in `ollama list` at {api_url}",
    request.model
);

Type guard

fn is_reachable_ollama(status: StatusCode) -> bool {
    // /api/tags answering 200 means the base URL is an Ollama server
    status.is_success()
}

Try / catch

match ollama::stream_chat_completion(&*client, api_url, api_key, req, &headers).await {
    Ok(stream) => { /* consume */ }
    Err(err) => {
        let msg = err.to_string();
        if msg.starts_with("Failed to connect to Ollama API: 404") {
            // model or route missing: pull model / fix api_url
        } else if msg.starts_with("Failed to connect to Ollama API: 401")
            || msg.starts_with("Failed to connect to Ollama API: 403") {
            // api_key / proxy auth problem
        } else {
            return Err(err);
        }
    }
}

Prevention

When it happens

Trigger: Calling stream_chat_completion with a model name that is not pulled locally (404 'model ... not found'); a request body containing fields an older Ollama build rejects (400); api_url routed through a gateway/SSO that answers 401/403; api_url with a wrong path so /api/chat 404s; api_key sent to an Ollama instance that proxies to a cloud endpoint requiring auth.

Common situations: Model in Zed's Ollama settings doesn't match `ollama list` output (tag mismatch such as llama3 vs llama3:8b); 'ollama serve' running on another machine/container while api_url still says localhost; corporate proxy intercepting localhost-adjacent URLs; Ollama version predating the /api/chat streaming endpoint.

Related errors


AI-assisted analysis of zed-industries/zed@f4178619ac (2026-08-20). Data as JSON: /api/errors/6ec68e43e69d4f65. Report an issue: GitHub.