apache/beam · error · IllegalStateException
Cannot call getToRowFunction when there is no schema
Error message
Cannot call getToRowFunction when there is no schema
What it means
PCollection.getToRowFunction() throws this IllegalStateException when the PCollection has no schema. The toRow function (T -> Row) only exists on a SchemaCoder, so the method guards with hasSchema() and fails fast rather than returning null.
Solutions
- Check pc.hasSchema() before calling
- Attach a schema with pc.setSchema(...) or pc.setRowSchema(... so a toRowFunction exists
- Use a schema-registered element type so Beam derives the toRow function automatically
Example fix
// before
SerializableFunction<T, Row> fn = pc.getToRowFunction(); // throws
// after
if (!pc.hasSchema()) {
pc = pc.setRowSchema(mySchema);
}
SerializableFunction<T, Row> fn = pc.getToRowFunction(); Defensive patterns
Strategy: validation
Validate before calling
if (!pc.hasSchema()) { pc = pc.setRowSchema(mySchema); } Type guard
boolean canConvertToRow(PCollection<?> pc) { return pc.hasSchema(); } Try / catch
try { var fn = pc.getToRowFunction(); } catch (IllegalStateException e) { pc = pc.setRowSchema(mySchema); } Prevention
- Attach schema immediately after creating the PCollection
- Use setRowSchema before Row-based IO connectors
- Avoid raw DoFn outputs when Row conversion is planned
When it happens
Trigger: Calling getToRowFunction() directly, or via toRow(), toRowFunction(), rows(), schemaEntryMapper(), or schema-based expansion helpers, on a PCollection whose coder is not a SchemaCoder.
Common situations: Converting elements to Row for BigQuery writes without first setting a schema; applying Rows.toRows-style helpers on a PCollection from Create.of or a raw DoFn output.
Understand the failure class
Background: "X is required", "must be set", "cannot be empty": the missing-required-config error family, from Vertex AI project/location to WeChat keys — this error's family across 18 libraries.
Related errors
- Cannot call getFromRowFunction when there is no schema
- Cannot call getSchema when there is no schema
- A PValue contained in
- Cannot provide a coder for a Beam Row. Please provide a…
- Collection element type cannot be null for type: " +…
AI-assisted analysis of apache/beam@12126d8942 (2026-09-13).
Data as JSON: /api/errors/90d13dc6c1ae0f40.
Report an issue: GitHub.
Appendix: source
Thrown at sdks/java/core/src/main/java/org/apache/beam/sdk/values/PCollection.java:333
}
/** Returns whether this {@link PCollection} has an attached schema. */
public boolean hasSchema() {
return coderOrFailure.coder != null && coderOrFailure.coder instanceof SchemaCoder;
}
/** Returns the attached schema. */
public Schema getSchema() {
if (!hasSchema()) {
throw new IllegalStateException("Cannot call getSchema when there is no schema");
}
return ((SchemaCoder) getCoder()).getSchema();
}
/** Returns the attached schema's toRowFunction. */
public SerializableFunction<T, Row> getToRowFunction() {
if (!hasSchema()) {
throw new IllegalStateException("Cannot call getToRowFunction when there is no schema");
}
return ((SchemaCoder<T>) getCoder()).getToRowFunction();
}
/** Returns the attached schema's fromRowFunction. */
public SerializableFunction<Row, T> getFromRowFunction() {
if (!hasSchema()) {
throw new IllegalStateException("Cannot call getFromRowFunction when there is no schema");
}
return ((SchemaCoder<T>) getCoder()).getFromRowFunction();
}
/**
* of the {@link PTransform}.
*
* @return the output of the applied {@link PTransform}
*/
public <OutputT extends POutput> OutputT apply(PTransform<? super PCollection<T>, OutputT> t) {View on GitHub (pinned to 12126d8942)