{"id":"83f2d38def6b3616","repo":"gohugoio/hugo","slug":"unable-to-convert-value-of-type-q-to-q-83f2d3","errorCode":null,"errorMessage":"unable to convert value of type %q to %q","messagePattern":"unable to convert value of type %q to %q","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"common/hreflect/convert.go","lineNumber":88,"sourceCode":"\nfunc ToStringValueE(v reflect.Value) (reflect.Value, error) {\n\tif v, ok := ConvertIfPossible(v, typeString); ok {\n\t\treturn v, nil\n\t}\n\treturn reflect.Value{}, errConvert(v, \"string\")\n}\n\n// ToString converts v to string if possible, panicking if not.\nfunc ToString(v reflect.Value) string {\n\tvv, err := ToStringE(v)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\treturn vv\n}\n\nfunc errConvert(v reflect.Value, s string) error {\n\treturn fmt.Errorf(\"unable to convert value of type %q to %q\", v.Type().String(), s)\n}\n\n// ConvertIfPossible tries to convert val to typ if possible.\n// This is currently only implemented for int kinds,\n// added to handle the move to a new YAML library which produces uint64 for unsigned integers.\n// We can expand on this later if needed.\n// This conversion is lossless.\n// See Issue 14079.\nfunc ConvertIfPossible(val reflect.Value, typ reflect.Type) (reflect.Value, bool) {\n\tswitch val.Kind() {\n\tcase reflect.Pointer, reflect.Interface:\n\t\tif val.IsNil() {\n\t\t\t// Return typ's zero value.\n\t\t\treturn reflect.Zero(typ), true\n\t\t}\n\t\tval = val.Elem()\n\t}\n","sourceCodeStart":70,"sourceCodeEnd":106,"githubUrl":"https://github.com/gohugoio/hugo/blob/8a468df065a75c1c7cf9f6850f32148746590ea5/common/hreflect/convert.go#L70-L106","documentation":"hreflect's errConvert is returned when ToStringValueE (and siblings) cannot convert a reflect.Value to the requested type. ConvertIfPossible only performs lossless conversions currently implemented for integer kinds (added for the new YAML library that decodes unsigned ints as uint64, see issue 14079); any value whose type can't be losslessly converted to the target — e.g. a map, slice, or float where a string is required — produces this error.","triggerScenarios":"Internal conversions where Hugo needs a specific type from a reflected value: e.g. a template or config path that requires a string but receives a map/slice/number that ConvertIfPossible can't handle, or an integer that would lose precision converting to the target type.","commonSituations":"Front matter/config values of the wrong type after the YAML library migration (uint64 vs int), passing complex values (maps, slices) where a string is expected in template function arguments, or data files supplying a number where Hugo requires a string.","solutions":["Convert the value explicitly in the template (`string`, `int`) or fix the type at the data/config source.","Check the error's quoted types to see what Hugo received vs. what it needed, and adjust the offending front matter/config key.","If it appeared after a Hugo upgrade, check the changelog around YAML decoding (issue 14079) — quote values that must be strings."],"exampleFix":"# before (front matter)\nversion: 2.0   # decoded as float\n\n# after\nversion: \"2.0\"  # string as the template expects","handlingStrategy":"type-guard","validationCode":"// Check convertibility before asking for a conversion\nif !reflect.TypeOf(v).ConvertibleTo(target) { /* handle explicitly */ }","typeGuard":"func convertibleTo(v any, target reflect.Type) bool {\n\tt := reflect.TypeOf(v)\n\treturn t != nil && (t.AssignableTo(target) || t.ConvertibleTo(target))\n}","tryCatchPattern":"out, err := hreflect.ConvertTo(v, target)\nif err != nil {\n\t// incompatible types: the error names both — fix the source data or choose a different target\n}","preventionTips":["Don't rely on implicit conversion between unrelated types (e.g. map → int); convert explicitly at the boundary.","Normalize data-file values to the expected type in the data itself rather than coercing at render time.","Type-switch on the handful of shapes you actually accept and reject the rest with a clear message."],"tags":["hugo","reflection","type-conversion","yaml"],"analyzedSha":"8a468df065a75c1c7cf9f6850f32148746590ea5","analyzedAt":"2026-07-31T21:23:07.045Z","schemaVersion":2}