restsharp/RestSharp · error · InvalidOperationException

Unable to find a serializer for {dataFormat}

Error message

Unable to find a serializer for {dataFormat}

What it means

RestSerializers.GetSerializer throws InvalidOperationException when no serializer is registered for the requested DataFormat. This happens at request-build time (serializing a body) and at deserialization time when looking up a handler by DataFormat. The default configuration registers Json, Xml, and None, so requesting Csv or a custom format without registration triggers it.

Source

Thrown at src/RestSharp/Serializers/RestSerializers.cs:31

// limitations under the License.
//

using System.Collections.ObjectModel;
using RestSharp.Extensions;
using RestSharp.Serializers.Xml;

namespace RestSharp.Serializers;

public class RestSerializers(Dictionary<DataFormat, SerializerRecord> records) {
    [PublicAPI]
    public IReadOnlyDictionary<DataFormat, SerializerRecord> Serializers { get; } = new ReadOnlyDictionary<DataFormat, SerializerRecord>(records);

    public RestSerializers(SerializerConfig config) : this(config.Serializers) { }

    public IRestSerializer GetSerializer(DataFormat dataFormat)
        => Serializers.TryGetValue(dataFormat, out var value)
            ? value.GetSerializer()
            : throw new InvalidOperationException($"Unable to find a serializer for {dataFormat}");

    internal string[] GetAcceptedContentTypes() => Serializers.SelectMany(x => x.Value.AcceptedContentTypes).Distinct().ToArray();

    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;

View on GitHub (pinned to 6a50821692)

Solutions

  1. Register a serializer for the DataFormat via RestClientOptions/ConfigureSerialization (e.g. config.UseCsvHelper(), config.UseXmlSerializer(), config.UseNewtonsoftJson()).
  2. If using a custom DataFormat, add an IRestSerializer for it in the SerializerConfig.Serializers dictionary.
  3. Verify the body's DataFormat matches a registered serializer before calling ExecuteAsync.
  4. When only JSON is needed, ensure DataFormat.Json stays registered (do not clear the default config).

Example fix

// before
var client = new RestClient(options, _ => { }, _ => { }); // csv not registered
request.AddBody(obj, DataFormat.Csv); // -> GetSerializer throws

// after
var client = new RestClient(options, configureSerialization: cfg => cfg.UseCsvHelper());
request.AddBody(obj, DataFormat.Csv);
Defensive patterns

Strategy: validation

Validate before calling

if (!client.Serializers.Serializers.ContainsKey(requiredFormat))
    throw new InvalidOperationException($"No serializer registered for {requiredFormat}; register one via ConfigureSerialization");

Type guard

static bool IsFormatRegistered(RestClient client, DataFormat format) => client.Serializers.Serializers.ContainsKey(format);

Try / catch

try {
    await client.ExecuteAsync<T>(request);
}
catch (InvalidOperationException ex) when (ex.Message.Contains("Unable to find a serializer")) {
    // register the missing serializer and retry
}

Prevention

When it happens

Trigger: Adding a body with a DataFormat (e.g. DataFormat.Csv or a custom value) that was not registered via ConfigureSerialization; clearing/replacing the default SerializerConfig so only some formats remain; deserializing a response whose detected format maps to an unregistered DataFormat.

Common situations: Using the CsvHelper serializer package but forgetting to register it via UseCsvHelper(); providing a custom DataFormat enum value; upgrading and inadvertently overriding the default serializer config.

Related errors


AI-assisted analysis of restsharp/RestSharp@6a50821692 (2026-08-13). Data as JSON: /api/errors/77d074b15483c570. Report an issue: GitHub.