apache/beam · error · RuntimeException

Failed to serialize %s value: %s

Error message

Failed to serialize %s value: %s

What it means

AsJsons<InputT> converts pipeline elements to JSON strings via a Jackson ObjectMapper. If writeValue throws IOException during serialization inside the DoFn, it is wrapped in a RuntimeException naming the input class and value, failing the element (and by default the pipeline).

Source

Thrown at sdks/java/extensions/jackson/src/main/java/org/apache/beam/sdk/extensions/jackson/AsJsons.java:159

    return new AsJsonsWithFailures<>(exceptionHandler, exceptionHandler.getOutputTypeDescriptor());
  }

  private String writeValue(InputT input) throws JsonProcessingException {
    ObjectMapper mapper = Optional.ofNullable(customMapper).orElse(DEFAULT_MAPPER);
    return mapper.writeValueAsString(input);
  }

  @Override
  public PCollection<String> expand(PCollection<InputT> input) {
    return input.apply(
        MapElements.via(
            new SimpleFunction<InputT, String>() {
              @Override
              public String apply(InputT input) {
                try {
                  return writeValue(input);
                } catch (IOException e) {
                  throw new RuntimeException(
                      "Failed to serialize " + inputClass.getName() + " value: " + input, e);
                }
              }
            }));
  }

  /** A {@code PTransform} that adds exception handling to {@link AsJsons}. */
  public class AsJsonsWithFailures<FailureT>
      extends PTransform<PCollection<InputT>, WithFailures.Result<PCollection<String>, FailureT>> {

    private @Nullable InferableFunction<WithFailures.ExceptionElement<InputT>, FailureT>
        exceptionHandler;

    private final transient @Nullable TypeDescriptor<FailureT> failureType;

    AsJsonsWithFailures(
        InferableFunction<WithFailures.ExceptionElement<InputT>, FailureT> exceptionHandler,
        TypeDescriptor<FailureT> failureType) {

View on GitHub (pinned to 12126d8942)

Solutions

  1. Fix the input class so it is Jackson-serializable (add/adjust getters, @JsonProperty, remove circular refs)
  2. Register needed modules on the ObjectMapper, e.g. jackson-datatype-jsr310 (via withObjectMapper)
  3. Replace unserializable fields with serializable representations before the AsJsons transform

Example fix

// before
AsJsons.of(Event.class) // Event has a java.time.Instant field, no JSR310 module
// after
new ObjectMapper().registerModule(new JavaTimeModule());
AsJsons.<Event>withObjectMapper(mapper).apply(events);
Defensive patterns

Strategy: try-catch

Validate before calling

try { mapper.writeValueAsString(sample); } catch (IOException e) { throw new IllegalArgumentException("Type not Jackson-serializable", e); }

Try / catch

try { result = AsJsons.of(Event.class).apply(elements); } catch (RuntimeException e) { if (e.getMessage().startsWith("Failed to serialize")) { /* fix POJO or configure mapper, re-run */ } else throw e; }

Prevention

When it happens

Trigger: Applying AsJsons.of(TypeT) to an object graph Jackson cannot serialize: unserializable types, self-referencing structures causing infinite recursion, or a custom serializer throwing IOException.

Common situations: POJOs with non-standard types (InputStream, DateTime without JSR310 module), circular object references, or map keys of non-string types.

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/beam@12126d8942 (2026-09-13). Data as JSON: /api/errors/5204bca41be6517e. Report an issue: GitHub.