{"record":{"id":"8345e8c103da5f4e","repo":"charmbracelet/crush","slug":"failed-to-update-preferred-model-status-code-d","errorCode":null,"errorMessage":"failed to update preferred model: status code %d","messagePattern":"failed to update preferred model: status code (.+?)","errorType":"error_code","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/client/config.go","lineNumber":59,"sourceCode":"\tif rsp.StatusCode != http.StatusOK {\n\t\treturn fmt.Errorf(\"failed to remove config field: status code %d\", rsp.StatusCode)\n\t}\n\treturn nil\n}\n\n// UpdatePreferredModel updates the preferred model on the server.\nfunc (c *Client) UpdatePreferredModel(ctx context.Context, id string, scope config.Scope, modelType config.SelectedModelType, model config.SelectedModel) error {\n\trsp, err := c.post(ctx, fmt.Sprintf(\"/workspaces/%s/config/model\", id), nil, jsonBody(struct {\n\t\tScope     config.Scope             `json:\"scope\"`\n\t\tModelType config.SelectedModelType `json:\"model_type\"`\n\t\tModel     config.SelectedModel     `json:\"model\"`\n\t}{Scope: scope, ModelType: modelType, Model: model}), http.Header{\"Content-Type\": []string{\"application/json\"}})\n\tif err != nil {\n\t\treturn fmt.Errorf(\"failed to update preferred model: %w\", err)\n\t}\n\tdefer rsp.Body.Close()\n\tif rsp.StatusCode != http.StatusOK {\n\t\treturn fmt.Errorf(\"failed to update preferred model: status code %d\", rsp.StatusCode)\n\t}\n\treturn nil\n}\n\n// SetCompactMode sets compact mode on the server.\nfunc (c *Client) SetCompactMode(ctx context.Context, id string, scope config.Scope, enabled bool) error {\n\trsp, err := c.post(ctx, fmt.Sprintf(\"/workspaces/%s/config/compact\", id), nil, jsonBody(struct {\n\t\tScope   config.Scope `json:\"scope\"`\n\t\tEnabled bool         `json:\"enabled\"`\n\t}{Scope: scope, Enabled: enabled}), http.Header{\"Content-Type\": []string{\"application/json\"}})\n\tif err != nil {\n\t\treturn fmt.Errorf(\"failed to set compact mode: %w\", err)\n\t}\n\tdefer rsp.Body.Close()\n\tif rsp.StatusCode != http.StatusOK {\n\t\treturn fmt.Errorf(\"failed to set compact mode: status code %d\", rsp.StatusCode)\n\t}\n\treturn nil","sourceCodeStart":41,"sourceCodeEnd":77,"githubUrl":"https://github.com/charmbracelet/crush/blob/7944b8e52225d8805e31eacbf7ef24856b0dfb7a/internal/client/config.go#L41-L77","documentation":"UpdatePreferredModel's server-rejection path: the server responded with a non-200 status. The status code is embedded in the error message.","triggerScenarios":"POST /workspaces/{id}/config/model returns non-200, e.g. 400 when the SelectedModel payload is invalid or the model type is unknown to the server.","commonSituations":"Client sends a model the server/provider catalog does not know; stale server not recognizing newer model types; workspace gone.","solutions":["Check the status code and server logs to identify the rejected payload","Validate the model id and model type against the provider catalog before calling","Update the server binary if it predates the model type being set"],"exampleFix":null,"handlingStrategy":"validation","validationCode":"// Ensure the model exists in the provider catalog first\ncatalog, err := config.Providers()\nif err != nil {\n    return err\n}\nif !catalog.HasModel(modelType, model.ID) {\n    return fmt.Errorf(\"unknown model %q for type %s\", model.ID, modelType)\n}","typeGuard":null,"tryCatchPattern":"if err := c.UpdatePreferredModel(ctx, id, scope, modelType, model); err != nil {\n    if strings.Contains(err.Error(), \"status code 400\") {\n        return fmt.Errorf(\"server rejected model payload: %w\", err)\n    }\n    return err\n}","preventionTips":["Validate model ids/types against the catalog before updating","Keep client and server model catalogs in sync","Surface the embedded status code to users for diagnosis"],"tags":["http","model-selection","bad-request"],"backgroundTag":"http-non-200-response","analyzedSha":"7944b8e52225d8805e31eacbf7ef24856b0dfb7a","analyzedAt":"2026-08-29T12:48:59.079Z","schemaVersion":2},"datasetVersion":"2026-08-29T17:17:51.833Z"}