{"record":{"id":"d329b16465f77c20","repo":"tursodatabase/turso","slug":"unexpected-token-for-last-insert-rowid-reader-to","errorCode":null,"errorMessage":"Unexpected token for last_insert_rowid: {reader.TokenType}","messagePattern":"Unexpected token for last_insert_rowid: (.+?)","errorType":"validation","errorClass":"JsonException","httpStatus":null,"severity":"error","filePath":"bindings/dotnet/src/Turso.Serverless.Client/HranaProtocol.cs","lineNumber":355,"sourceCode":"\n    [JsonPropertyName(\"is_explain\")]\n    public bool IsExplain { get; set; }\n\n    [JsonPropertyName(\"is_readonly\")]\n    public bool IsReadonly { get; set; }\n}\n\n/// <summary>The server sends <c>last_insert_rowid</c> as either a JSON string or a number.</summary>\ninternal sealed class LongFromStringOrNumberConverter : JsonConverter<long?>\n{\n    public override long? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)\n    {\n        return reader.TokenType switch\n        {\n            JsonTokenType.Null => null,\n            JsonTokenType.String => long.Parse(reader.GetString()!, CultureInfo.InvariantCulture),\n            JsonTokenType.Number => reader.GetInt64(),\n            _ => throw new JsonException($\"Unexpected token for last_insert_rowid: {reader.TokenType}\"),\n        };\n    }\n\n    public override void Write(Utf8JsonWriter writer, long? value, JsonSerializerOptions options)\n    {\n        if (value is null)\n        {\n            writer.WriteNullValue();\n        }\n        else\n        {\n            writer.WriteNumberValue(value.Value);\n        }\n    }\n}\n","sourceCodeStart":337,"sourceCodeEnd":371,"githubUrl":"https://github.com/tursodatabase/turso/blob/244cde92a7df7f9b8b8b7a4075c35a12977e303e/bindings/dotnet/src/Turso.Serverless.Client/HranaProtocol.cs#L337-L371","documentation":"last_insert_rowid in a Hrana response may be serialized as either a JSON string or a number, so the client registers a custom JsonConverter that accepts both token types. If the server emits any other token (boolean, object, start-of-array), System.Text.Json raises this JsonException because the converter has no valid value to produce.","triggerScenarios":"The response element containing last_insert_rowid arrives as true, an object, or a truncated body; typically caused by a gateway/proxy rewriting the body, a non-Hrana endpoint returning JSON in a different shape, or a version-skewed server emitting a new representation the converter does not know.","commonSituations":"options.Url pointing at a load balancer or edge worker that mangles responses; HTML error pages parsed as JSON during outages; mixing an old client with a newer server build that changed the field shape.","solutions":["Verify options.Url points at the real Turso/libsql Hrana endpoint (libsql://... or https URL of the database)","Log the raw response body when the exception surfaces to see which token actually arrived","Update Turso.Serverless.Client to the latest version for broader converter coverage","If a reverse proxy sits in front of Turso, configure it to pass JSON bodies through unmodified"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try { var rs = await conn.ExecuteAsync(sql, cmd, timeout, ct); } catch (JsonException ex) when (ex.Message.Contains(\"last_insert_rowid\")) { log.RawResponse(); throw new TursoServerlessException(\"Server sent an unexpected last_insert_rowid payload; check endpoint URL and versions\", ex); }","preventionTips":["Pin the client package and server to compatible versions","Smoke-test the endpoint with a trivial INSERT before shipping integration code","Keep the connection URL in configuration so switching endpoints is a config change, not code"],"tags":["csharp","dotnet","json","hrana-protocol","deserialization"],"backgroundTag":"json-deserialization-unexpected-token","analyzedSha":"244cde92a7df7f9b8b8b7a4075c35a12977e303e","analyzedAt":"2026-08-20T07:02:18.389Z","contentChangedAt":"2026-08-20T07:02:18.389Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}