plandex-ai/plandex · error

Error encoding custom models: %v

Error message

Error encoding custom models: %v

What it means

After building the API list, ListCustomModelsHandler encodes it with json.NewEncoder(w).Encode(apiList). If encoding fails the handler returns 500 'Error encoding custom models: <err>'. Like most encode-into-ResponseWriter failures, the root cause is usually a broken client connection rather than unmarshalable data ([]*shared.CustomModel is JSON-safe).

Source

Thrown at app/server/handlers/models.go:400

		return
	}

	models, err := db.ListCustomModels(auth.OrgId)
	if err != nil {
		log.Printf("Error fetching custom models: %v\n", err)
		http.Error(w, "Failed to fetch custom models: "+err.Error(), http.StatusInternalServerError)
		return
	}

	var apiList []*shared.CustomModel
	for _, m := range models {
		apiList = append(apiList, m.ToApi())
	}

	err = json.NewEncoder(w).Encode(apiList)
	if err != nil {
		log.Printf("Error encoding custom models: %v\n", err)
		http.Error(w, fmt.Sprintf("Error encoding custom models: %v", err), http.StatusInternalServerError)
		return
	}

	log.Println("Successfully fetched custom models")
}

func GetCustomProviderHandler(w http.ResponseWriter, r *http.Request) {
	auth := Authenticate(w, r, true)
	if auth == nil {
		return
	}

	id := mux.Vars(r)["providerId"]

	res, err := db.GetCustomProvider(auth.OrgId, id)
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return

View on GitHub (pinned to e2d772072e)

Solutions

  1. Check caller logs for an aborted/cancelled request — the write failure follows the disconnect.
  2. Increase client/proxy timeouts if large lists are being cut off mid-response.
  3. If it occurs with healthy connections, verify CustomModel.ToApi() has not introduced unmarshalable fields.
  4. Consider marshaling to a buffer first so encode errors are distinguishable from transport errors.

Example fix

// before
err = json.NewEncoder(w).Encode(apiList)
if err != nil {
    http.Error(w, fmt.Sprintf("Error encoding custom models: %v", err), http.StatusInternalServerError)
}
// after
buf, err := json.Marshal(apiList)
if err != nil {
    http.Error(w, "encoding failed", http.StatusInternalServerError)
    return
}
w.Header().Set("Content-Type", "application/json")
w.Write(buf)
Defensive patterns

Strategy: try-catch

Validate before calling

// Caller: keep the connection alive and honor context cancellation
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
req, _ := http.NewRequestWithContext(ctx, "GET", listURL, nil)

Try / catch

if err := json.NewEncoder(w).Encode(apiList); err != nil {
    log.Printf("Error encoding custom models: %v", err) // likely client disconnect
    return
}

Prevention

When it happens

Trigger: GET list of custom models where the client disconnects or the connection resets while the JSON array is streamed; or writing to the ResponseWriter fails after headers were committed.

Common situations: Large model lists truncated by client timeout; proxy closes connection; user navigates away/cancels the request mid-download.

Related errors


AI-assisted analysis of plandex-ai/plandex@e2d772072e (2026-09-05). Data as JSON: /api/errors/8b19e8ff0033a47b. Report an issue: GitHub.