restsharp/RestSharp · error · DeserializationException
Error occured while deserializing the response
Error message
Error occured while deserializing the response
What it means
When ThrowOnDeserializationError is enabled and deserializing the response body into T throws, RestSharp wraps the original exception in a DeserializationException (message 'Error occured while deserializing the response'). Without that option, the error is captured on the response (ResponseStatus.Error, response.ErrorException) and not thrown. With ThrowOnAnyError, the original exception propagates directly instead.
Source
Thrown at src/RestSharp/Serializers/RestSerializers.cs:52
internal async ValueTask<RestResponse<T>> Deserialize<T>(RestRequest request, RestResponse raw, ReadOnlyRestClientOptions options, CancellationToken cancellationToken) {
var response = RestResponse<T>.FromResponse(raw);
try {
await OnBeforeDeserialization(raw, cancellationToken).ConfigureAwait(false);
#pragma warning disable CS0618 // Type or member is obsolete
request.OnBeforeDeserialization?.Invoke(raw);
#pragma warning restore CS0618 // Type or member is obsolete
response.Data = DeserializeContent<T>(raw);
}
catch (Exception ex) {
if (options.ThrowOnAnyError) throw;
if (options.FailOnDeserializationError || options.ThrowOnDeserializationError) response.ResponseStatus = ResponseStatus.Error;
response.AddException(ex);
if (options.ThrowOnDeserializationError) throw new DeserializationException(response, ex);
}
return response;
}
static async ValueTask OnBeforeDeserialization(RestResponse response, CancellationToken cancellationToken) {
if (response.Request.Interceptors == null) return;
foreach (var interceptor in response.Request.Interceptors) {
await interceptor.BeforeDeserialization(response, cancellationToken).ConfigureAwait(false);
}
}
/// <summary>
/// Deserialize the response content into the specified type
/// </summary>
/// <param name="response">Response instance</param>
/// <typeparam name="T">Deserialized model type</typeparam>View on GitHub (pinned to 6a50821692)
Solutions
- Inspect the DeserializationException.InnerException and the raw response.Content to find the deserialization failure cause.
- Verify the target type T is serializable (parameterless constructor, public settable properties, compatible types).
- If the server can return errors, first call ExecuteAsync (non-generic) and check IsSuccessful/StatusCode before deserializing, or handle the exception.
- If you prefer non-throwing behavior, leave ThrowOnDeserializationError false and inspect response.ErrorException / ResponseStatus instead.
- Register a deserializer that supports the response's actual content type.
Example fix
// before
var resp = await client.ExecuteAsync<MyModel>(req); // throws DeserializationException
// after (option A: inspect raw first)
var raw = await client.ExecuteAsync(req);
if (!raw.IsSuccessful) HandleError(raw);
else {
var model = client.Serializers.DeserializeContent<MyModel>(raw);
}
// after (option B: tolerant options)
var options = new RestClientOptions(url) { ThrowOnDeserializationError = false }; Defensive patterns
Strategy: try-catch
Validate before calling
// Validate response shape before deserializing when content is untrusted
var raw = await client.ExecuteAsync(req);
if (!raw.IsSuccessful || string.IsNullOrWhiteSpace(raw.Content)) return;
if (raw.ContentType?.Contains("json") is false) return; // not the expected type Try / catch
try {
return await client.ExecuteAsync<T>(request, ct);
}
catch (DeserializationException ex) {
logger.LogError(ex.InnerException, "Failed to deserialize: {Content}", ex.Response.Content);
throw;
} Prevention
- Confirm the target type T has a parameterless constructor and serializable properties.
- Check IsSuccessful and Content/ContentType before deserializing untrusted responses.
- Set ThrowOnDeserializationError=false and inspect response.ErrorException if you prefer non-throwing flow.
- Keep the registered deserializer matched to the server's content type.
When it happens
Trigger: Calling ExecuteAsync<T> with options.ThrowOnDeserializationError = true where the response content cannot be deserialized into T (schema mismatch, malformed JSON/XML, wrong content type, circular references, missing parameterless constructor).
Common situations: API returning an error page (HTML) instead of expected JSON; response schema changed; target type T lacks a parameterless constructor or has non-serializable members; content type not matching any registered deserializer.
Related errors
- Unable to find a serializer for {dataFormat}
- Response content is null
- The type must contain a public, parameterless constructor.
- If the type implements IEnumerable<T>, then it must contain
- Response content is null
AI-assisted analysis of restsharp/RestSharp@6a50821692 (2026-08-13).
Data as JSON: /api/errors/a37ce09a2428de60.
Report an issue: GitHub.