{"id":"9a7654913c9d6220","repo":"gohugoio/hugo","slug":"failed-to-convert-t-to-a-string-slice","errorCode":null,"errorMessage":"failed to convert %T to a string slice","messagePattern":"failed to convert %T to a string slice","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"common/types/convert.go","lineNumber":81,"sourceCode":"\t\treturn result, nil\n\t}\n\n\t// Probably []int or similar. Fall back to reflect.\n\tvv := reflect.ValueOf(v)\n\n\tswitch vv.Kind() {\n\tcase reflect.Slice, reflect.Array:\n\t\tresult = make([]string, vv.Len())\n\t\tfor i := range vv.Len() {\n\t\t\ts, err := cast.ToStringE(vv.Index(i).Interface())\n\t\t\tif err != nil {\n\t\t\t\treturn nil, err\n\t\t\t}\n\t\t\tresult[i] = s\n\t\t}\n\t\treturn result, nil\n\tdefault:\n\t\treturn nil, fmt.Errorf(\"failed to convert %T to a string slice\", v)\n\t}\n}\n\n// TypeToString converts v to a string if it's a valid string type.\n// Note that this will not try to convert numeric values etc.,\n// use ToString for that.\nfunc TypeToString(v any) (string, bool) {\n\tswitch s := v.(type) {\n\tcase string:\n\t\treturn s, true\n\tcase template.HTML:\n\t\treturn string(s), true\n\tcase template.CSS:\n\t\treturn string(s), true\n\tcase template.HTMLAttr:\n\t\treturn string(s), true\n\tcase template.JS:\n\t\treturn string(s), true","sourceCodeStart":63,"sourceCodeEnd":99,"githubUrl":"https://github.com/gohugoio/hugo/blob/8a468df065a75c1c7cf9f6850f32148746590ea5/common/types/convert.go#L63-L99","documentation":"`types.ToStringSlicePreserveStringE` converts a value to `[]string`, wrapping single strings, delegating to `cast.ToStringSliceE`, and finally reflecting over slices/arrays element-by-element. If the value is neither a string nor any slice/array kind (e.g. a map, number, or struct), the reflect fallback hits its default case and returns this error naming the concrete Go type.","triggerScenarios":"Config fields or template function arguments that must be a string or list of strings — e.g. taxonomy terms, `keywords`, cascade/permalink lists, `where`/`in` inputs — receiving a map or scalar of the wrong shape; also slices containing elements that can't stringify (the inner cast error propagates).","commonSituations":"YAML front matter where `tags: foo: bar` (a map) was written instead of `tags: [foo, bar]`; passing a page or dict object where a list of strings is expected in a template; config merging producing a table where an array was expected.","solutions":["Change the value to a string or an array of strings in the config/front matter identified by the printed type (e.g. map[string]interface{} means you wrote a table/map).","In templates, check the argument you pass — use `slice \"a\" \"b\"` or a field that is actually a string list.","Validate front matter of recently edited content: `hugo --printPathWarnings` and check the file named in the surrounding error context."],"exampleFix":"# before (front matter)\ntags:\n  name: go\n\n# after\ntags:\n  - go","handlingStrategy":"type-guard","validationCode":"if _, ok := v.([]string); !ok {\n    if _, ok := v.([]any); !ok {\n        return fmt.Errorf(\"expected string slice, got %T\", v)\n    }\n}","typeGuard":"func toStringSlice(v any) ([]string, bool) {\n    switch t := v.(type) {\n    case []string:\n        return t, true\n    case []any:\n        out := make([]string, 0, len(t))\n        for _, e := range t {\n            s, ok := e.(string)\n            if !ok { return nil, false }\n            out = append(out, s)\n        }\n        return out, true\n    case string:\n        return []string{t}, true\n    }\n    return nil, false\n}","tryCatchPattern":null,"preventionTips":["In config/front matter, write list values as actual arrays, not comma-joined strings or mixed-type lists","Keep list elements homogeneous strings — a stray number or map in the list breaks conversion","Normalize scalars to single-element slices at the boundary if callers may pass either"],"tags":["hugo","type-conversion","front-matter","templates"],"analyzedSha":"8a468df065a75c1c7cf9f6850f32148746590ea5","analyzedAt":"2026-07-31T21:23:07.045Z","schemaVersion":2}