{"record":{"id":"b903dce4d4f1abc0","repo":"spectreconsole/spectre.console","slug":"could-not-convert-input-to-a-string","errorCode":null,"errorMessage":"Could not convert input to a string","messagePattern":"Could not convert input to a string","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Spectre.Console/Internal/TypeConverterHelper.cs","lineNumber":15,"sourceCode":"namespace Spectre.Console;\n\ninternal static class TypeConverterHelper\n{\n    internal const DynamicallyAccessedMemberTypes ConverterAnnotation = DynamicallyAccessedMemberTypes.PublicParameterlessConstructor | DynamicallyAccessedMemberTypes.PublicFields;\n\n    internal static bool IsGetConverterSupported =>\n        !AppContext.TryGetSwitch(\"Spectre.Console.TypeConverterHelper.IsGetConverterSupported \", out var enabled) || enabled;\n\n    public static string ConvertToString<T>(T input)\n    {\n        var result = GetTypeConverter<T>().ConvertToInvariantString(input);\n        if (result == null)\n        {\n            throw new InvalidOperationException(\"Could not convert input to a string\");\n        }\n\n        return result;\n    }\n\n    public static bool TryConvertFromString<T>(string input, [MaybeNull] out T? result)\n    {\n        try\n        {\n            result = (T?)GetTypeConverter<T>().ConvertFromInvariantString(input);\n            return true;\n        }\n        catch\n        {\n            result = default;\n            return false;\n        }\n    }","sourceCodeStart":1,"sourceCodeEnd":33,"githubUrl":"https://github.com/spectreconsole/spectre.console/blob/0acc92fada6c42f13984e79c2b5f3d993bdfb099/src/Spectre.Console/Internal/TypeConverterHelper.cs#L1-L33","documentation":"Thrown by TypeConverterHelper.ConvertToString<T>(T input) when the TypeConverter for type T returns null from ConvertToInvariantString. This is an internal helper used by Spectre.Console to stringify choice values in prompts (e.g., TextPrompt<T> with choices). The built-in TypeDescriptor system should normally return a non-null string for most types, but custom types with misconfigured TypeConverters, or exotic types without a registered converter, can return null.","triggerScenarios":"Using a SelectionPrompt or TextPrompt with a generic type T whose TypeDescriptor.GetConverter / custom TypeConverterAttribute returns null from ConvertToInvariantString. Also possible if T is a type that has no standard string representation and the fallback TypeConverter yields null.","commonSituations":"Custom enum or record type used as a prompt choice without a working TypeConverter; third-party types whose TypeConverterAttribute points to a converter that returns null; edge cases with nullable value types or interfaces used as prompt generics; trimming/AOT scenarios where reflection-based converter discovery is suppressed.","solutions":["Provide an explicit converter or ToString override on your custom type so ConvertToInvariantString never returns null.","Decorate your type with [TypeConverter(typeof(MyConverter))] that returns a non-null string.","For prompt choices, use simpler types (string, int, enum) that have reliable built-in converters.","If the type legitimately cannot stringify, provide a custom display via the prompt's converter parameter instead of relying on TypeConverterHelper."],"exampleFix":"// before\npublic record MyId(Guid Value);\nvar choice = AnsiConsole.Prompt(\n    new SelectionPrompt<MyId>().AddChoices(ids));\n\n// after\n[TypeConverter(typeof(MyIdConverter))]\npublic record MyId(Guid Value);\n\npublic class MyIdConverter : TypeConverter\n{\n    public override object? ConvertTo(ITypeDescriptorContext? ctx, CultureInfo? c, object value, Type dest)\n        => value is MyId id ? id.Value.ToString() : null;\n}","handlingStrategy":"validation","validationCode":"// Test string conversion before using as prompt choice\nvar testString = TypeDescriptor.GetConverter(typeof(T))\n    ?.ConvertToInvariantString(sampleValue);\nif (testString is null)\n{\n    throw new InvalidOperationException(\n        $\"Type {typeof(T).Name} cannot be converted to string for prompt display.\");\n}","typeGuard":"static bool HasStringRepresentation<T>(T sample)\n{\n    try\n    {\n        var converter = TypeDescriptor.GetConverter(typeof(T));\n        return converter?.ConvertToInvariantString(sample) is not null;\n    }\n    catch\n    {\n        return false;\n    }\n}","tryCatchPattern":"try\n{\n    var prompt = new SelectionPrompt<T>().AddChoices(items);\n    return AnsiConsole.Prompt(prompt);\n}\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"convert input to a string\"))\n{\n    // Fallback: provide a custom converter to the prompt\n    var prompt = new SelectionPrompt<T>()\n        .Converter(myConverter)\n        .AddChoices(items);\n    return AnsiConsole.Prompt(prompt);\n}","preventionTips":["Use types with reliable string conversion (primitives, enums, records with ToString) as prompt generics.","Decorate custom types with [TypeConverter] that never returns null.","Override ToString on custom choice types.","Provide an explicit .Converter(lambda) to SelectionPrompt/TextPrompt to bypass TypeConverterHelper entirely."],"tags":["invalidoperationexception","typeconverter","prompts","reflection","custom-type"],"backgroundTag":null,"analyzedSha":"0acc92fada6c42f13984e79c2b5f3d993bdfb099","analyzedAt":"2026-08-13T18:27:21.983Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}