{"record":{"id":"8476bf4505539bd4","repo":"dotnet/efcore","slug":"invalid-token-type-tokentype","errorCode":null,"errorMessage":"Invalid token type: '{tokenType}'.","messagePattern":"Invalid token type: '(.+?)'\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/EFCore.Cosmos/Query/Internal/CosmosShapedQueryCompilingExpressionVisitor.ShaperProcessingExpressionVisitor.ClientMethods.cs","lineNumber":36,"sourceCode":"        private static readonly byte EndArrayByte = Encoding.UTF8.GetBytes(\"]\")[0];\n        private static readonly byte NextItemByte = Encoding.UTF8.GetBytes(\",\")[0];\n\n        public static ReadOnlyMemory<byte> ExtractDocuments(ReadOnlyMemory<byte> data)\n        {\n            var documentsReader = new Utf8JsonReader(data.Span);\n\n            documentsReader.Read();\n            Debug.Assert(documentsReader.TokenType == JsonTokenType.StartObject);\n\n            documentsReader.Read();\n            while (documentsReader.TokenType == JsonTokenType.PropertyName)\n            {\n                if (documentsReader.ValueTextEquals(\"Documents\"))\n                {\n                    documentsReader.Read();\n                    var token = documentsReader.TokenType;\n                    return token != JsonTokenType.StartArray\n                        ? throw new InvalidOperationException(CoreStrings.JsonReaderInvalidTokenType(token))\n                        : data[(int)documentsReader.BytesConsumed..];\n                }\n\n                documentsReader.Skip();\n                documentsReader.Read();\n            }\n\n            throw new InvalidOperationException(CoreStrings.JsonReaderInvalidTokenType(documentsReader.TokenType));\n        }\n\n        public static bool TryMaterializeNextJsonCollectionItem<T>(\n            QueryContext queryContext,\n            ReadOnlyMemory<byte> data,\n            Shaper<T> shaper,\n            int ordinal,\n            out int bytesConsumed,\n            [NotNullWhen(true)] out T? result)\n        {","sourceCodeStart":18,"sourceCodeEnd":54,"githubUrl":"https://github.com/dotnet/efcore/blob/dbf9771522148d61a2467854921bd5dc6f6e6916/src/EFCore.Cosmos/Query/Internal/CosmosShapedQueryCompilingExpressionVisitor.ShaperProcessingExpressionVisitor.ClientMethods.cs#L18-L54","documentation":"Thrown by ExtractDocuments when, after locating the 'Documents' property in the Cosmos query response JSON, the token immediately following is not a StartArray. The shaper expects the Documents value to be a JSON array; any other token type (object, string, null) indicates a malformed or unexpected response payload and aborts document extraction.","triggerScenarios":"The Cosmos response payload's 'Documents' node is not an array (e.g. an object or scalar), which can happen with non-standard server responses, gateway proxy rewriting, or a malformed response. This is a shaper-level deserialization guard.","commonSituations":"Custom response interceptors/proxies that reshape the payload. Emulator or gateway bugs returning unexpected JSON. Version mismatch between EF Core Cosmos and the Cosmos SDK response contract.","solutions":["Ensure no middleware/proxy rewrites the Cosmos response JSON before EF reads it.","Update the EF Core Cosmos provider and Azure Cosmos SDK to matching, supported versions.","If using a gateway or emulator, verify it returns the standard { \"Documents\": [...] } shape.","Capture the raw response to confirm the Documents token type and report a bug if non-standard."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try\n{\n    return await query.ToListAsync();\n}\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"Invalid token type\"))\n{\n    logger.LogError(ex, \"Malformed Cosmos response payload\");\n    throw;\n}","preventionTips":["Keep EF Core Cosmos and Cosmos SDK versions in sync and supported.","Do not place response-rewriting proxies between the SDK and EF Core.","Capture raw responses in staging to diagnose malformed envelopes."],"tags":["cosmos","json","shaper","deserialization","response"],"analyzedSha":"dbf9771522148d61a2467854921bd5dc6f6e6916","analyzedAt":"2026-08-06T20:46:03.226Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}