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)
returnView on GitHub (pinned to e2d772072e)
Solutions
- Check caller logs for an aborted/cancelled request — the write failure follows the disconnect.
- Increase client/proxy timeouts if large lists are being cut off mid-response.
- If it occurs with healthy connections, verify CustomModel.ToApi() has not introduced unmarshalable fields.
- 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
- Marshal to a buffer before writing to distinguish encode vs transport failures
- Paginate large model lists so responses complete within proxy timeouts
- Set adequate client and proxy read timeouts for list endpoints
- Keep CustomModel.ToApi() JSON-marshalable
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
- Error encoding custom model: %v
- Error encoding custom provider: %v
- token exchange failed - error reading body: %s
- failed to update context: %v
- failed to download the update: %w
AI-assisted analysis of plandex-ai/plandex@e2d772072e (2026-09-05).
Data as JSON: /api/errors/8b19e8ff0033a47b.
Report an issue: GitHub.