github/copilot-sdk · error · JsonException

ToolBinaryResultType value cannot be null.

Error message

ToolBinaryResultType value cannot be null.

What it means

Within the ToolBinaryResultType converter, after the token-type check, reader.GetString() can return null (e.g. for certain token states); the converter treats a null string as invalid and throws this JsonException because ToolBinaryResultType cannot be constructed without a value.

Solutions

  1. Ensure the JSON contains a real, well-formed string value for the field.
  2. Re-parse from a complete, valid JSON document rather than a partial buffer.
  3. Validate payloads with a JSON schema before deserializing.
  4. Catch JsonException and rethrow with payload context.

Example fix

// before
var v = reader.GetString(); // null
// after
var v = reader.GetString() ?? throw new JsonException("missing value");
Defensive patterns

Strategy: try-catch

Validate before calling

using var doc = JsonDocument.Parse(json); // ensure full valid JSON first
_ = doc.RootElement.GetProperty("resultType").GetString() ?? throw new InvalidDataException("resultType missing");

Try / catch

try { result = JsonSerializer.Deserialize<ToolResult>(json); }
catch (JsonException ex) { throw new InvalidDataException("malformed ToolBinaryResultType payload", ex); }

Prevention

When it happens

Trigger: reader.GetString() returns null during deserialization of a ToolBinaryResultType — practically when the string token carries no usable value or the reader is in an unexpected state.

Common situations: Corrupted or truncated JSON; a custom pipeline reusing a Utf8JsonReader at an unexpected position; exotic null-like string tokens.

Understand the failure class

Background: "failed to unmarshal" / json.Unmarshal errors: why parsing a response into a Go struct fails and how to fix it — this error's family across 23 libraries.

Related errors


AI-assisted analysis of github/copilot-sdk@cd8cf15dc3 (2026-09-09). Data as JSON: /api/errors/42c162b46d42a33d. Report an issue: GitHub.

Appendix: source

Thrown at dotnet/src/Types.cs:692

    /// <inheritdoc/>
    public override string ToString() => Value;

    /// <summary>Provides a <see cref="JsonConverter{ToolBinaryResultType}"/> for serializing <see cref="ToolBinaryResultType"/> instances.</summary>
    [EditorBrowsable(EditorBrowsableState.Never)]
    public sealed class Converter : JsonConverter<ToolBinaryResultType>
    {
        /// <inheritdoc/>
        public override ToolBinaryResultType Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
        {
            if (reader.TokenType != JsonTokenType.String)
            {
                throw new JsonException("Expected string for ToolBinaryResultType.");
            }

            var value = reader.GetString();
            if (value is null)
            {
                throw new JsonException("ToolBinaryResultType value cannot be null.");
            }

            return new ToolBinaryResultType(value);
        }

        /// <inheritdoc/>
        public override void Write(Utf8JsonWriter writer, ToolBinaryResultType value, JsonSerializerOptions options) =>
            writer.WriteStringValue(value.Value);
    }
}

/// <summary>
/// Represents the structured result of a tool execution.
/// </summary>
public sealed class ToolResultObject
{
    /// <summary>
    /// Text result to be consumed by the language model.

View on GitHub (pinned to cd8cf15dc3)