{"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/3a2006ef569de08368d59db5e1468aa8f407e4f8/src/EFCore.Cosmos/Query/Internal/CosmosShapedQueryCompilingExpressionVisitor.ShaperProcessingExpressionVisitor.ClientMethods.cs#L18-L54","documentation":"The shaper's client method scans the Cosmos response stream for the 'Documents' array. After locating the property it expects JsonTokenType.StartArray; any other token type throws InvalidOperationException CoreStrings.JsonReaderInvalidTokenType with the offending token. A second throw at the end of the loop fires if 'Documents' is never found.","triggerScenarios":"The Cosmos response payload does not contain a 'Documents' array where the shaper expects it — caused by an unexpected response shape (error payload, proxy rewriting the body, malformed continuation).","commonSituations":"A gateway/proxy returning an error JSON instead of the documents envelope, a Cosmos emulator bug producing a non-standard body, or a corrupted continuation token resuming mid-payload.","solutions":["Capture the raw response (Fiddler/emulator logs) and confirm the 'Documents' array is present and well-formed.","Check for upstream failures (429/503) that return a status body without 'Documents'.","Regenerate continuation tokens; do not reuse tokens across schema or partition key changes."],"exampleFix":null,"handlingStrategy":"retry","validationCode":null,"typeGuard":null,"tryCatchPattern":"try\n{\n    await foreach (var item in query.WithCancellation(ct)) { /* ... */ }\n}\ncatch (InvalidOperationException ex) when (ex.Message.StartsWith(\"Invalid token type\"))\n{\n    // log raw response via Cosmos diagnostics; re-issue or surface a domain error\n    logger.LogError(ex, \"Cosmos response shape unexpected\");\n    throw;\n}","preventionTips":["Capture Cosmos diagnostics + raw body on failure for shape analysis.","Keep SDK and EF Cosmos provider versions aligned.","Avoid proxies that rewrite the response envelope."],"tags":["cosmos","json","shaper","response-parsing"],"backgroundTag":null,"analyzedSha":"3a2006ef569de08368d59db5e1468aa8f407e4f8","analyzedAt":"2026-08-11T23:42:04.146Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}