router-for-me/CLIProxyAPI · error

parse home models payload: %w

Error message

parse home models payload: %w

What it means

decodeHomeModels failed to unmarshal the payload into map[string][]map[string]any — the body is non-empty but is not valid JSON, or is valid JSON of an incompatible shape (e.g. an object, a string, or a top-level array). The %w wraps the encoding/json error identifying the exact offset and reason.

Source

Thrown at internal/api/server_routes.go:975

func unmarshalHomeModelsTopLevel(raw []byte) (map[string]json.RawMessage, bool) {
	if len(raw) == 0 {
		return nil, false
	}
	var top map[string]json.RawMessage
	if errUnmarshal := json.Unmarshal(raw, &top); errUnmarshal != nil {
		return nil, false
	}
	return top, true
}

func decodeHomeModels(raw []byte) ([]homeModelEntry, error) {
	if len(raw) == 0 {
		return nil, fmt.Errorf("home models payload is empty")
	}

	var bySection map[string][]map[string]any
	if err := json.Unmarshal(raw, &bySection); err != nil {
		return nil, fmt.Errorf("parse home models payload: %w", err)
	}
	if len(bySection) == 0 {
		return nil, fmt.Errorf("home models payload has no sections")
	}

	seen := make(map[string]struct{})
	out := make([]homeModelEntry, 0, 256)
	for _, models := range bySection {
		for _, model := range models {
			id, _ := model["id"].(string)
			id = strings.TrimSpace(id)
			if id == "" {
				name, _ := model["name"].(string)
				name = strings.TrimSpace(name)
				id = strings.TrimPrefix(name, "models/")
			}
			if id == "" {
				continue

View on GitHub (pinned to 78f0c4079e)

Solutions

  1. Log the first few hundred bytes of the raw payload to see what was actually returned
  2. If HTML was returned, fix the authentication/session for the home models fetch
  3. If the schema changed, update decodeHomeModels to match the new section structure
  4. Add a Content-Type: application/json check on the fetch response before decoding

Example fix

// before
if err := json.Unmarshal(raw, &bySection); err != nil {
	return nil, fmt.Errorf("parse home models payload: %w", err)
}

// after
if err := json.Unmarshal(raw, &bySection); err != nil {
	return nil, fmt.Errorf("parse home models payload: %w (body prefix: %.200q)", err, raw)
}
Defensive patterns

Strategy: validation

Validate before calling

if !json.Valid(raw) {
	return fmt.Errorf("home models payload is not valid JSON (prefix: %.100q)", raw)
}
entries, err := decodeHomeModels(raw)

Try / catch

var syntaxErr *json.SyntaxError
if errors.As(err, &syntaxErr) {
	// payload corruption or HTML page — refetch rather than retry decode
}

Prevention

When it happens

Trigger: Upstream home models endpoint returned HTML (login page, proxy error page) instead of JSON; partial/truncated response; a different schema version where sections are objects rather than arrays.

Common situations: Auth expiry causing a 200-redirect to an HTML login page; corporate proxy injecting an error page; upstream schema migration changing section structure.

Related errors


AI-assisted analysis of router-for-me/CLIProxyAPI@78f0c4079e (2026-08-15). Data as JSON: /api/errors/04d2b8bdecb29e47. Report an issue: GitHub.