{"record":{"id":"27b90a46fbe08542","repo":"elastic/elasticsearch","slug":"cannot-parse-field-with-value-type","errorCode":null,"errorMessage":"[{}] cannot parse field [{}] with value type [{}]","messagePattern":"\\[(.+?)\\] cannot parse field \\[(.+?)\\] with value type \\[(.+?)\\]","errorType":"exception","errorClass":"XContentParseException","httpStatus":null,"severity":"error","filePath":"libs/x-content/src/main/java/org/elasticsearch/xcontent/ObjectParser.java","lineNumber":132,"sourceCode":"\n    /**\n     * Defines how to consume a parsed undefined field\n     */\n    public interface UnknownFieldConsumer<Value> {\n        void accept(Value target, String field, Object value);\n    }\n\n    private static <Value, Context> UnknownFieldParser<Value, Context> consumeUnknownField(UnknownFieldConsumer<Value> consumer) {\n        return (objectParser, field, location, parser, value, context) -> {\n            XContentParser.Token t = parser.currentToken();\n            switch (t) {\n                case VALUE_STRING -> consumer.accept(value, field, parser.text());\n                case VALUE_NUMBER -> consumer.accept(value, field, parser.numberValue());\n                case VALUE_BOOLEAN -> consumer.accept(value, field, parser.booleanValue());\n                case VALUE_NULL -> consumer.accept(value, field, null);\n                case START_OBJECT -> consumer.accept(value, field, parser.map());\n                case START_ARRAY -> consumer.accept(value, field, parser.list());\n                default -> throw new XContentParseException(\n                    parser.getTokenLocation(),\n                    \"[\" + objectParser.name + \"] cannot parse field [\" + field + \"] with value type [\" + t + \"]\"\n                );\n            }\n        };\n    }\n\n    private static <Value, Category, Context> UnknownFieldParser<Value, Context> unknownIsNamedXContent(\n        Class<Category> categoryClass,\n        BiConsumer<Value, ? super Category> consumer\n    ) {\n        return (objectParser, field, location, parser, value, context) -> {\n            Category o;\n            try {\n                o = parser.namedObject(categoryClass, field, context);\n            } catch (NamedObjectNotFoundException e) {\n                Set<String> candidates = new HashSet<>(\n                    objectParser.fieldParserMap.getOrDefault(parser.getRestApiVersion(), Collections.emptyMap()).keySet()","sourceCodeStart":114,"sourceCodeEnd":150,"githubUrl":"https://github.com/elastic/elasticsearch/blob/db6a809a667c081ca1dc7500389d26975573215f/libs/x-content/src/main/java/org/elasticsearch/xcontent/ObjectParser.java#L114-L150","documentation":"When ObjectParser is configured to consume unknown fields via a custom UnknownFieldConsumer (consumeUnknownField), it switches on the current token to extract a value. Only VALUE_STRING, VALUE_NUMBER, VALUE_BOOLEAN, VALUE_NULL, START_OBJECT, START_ARRAY are handled; any other token (FIELD_NAME, END_OBJECT, END_ARRAY) falls to default and throws. This indicates the unknown field's structure is not a simple scalar/object/array at the position the consumer expected.","triggerScenarios":"An UnknownFieldConsumer-based parser encounters an unknown field positioned on an unexpected token, typically because the parser cursor is mid-object (FIELD_NAME) or at a structural boundary. Can arise from a misbehaving custom field declaration that advances the cursor, or genuinely malformed input where an unknown field starts with a structural token.","commonSituations":"Custom ObjectParser subclasses using declareUnknownFieldHandler with a consumer that assumes scalar/array/object shapes but receives a FIELD_NAME. Bugs where a declared field's parser leaves the cursor in the wrong position. Rare with well-formed input and correct declarations.","solutions":["Verify the input is well-formed JSON and the unknown field carries a value/object/array, not a bare field name.","Audit any custom field parsers for cursor-mismanagement that could leave the consumer reading a FIELD_NAME or END token.","If you need to tolerate arbitrary unknown structures, handle the default case in your own UnknownFieldParser (e.g. parser.skipChildren()) instead of throwing."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"// ensure unknown fields carry a consumable scalar/object/array token\nToken t = parser.currentToken();\nif (t != Token.VALUE_STRING && t != Token.VALUE_NUMBER && t != Token.VALUE_BOOLEAN\n    && t != Token.VALUE_NULL && t != Token.START_OBJECT && t != Token.START_ARRAY) {\n    parser.skipChildren(); // tolerate unexpected structure instead of throwing\n}","typeGuard":null,"tryCatchPattern":"try {\n    op.parse(parser, value, ctx);\n} catch (XContentParseException e) {\n    if (e.getMessage().contains(\"cannot parse field [\") && e.getMessage().contains(\"with value type [\")) {\n        // log the offending token; sanitize input or relax the unknown-field handler\n    }\n}","preventionTips":["Ensure declared field parsers return with the cursor correctly positioned after their value.","When using a custom UnknownFieldConsumer, handle the default token case (e.g. skipChildren) to avoid hard failure.","Validate JSON structure (balanced objects/arrays) before parsing."],"tags":["xcontent","object-parser","unknown-field","token-mismatch"],"backgroundTag":null,"analyzedSha":"db6a809a667c081ca1dc7500389d26975573215f","analyzedAt":"2026-08-12T01:39:14.192Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}