{"record":{"id":"dd252174142d9fa4","repo":"elastic/elasticsearch","slug":"can-t-write-raw-bytes-whose-xcontent-type-can-t-be","errorCode":null,"errorMessage":"Can't write raw bytes whose xcontent-type can't be guessed","messagePattern":"Can't write raw bytes whose xcontent-type can't be guessed","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"libs/x-content/impl/src/main/java/org/elasticsearch/xcontent/provider/json/JsonXContentGenerator.java","lineNumber":474,"sourceCode":"\n    public void writeEndRaw() {\n        assert base != null : \"JsonGenerator should be of instance GeneratorBase but was: \" + generator.getClass();\n        if (base != null) {\n            JsonStreamContext context = base.getOutputContext();\n            assert (context instanceof JsonWriteContext) : \"Expected an instance of JsonWriteContext but was: \" + context.getClass();\n            ((JsonWriteContext) context).writeValue();\n        }\n    }\n\n    @Override\n    public void writeRawField(String name, InputStream content) throws IOException {\n        if (content.markSupported() == false) {\n            // needed for the XContentFactory.xContentType call\n            content = new BufferedInputStream(content);\n        }\n        XContentType contentType = XContentFactory.xContentType(content);\n        if (contentType == null) {\n            throw new IllegalArgumentException(\"Can't write raw bytes whose xcontent-type can't be guessed\");\n        }\n        writeRawField(name, content, contentType);\n    }\n\n    @Override\n    public void writeRawField(String name, InputStream content, XContentType contentType) throws IOException {\n        if (mayWriteRawData(contentType) == false) {\n            try (XContentParser parser = XContentFactory.xContent(contentType).createParser(XContentParserConfiguration.EMPTY, content)) {\n                parser.nextToken();\n                writeFieldName(name);\n                copyCurrentStructure(parser);\n            }\n        } else {\n            writeStartRaw(name);\n            flush();\n            Streams.copy(content, os);\n            writeEndRaw();\n        }","sourceCodeStart":456,"sourceCodeEnd":492,"githubUrl":"https://github.com/elastic/elasticsearch/blob/db6a809a667c081ca1dc7500389d26975573215f/libs/x-content/impl/src/main/java/org/elasticsearch/xcontent/provider/json/JsonXContentGenerator.java#L456-L492","documentation":"writeRawField(name, content) buffers the input and asks XContentFactory.xContentType(content) to sniff the format by magic bytes / structure. If sniffing returns null (the bytes do not look like JSON, Smile, YAML, or CBOR), it throws IllegalArgumentException because the generator cannot route the bytes to the correct sub-format.","triggerScenarios":"Calling jsonGenerator.writeRawField(name, inputStream) where the stream contents are not parseable as any known x-content type. The stream must support mark/reset (it is wrapped in BufferedInputStream if not).","commonSituations":"Passing plain text, base64, or arbitrary binary as a raw field; passing JSON with the BOM or whitespace stripped such that sniffing fails; the InputStream is empty or already drained; mixing x-content types when the generator only supports JSON (the JSON generator delegates non-JSON content through a parser copy, but it still needs to recognise the source type).","solutions":["Use the explicit-type overload writeRawField(name, content, XContentType) and pass the type you know","Ensure the bytes are valid JSON (or another supported x-content format) before calling the sniffing overload","Do not call writeRawField with non-x-content binary; use writeBinary instead"],"exampleFix":"// before\ngenerator.writeRawField(\"payload\", new ByteArrayInputStream(bytes)); // type unknown -> throws\n// after\ngenerator.writeRawField(\"payload\", new ByteArrayInputStream(bytes), XContentType.JSON);","handlingStrategy":"validation","validationCode":"XContentType type = XContentFactory.xContentType(content);\nif (type == null) {\n    throw new IllegalArgumentException(\"Cannot sniff xcontent type of raw field \" + name);\n}\ngenerator.writeRawField(name, content, type);","typeGuard":"static boolean isKnownXContent(InputStream in) {\n    if (!in.markSupported()) return false;\n    in.mark(1024);\n    try { return XContentFactory.xContentType(in) != null; }\n    finally { try { in.reset(); } catch (IOException ignored) {} }\n}","tryCatchPattern":"try { generator.writeRawField(name, content); }\ncatch (IllegalArgumentException e) { /* use typed overload or writeBinary */ }","preventionTips":["Prefer the typed overload when you know the format","Buffer and sniff once, then reuse","Do not feed arbitrary binary to writeRawField"],"tags":["x-content","json","serialization","input-validation"],"backgroundTag":null,"analyzedSha":"db6a809a667c081ca1dc7500389d26975573215f","analyzedAt":"2026-08-12T01:39:14.192Z","schemaVersion":2},"datasetVersion":"2026-08-12T12:31:55.035Z"}