elastic/elasticsearch · error · XContentParseException

failed to build [{name}] after last required field arrived

Error message

failed to build [{name}] after last required field arrived

What it means

buildTarget invokes the builder BiFunction once all required constructor args arrive. If the builder (or any queued field consumer flushed immediately after) throws an XContentParseException, it is re-wrapped with the original location preserved and a message naming the parser. This is the 'XContentParseException-typed cause' branch; the original parse location is retained.

Source

Thrown at libs/x-content/src/main/java/org/elasticsearch/xcontent/ConstructingObjectParser.java:585

                    + "argument. If it doesn't have any it should use ObjectParser instead of ConstructingObjectParser. This is a bug "
                    + "in the parser declaration.";
            // All missing constructor arguments were optional. Just build the target and return it.
            buildTarget();
            return targetObject;
        }

        private void buildTarget() {
            try {
                targetObject = builder.apply(constructorArgs, context);
                if (queuedOrderedModeCallback != null) {
                    queuedOrderedModeCallback.accept(targetObject);
                }
                while (queuedFieldsCount > 0) {
                    queuedFieldsCount -= 1;
                    queuedFields[queuedFieldsCount].accept(targetObject);
                }
            } catch (XContentParseException e) {
                throw new XContentParseException(
                    e.getLocation(),
                    "failed to build [" + objectParser.getName() + "] after last required field arrived",
                    e
                );
            } catch (Exception e) {
                throw new XContentParseException(
                    null,
                    "Failed to build [" + objectParser.getName() + "] after last required field arrived",
                    e
                );
            }
        }
    }

    private static class ConstructorArgInfo {
        final ParseField field;
        final boolean required;

View on GitHub (pinned to db6a809a66)

Solutions

  1. Examine the cause's getLocation() and message to find the nested parse failure, then fix the offending sub-content.
  2. Ensure the builder lambda does not assume invariants the parser cannot guarantee; validate and fail with a clearer message before reaching buildTarget.
Defensive patterns

Strategy: try-catch

Try / catch

try {
    Value v = cop.apply(parser, ctx);
} catch (XContentParseException e) {
    if (e.getMessage().contains("failed to build [") && e.getCause() instanceof XContentParseException nested) {
        XContentLocation loc = nested.getLocation(); // precise nested failure point
    }
}

Prevention

When it happens

Trigger: The builder lambda internally re-parses or constructs a nested object that fails with XContentParseException (e.g. building a value object whose own construction parses further content). A queued ordered-mode callback throws an XContentParseException.

Common situations: ConstructingObjectParser whose builder delegates to another parser/registry call that fails. Builders that validate parsed sub-structures and reject them via XContentParseException.

Related errors


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