{"record":{"id":"c25c3ad857f881dc","repo":"apache/beam","slug":"requires-an-input-schema-note-that-only-row-or-user-classes","errorCode":null,"errorMessage":" requires an input Schema. Note that only Row or user classes are supported. Consider using TextIO or FileIO directly when writing primitive types","messagePattern":" requires an input Schema\\. Note that only Row or user classes are supported\\. Consider using TextIO or FileIO directly when writing primitive types","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"critical","filePath":"sdks/java/io/csv/src/main/java/org/apache/beam/sdk/io/csv/CsvIO.java","lineNumber":662,"sourceCode":"\n      abstract Write<T> autoBuild();\n\n      final Write<T> build() {\n\n        if (getCSVFormat().getHeaderComments() != null) {\n          checkArgument(\n              getCSVFormat().isCommentMarkerSet(),\n              \"CSVFormat withCommentMarker required when withHeaderComments\");\n        }\n\n        return autoBuild();\n      }\n    }\n\n    @Override\n    public WriteFilesResult<String> expand(PCollection<T> input) {\n      if (!input.hasSchema()) {\n        throw new IllegalArgumentException(\n            String.format(\n                \"%s requires an input Schema. Note that only Row or user classes are supported. Consider using TextIO or FileIO directly when writing primitive types\",\n                Write.class.getName()));\n      }\n\n      Schema schema = input.getSchema();\n\n      RowCoder rowCoder = RowCoder.of(schema);\n\n      PCollection<Row> rows =\n          input\n              .apply(\"To Rows\", MapElements.into(rows()).via(input.getToRowFunction()))\n              .setCoder(rowCoder);\n\n      CSVFormat csvFormat = buildHeaderFromSchemaIfNeeded(getCSVFormat(), schema);\n\n      TextIO.Write write = getTextIOWrite();\n","sourceCodeStart":644,"sourceCodeEnd":680,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/java/io/csv/src/main/java/org/apache/beam/sdk/io/csv/CsvIO.java#L644-L680","documentation":"CsvIO.Write's expand() requires the input PCollection to have a Beam Schema, because CSV columns are derived from schema fields. Primitive types (String, Integer, byte[]) have no schema, so writing them as CSV is ambiguous and the transform throws IllegalArgumentException, pointing users to TextIO/FileIO.","triggerScenarios":"Applying CsvIO.write(...) to a PCollection<String>, PCollection<Integer>, or any element type created without withFormatFunction/setSchema, so input.hasSchema() is false.","commonSituations":"Piping raw Strings (e.g. pre-formatted CSV lines) into CsvIO; upgrading a TextIO pipeline to CsvIO; creating a PCollection via Create.of(\"a,b\") without a schema; using a user class without registering a schema via withFormatFunction.","solutions":["Provide a schema: use CsvIO.<T>write(...).withFormatFunction(MyClass::toRow) or ensure the element class is a supported @DefaultSchema/annotated class.","For primitive types, write with TextIO.write() or FileIO instead of CsvIO.","Call input.setSchema(...) / apply a schema-transform upstream so the PCollection carries a Schema before CsvIO.write."],"exampleFix":"// before\npipeline.apply(Create.of(\"a,b,c\")).apply(CsvIO.write(path));\n// after\npipeline.apply(Create.of(rowOrUserClassElements)).apply(CsvIO.write(path));\n// or for raw text: .apply(TextIO.write().to(path));","handlingStrategy":"type-guard","validationCode":"if (!input.hasSchema()) {\n  throw new IllegalStateException(\"CsvIO.write requires a schema; use withFormatFunction or TextIO for primitives\");\n}\ninput.apply(CsvIO.write(path));","typeGuard":"boolean csvWritable(PCollection<?> input) { return input != null && input.hasSchema(); }","tryCatchPattern":"try { input.apply(CsvIO.write(path)); } catch (IllegalArgumentException e) { input.apply(TextIO.write().to(path)); }","preventionTips":["Only feed Row or schema-annotated user classes into CsvIO.write.","For raw text or primitive PCsollections, use TextIO/FileIO directly.","Check hasSchema() during pipeline assembly (graph-building time) to fail early."],"tags":["java","beam-io","csv","schema"],"backgroundTag":"schema-validation-failed","analyzedSha":"12126d8942aaf848030c478b4c6a28c6af861c66","analyzedAt":"2026-09-13T01:50:10.254Z","contentChangedAt":"2026-09-13T01:50:10.254Z","schemaVersion":2},"datasetVersion":"2026-09-14T16:17:12.679Z"}