{"record":{"id":"1538e9e3af5bcb4a","repo":"apache/beam","slug":"s-requires-an-input-schema-note-that-only-row-or-user","errorCode":null,"errorMessage":"%s 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":"error","filePath":"sdks/java/io/json/src/main/java/org/apache/beam/sdk/io/json/JsonIO.java","lineNumber":267,"sourceCode":"    @AutoValue.Builder\n    abstract static class Builder<T> {\n\n      /**\n       * The underlying {@link FileIO.Write} that writes converted input to JSON formatted output.\n       */\n      abstract Builder<T> setTextIOWrite(TextIO.Write value);\n\n      abstract Write<T> autoBuild();\n\n      final Write<T> build() {\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      SerializableFunction<Row, String> toJsonFn =\n          JsonUtils.getRowToJsonStringsFunction(input.getSchema());\n\n      PCollection<String> json = rows.apply(\"To JSON\", MapElements.into(strings()).via(toJsonFn));","sourceCodeStart":249,"sourceCodeEnd":285,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/java/io/json/src/main/java/org/apache/beam/sdk/io/json/JsonIO.java#L249-L285","documentation":"JsonIO.Write.expand() requires the input PCollection to have a schema (via Beam's schema inference); only Row-backed or schema-annotated user classes are supported. If hasSchema() is false, it throws this IllegalArgumentException suggesting TextIO/FileIO for primitive types. JsonIO writes JSON via schema-derived serialization, so untyped PCollections cannot be handled.","triggerScenarios":"Calling JsonIO.write() on a PCollection<String>, PCollection<Map<...>>, or other primitive/untyped collection without a registered schema or TypeDescriptor.","commonSituations":"Applying JsonIO.write directly to raw JSON strings read from TextIO; using POJOs without @DefaultSchema/GetSchema annotations; passing primitive types that Beam cannot infer schemas for.","solutions":["Use TextIO.write() for PCollection<String> of raw JSON text instead of JsonIO.","Annotate your POJO with @DefaultSchema and register it via SchemaRegistry, or convert to Row via setSchema.","Apply a schema-providing transform (e.g. withTypeDescriptor/setRowSchema) before JsonIO.write."],"exampleFix":"// before\npc.apply(\"write\", JsonIO.write().to(path)); // pc is PCollection<String>\n// after\npc.apply(\"write\", TextIO.write().to(path));\n// or give the PCollection a schema:\npc.setSchema(schema); pc.apply(JsonIO.write().to(path));","handlingStrategy":"type-guard","validationCode":"if (input == null || !input.hasSchema()) {\n  // use TextIO.write() for raw strings, or set a schema first\n}","typeGuard":"boolean isSchemaBacked(PCollection<?> pc) {\n  return pc != null && pc.hasSchema();\n}","tryCatchPattern":"try { pc.apply(JsonIO.write().to(out)); }\ncatch (IllegalArgumentException e) {\n  if (e.getMessage().contains(\"requires an input Schema\")) { /* switch to TextIO or add schema */ }\n  throw e;\n}","preventionTips":["Only apply JsonIO.write to Row-backed or @DefaultSchema-annotated collections.","Use TextIO for PCollection<String> of pre-serialized JSON.","Register POJO schemas with the Beam SchemaRegistry early in pipeline setup."],"tags":["json","schema","beam"],"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"}