elastic/elasticsearch · error · XContentParseException

[{}] cannot parse field [{}] with value type [{}]

Error message

[{}] cannot parse field [{}] with value type [{}]

What it means

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.

Source

Thrown at libs/x-content/src/main/java/org/elasticsearch/xcontent/ObjectParser.java:132

    /**
     * Defines how to consume a parsed undefined field
     */
    public interface UnknownFieldConsumer<Value> {
        void accept(Value target, String field, Object value);
    }

    private static <Value, Context> UnknownFieldParser<Value, Context> consumeUnknownField(UnknownFieldConsumer<Value> consumer) {
        return (objectParser, field, location, parser, value, context) -> {
            XContentParser.Token t = parser.currentToken();
            switch (t) {
                case VALUE_STRING -> consumer.accept(value, field, parser.text());
                case VALUE_NUMBER -> consumer.accept(value, field, parser.numberValue());
                case VALUE_BOOLEAN -> consumer.accept(value, field, parser.booleanValue());
                case VALUE_NULL -> consumer.accept(value, field, null);
                case START_OBJECT -> consumer.accept(value, field, parser.map());
                case START_ARRAY -> consumer.accept(value, field, parser.list());
                default -> throw new XContentParseException(
                    parser.getTokenLocation(),
                    "[" + objectParser.name + "] cannot parse field [" + field + "] with value type [" + t + "]"
                );
            }
        };
    }

    private static <Value, Category, Context> UnknownFieldParser<Value, Context> unknownIsNamedXContent(
        Class<Category> categoryClass,
        BiConsumer<Value, ? super Category> consumer
    ) {
        return (objectParser, field, location, parser, value, context) -> {
            Category o;
            try {
                o = parser.namedObject(categoryClass, field, context);
            } catch (NamedObjectNotFoundException e) {
                Set<String> candidates = new HashSet<>(
                    objectParser.fieldParserMap.getOrDefault(parser.getRestApiVersion(), Collections.emptyMap()).keySet()

View on GitHub (pinned to db6a809a66)

Solutions

  1. Verify the input is well-formed JSON and the unknown field carries a value/object/array, not a bare field name.
  2. Audit any custom field parsers for cursor-mismanagement that could leave the consumer reading a FIELD_NAME or END token.
  3. If you need to tolerate arbitrary unknown structures, handle the default case in your own UnknownFieldParser (e.g. parser.skipChildren()) instead of throwing.
Defensive patterns

Strategy: try-catch

Validate before calling

// ensure unknown fields carry a consumable scalar/object/array token
Token t = parser.currentToken();
if (t != Token.VALUE_STRING && t != Token.VALUE_NUMBER && t != Token.VALUE_BOOLEAN
    && t != Token.VALUE_NULL && t != Token.START_OBJECT && t != Token.START_ARRAY) {
    parser.skipChildren(); // tolerate unexpected structure instead of throwing
}

Try / catch

try {
    op.parse(parser, value, ctx);
} catch (XContentParseException e) {
    if (e.getMessage().contains("cannot parse field [") && e.getMessage().contains("with value type [")) {
        // log the offending token; sanitize input or relax the unknown-field handler
    }
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of elastic/elasticsearch@db6a809a66 (2026-08-12). Data as JSON: /api/errors/27b90a46fbe08542. Report an issue: GitHub.