elsa-workflows/elsa-core · error · Exception
Failed to convert an object of type
Error message
Failed to convert an object of type {sourceType} to {underlyingTargetType} What it means
ObjectConverter.ConvertTo uses Convert.ChangeType for scalar conversions and rethrows InvalidCastException as a generic Exception naming the source and target types. It means the source value cannot be losslessly converted to the requested primitive type.
Solutions
- Ensure the source value is convertible (e.g. int.TryParse first) to the target type
- Correct the input value or the expected property type
- Catch the exception and fall back to a default value
Example fix
// before
var n = ObjectConverter.ConvertTo<int>("abc");
// after
var n = int.TryParse("abc", out var v) ? v : 0; Defensive patterns
Strategy: validation
Validate before calling
if (value is string s && !double.TryParse(s, NumberStyles.Any, CultureInfo.InvariantCulture, out _))
throw new FormatException($"'{s}' is not numeric"); Try / catch
try { n = ObjectConverter.ConvertTo<int>(value); } catch (Exception) { n = 0; } Prevention
- Use TryParse for user/JSON-sourced scalars
- Keep workflow input types aligned with property metadata
- Use invariant culture for numeric strings
When it happens
Trigger: Converting e.g. a string "abc" to int, a DateTime to int, or a complex object to a numeric type via ConvertTo<T>.
Common situations: Workflow variable test values or inputs with wrong types compared to property types; culture/number-format issues with decimal strings; schema changes between workflow definition versions.
Understand the failure class
Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.
Related errors
- Cannot deserialize to .
- Failed to deserialize
- Invalid value type.
- Invalid target type.
- Value cannot be null. (Parameter 'type')
AI-assisted analysis of elsa-workflows/elsa-core@fe9217bdfa (2026-09-13).
Data as JSON: /api/errors/54bc75b21e24fede.
Report an issue: GitHub.
Appendix: source
Thrown at src/clients/Elsa.Api.Client/Extensions/ObjectConverter.cs:185
foreach (var item in enumerable)
{
var convertedItem = ConvertTo(item, desiredCollectionItemType);
collection.Add(convertedItem);
}
return collection;
}
}
}
try
{
return Convert.ChangeType(value, underlyingTargetType, CultureInfo.InvariantCulture);
}
catch (InvalidCastException)
{
throw new Exception($"Failed to convert an object of type {sourceType} to {underlyingTargetType}");
}
}
/// <summary>
/// Returns true if the specified type is date-like type, false otherwise.
/// </summary>
private static bool IsDateType(Type type)
{
var dateTypes = new[]
{
typeof(DateTime),
typeof(DateTimeOffset)
};
return dateTypes.Contains(type);
}
/// <summary>View on GitHub (pinned to fe9217bdfa)