{"record":{"id":"c9c9618f2338aed3","repo":"elastic/elasticsearch","slug":"failed-to-derive-xcontent","errorCode":null,"errorMessage":"Failed to derive xcontent","messagePattern":"Failed to derive xcontent","errorType":"exception","errorClass":"XContentParseException","httpStatus":null,"severity":"error","filePath":"libs/x-content/src/main/java/org/elasticsearch/xcontent/XContentFactory.java","lineNumber":181,"sourceCode":"            if (Character.isWhitespace(c) == false) {\n                break;\n            }\n        }\n        return null;\n    }\n\n    /**\n     * Guesses the content (type) based on the provided char sequence and returns the corresponding {@link XContent}\n     *\n     * @deprecated the content type should not be guessed except for few cases where we effectively don't know the content type.\n     * The REST layer should move to reading the Content-Type header instead. There are other places where auto-detection may be needed.\n     * This method is deprecated to prevent usages of it from spreading further without specific reasons.\n     */\n    @Deprecated\n    public static XContent xContent(CharSequence content) {\n        XContentType type = xContentType(content);\n        if (type == null) {\n            throw new XContentParseException(\"Failed to derive xcontent\");\n        }\n        return xContent(type);\n    }\n\n    /**\n     * Guesses the content type based on the provided bytes and returns the corresponding {@link XContent}\n     *\n     * @deprecated the content type should not be guessed except for few cases where we effectively don't know the content type.\n     * The REST layer should move to reading the Content-Type header instead. There are other places where auto-detection may be needed.\n     * This method is deprecated to prevent usages of it from spreading further without specific reasons.\n     */\n    @Deprecated\n    public static XContent xContent(byte[] data) {\n        return xContent(data, 0, data.length);\n    }\n\n    /**\n     * Guesses the content type based on the provided bytes and returns the corresponding {@link XContent}","sourceCodeStart":163,"sourceCodeEnd":199,"githubUrl":"https://github.com/elastic/elasticsearch/blob/db6a809a667c081ca1dc7500389d26975573215f/libs/x-content/src/main/java/org/elasticsearch/xcontent/XContentFactory.java#L163-L199","documentation":"Thrown by the deprecated XContentFactory.xContent(CharSequence) when xContentType(content) returns null, meaning the character sequence does not match the signature of any known XContent format (JSON, YAML, SMILE, CBOR). The method guesses format from leading bytes/characters and fails if no format matches.","triggerScenarios":"Passing an empty string, a string with only whitespace, a string whose first non-whitespace character does not match any known format's magic bytes/leading character, or a string containing non-UTF-8 binary data (e.g., SMILE bytes passed as a CharSequence).","commonSituations":"Auto-detection on empty or whitespace-only bodies. Passing binary SMILE/CBOR content as a string (corrupting it). Receiving garbled input from a malfunctioning upstream service. Using the deprecated xContent(CharSequence) API instead of specifying the content type explicitly.","solutions":["Specify the content type explicitly instead of relying on auto-detection (pass XContentType to the parser).","Ensure the CharSequence is non-empty and starts with a valid format indicator (e.g., '{' or '[' for JSON).","If the content is binary (SMILE/CBOR), use the byte[] overload, not CharSequence.","Migrate away from the deprecated xContent(CharSequence) method; read Content-Type from the request header."],"exampleFix":"// before — deprecated auto-detection\nXContent x = XContentFactory.xContent(charSequence);\n\n// after — explicit type from header\nXContentType type = XContentType.fromMediaType(contentTypeHeader);\nXContent x = XContentFactory.xContent(type);","handlingStrategy":"validation","validationCode":"// Before calling xContent(CharSequence), verify content is non-empty and detectable\npublic static XContentType detectOrThrow(CharSequence content) {\n    if (content == null || content.length() == 0 || content.toString().isBlank()) {\n        throw new IllegalArgumentException(\"Content is empty, cannot detect type\");\n    }\n    XContentType type = XContentFactory.xContentType(content);\n    if (type == null) {\n        throw new IllegalArgumentException(\"Content does not match JSON, YAML, SMILE, or CBOR\");\n    }\n    return type;\n}","typeGuard":null,"tryCatchPattern":"try {\n    XContent x = XContentFactory.xContent(content);\n} catch (XContentParseException e) {\n    if (\"Failed to derive xcontent\".equals(e.getMessage())) {\n        // Fallback: ask caller for explicit content type\n        throw new IllegalStateException(\"Cannot detect content type; specify XContentType explicitly\", e);\n    }\n    throw e;\n}","preventionTips":["Avoid the deprecated xContent(CharSequence) API; pass the known XContentType explicitly.","Read the Content-Type header and derive XContentType from it instead of guessing.","Ensure content is non-empty and starts with a recognizable format indicator before auto-detection."],"tags":["xcontent","content-type-detection","deprecated","factory"],"backgroundTag":null,"analyzedSha":"db6a809a667c081ca1dc7500389d26975573215f","analyzedAt":"2026-08-12T01:39:14.192Z","schemaVersion":2},"datasetVersion":"2026-08-12T12:31:55.035Z"}