{"record":{"id":"ba12378060c642be","repo":"apache/beam","slug":"a-function-must-be-provided-to-convert-the-input-type-into-a","errorCode":null,"errorMessage":"A function must be provided to convert the input type into a TableRow or GenericRecord. Use BigQueryIO.Write.withFormatFunction or BigQueryIO.Write.withAvroFormatFunction to provide a formatting function. A format function is not required if Beam schemas are used.","messagePattern":"A function must be provided to convert the input type into a TableRow or GenericRecord\\. Use BigQueryIO\\.Write\\.withFormatFunction or BigQueryIO\\.Write\\.withAvroFormatFunction to provide a formatting function\\. A format function is not required if Beam schemas are used\\.","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"sdks/java/io/google-cloud-platform/src/main/java/org/apache/beam/sdk/io/gcp/bigquery/BigQueryIO.java","lineNumber":4123,"sourceCode":"              formatFunction == null,\n              \"Only one of withFormatFunction or withAvroFormatFunction/withAvroWriter maybe set,\"\n                  + \" not both.\");\n\n          SerializableFunction<@Nullable TableSchema, org.apache.avro.Schema> avroSchemaFactory =\n              getAvroSchemaFactory();\n          if (avroSchemaFactory == null) {\n            checkArgument(\n                hasSchema,\n                \"A schema must be provided if an avroFormatFunction \"\n                    + \"is set but no avroSchemaFactory is defined.\");\n            avroSchemaFactory = DEFAULT_AVRO_SCHEMA_FACTORY;\n          }\n          rowWriterFactory = avroRowWriterFactory.prepare(dynamicDestinations, avroSchemaFactory);\n        } else if (formatFunction != null) {\n          rowWriterFactory =\n              RowWriterFactory.tableRows(formatFunction, formatRecordOnFailureFunction);\n        } else {\n          throw new IllegalArgumentException(\n              \"A function must be provided to convert the input type into a TableRow or \"\n                  + \"GenericRecord. Use BigQueryIO.Write.withFormatFunction or \"\n                  + \"BigQueryIO.Write.withAvroFormatFunction to provide a formatting function. \"\n                  + \"A format function is not required if Beam schemas are used.\");\n        }\n      } else {\n        checkArgument(\n            avroRowWriterFactory == null,\n            \"When using a formatFunction, the AvroRowWriterFactory should be null\");\n        rowWriterFactory =\n            RowWriterFactory.tableRows(\n                checkArgumentNotNull(\n                    formatFunction,\n                    \"A function must be provided to convert the input type into a TableRow or \"\n                        + \"GenericRecord. Use BigQueryIO.Write.withFormatFunction or \"\n                        + \"BigQueryIO.Write.withAvroFormatFunction to provide a formatting \"\n                        + \"function. A format function is not required if Beam schemas are \"\n                        + \"used.\"),","sourceCodeStart":4105,"sourceCodeEnd":4141,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/java/io/google-cloud-platform/src/main/java/org/apache/beam/sdk/io/gcp/bigquery/BigQueryIO.java#L4105-L4141","documentation":"BigQueryIO.Write requires a way to convert each pipeline element into a TableRow (or a GenericRecord for Avro/file loads). If the input type is not a TableRow itself and neither a format function nor an Avro format function is configured (and Beam schemas are not inferred), the write expansion throws IllegalArgumentException telling the developer to provide one.","triggerScenarios":"Calling BigQueryIO.write() (or writeTables/fileLoads variants) with an input PCollection of a custom POJO or arbitrary type without calling withFormatFunction()/withAvroFormatFunction(), without a schema-derived encoding (setSchema/fromBean methods absent), and where dynamicDestinations/avroRowWriterFactory paths were not taken.","commonSituations":"Pipelines writing custom Java classes to BigQuery where the developer assumed Beam would auto-convert; refactors that removed a format function while keeping a typed input; using the generic write() instead of schema-aware APIs.","solutions":["Add .withFormatFunction(myObj -> new TableRow().set(...)) to the BigQueryIO.Write transform.","For file/store loads of Avro records, use .withAvroFormatFunction(record -> ...) instead.","Alternatively define a Beam Schema for the input type (via @DefaultSchema / SchemaCoder) so a format function is not required.","If the input is already TableRow, verify the generic type parameter is TableRow; otherwise Beam cannot see it as such."],"exampleFix":"// before\nrows.apply(\"Write\", BigQueryIO.<MyEvent>write().to(\"proj:ds.table\"));\n// after\nrows.apply(\"Write\", BigQueryIO.<MyEvent>write().to(\"proj:ds.table\")\n    .withFormatFunction(e -> new TableRow().set(\"id\", e.getId()).set(\"ts\", e.getTs())));","handlingStrategy":"validation","validationCode":"Write<MyEvent> w = BigQueryIO.<MyEvent>write().to(tableSpec);\nif (w.getFormatFunction() == null && w.getAvroFormatFunction() == null && !hasBeamSchema(MyEvent.class)) {\n  throw new IllegalStateException(\"Provide withFormatFunction or a Beam schema before expanding\");\n}","typeGuard":"static boolean needsFormatFunction(Class<?> inputType) {\n  return !TableRow.class.isAssignableFrom(inputType) && !GenericRecord.class.isAssignableFrom(inputType);\n}","tryCatchPattern":"try {\n  rows.apply(\"WriteBQ\", writeTransform);\n} catch (IllegalArgumentException e) {\n  if (e.getMessage().startsWith(\"A function must be provided\")) {\n    throw new IllegalStateException(\"Configure withFormatFunction/withAvroFormatFunction or a Beam schema\", e);\n  } else { throw e; }\n}","preventionTips":["Always pair a typed (non-TableRow) PCollection with a format function or a registered Beam schema.","Prefer @DefaultSchema-annotated POJOs so Beam infers row conversion automatically.","Construct BigQueryIO.Write transforms in a shared factory method that enforces format-function presence."],"tags":["java","bigquery","apache-beam","missing-argument"],"backgroundTag":"missing-required-argument","analyzedSha":"12126d8942aaf848030c478b4c6a28c6af861c66","analyzedAt":"2026-09-13T01:50:10.254Z","contentChangedAt":"2026-09-13T01:50:10.254Z","schemaVersion":2},"datasetVersion":"2026-09-14T11:17:12.474Z"}