apache/iceberg · error · UncheckedIOException

Failed to serialize object

Error message

Failed to serialize object

What it means

SerializationUtil.serializeToBytes Java-serializes an object to a byte array; if ObjectOutputStream raises an IOException (non-serializable field, custom writeObject failure) it is rethrown as UncheckedIOException with this message.

Solutions

  1. Make all fields of the serialized object Serializable (e.g. use SerializableTable wrapper)
  2. Remove or mark transient fields holding streams/connections
  3. Ensure nested objects' writeObject/readObject are correct

Example fix

// before
byte[] bytes = SerializationUtil.serializeToBytes(table); // table holds open FileIO
// after
byte[] bytes = SerializationUtil.serializeToBytes(SerializableTable.copyOf(table));
Defensive patterns

Strategy: try-catch

Validate before calling

if (!(obj instanceof java.io.Serializable)) throw new IllegalArgumentException("object must be Serializable to serializeToBytes");

Type guard

boolean isSerializable(Object o) { return o instanceof java.io.Serializable; }

Try / catch

try { byte[] b = SerializationUtil.serializeToBytes(obj); } catch (UncheckedIOException e) { /* fix non-serializable field */ }

Prevention

When it happens

Trigger: Calling serializeToBytes(obj) with an object graph containing a non-Serializable member whose serialization throws IOException — e.g. wrapping a stream/connection inside a Serializable task.

Common situations: Serializing tables/functions for broadcast or distributed execution where a field holds a FileIO holding open resources; lambdas capturing non-serializable state.

Understand the failure class

Background: "JSON serialization failed", "not JSON serializable", "Failed to serialize": why JSON marshaling errors happen and how to fix them — this error's family across 46 libraries.

Related errors


AI-assisted analysis of apache/iceberg@86d9c8fc54 (2026-09-12). Data as JSON: /api/errors/a11853f05039928c. Report an issue: GitHub.

Appendix: source

Thrown at core/src/main/java/org/apache/iceberg/util/SerializationUtil.java:68

   * Serialize an object to bytes. If the object implements {@link HadoopConfigurable}, the
   * confSerializer will be used to serialize Hadoop configuration used by the object.
   *
   * @param obj object to serialize
   * @param confSerializer serializer for the Hadoop configuration
   * @return serialized bytes
   */
  public static byte[] serializeToBytes(
      Object obj, Function<Configuration, SerializableSupplier<Configuration>> confSerializer) {
    if (obj instanceof HadoopConfigurable) {
      ((HadoopConfigurable) obj).serializeConfWith(confSerializer);
    }

    try (ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(baos)) {
      oos.writeObject(obj);
      return baos.toByteArray();
    } catch (IOException e) {
      throw new UncheckedIOException("Failed to serialize object", e);
    }
  }

  @SuppressWarnings({"DangerousJavaDeserialization", "unchecked"})
  public static <T> T deserializeFromBytes(byte[] bytes) {
    if (bytes == null) {
      return null;
    }

    try (ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
        ObjectInputStream ois = new ObjectInputStream(bais)) {
      return (T) ois.readObject();
    } catch (IOException e) {
      throw new UncheckedIOException("Failed to deserialize object", e);
    } catch (ClassNotFoundException e) {
      throw new RuntimeException("Could not read object ", e);
    }
  }

View on GitHub (pinned to 86d9c8fc54)