apache/beam · error · RuntimeException

Failed to serialize input batch

Error message

Failed to serialize input batch

What it means

OpenAIModelHandler.request() serializes input batches when building the API request (and/or parsing the response) with Jackson. A JsonProcessingException — malformed request payload construction or unexpected JSON in the response — is caught and rethrown as a RuntimeException "Failed to serialize input batch" with the original exception as cause.

Solutions

  1. Inspect the cause for the exact Jackson path/field that failed.
  2. Annotate custom InputT/OutputT classes with Jackson-compatible getters or @JsonProperty mappings.
  3. Validate that inputs contain only JSON-serializable values before batching.
  4. Verify the model actually returns valid JSON (enable strict structured outputs).

Example fix

// before
class MyInput { public MyInput(String v) { this.v = v; } private String v; }
// after
class MyInput { @JsonProperty public String getV() { return v; } private String v; }
Defensive patterns

Strategy: validation

Validate before calling

new ObjectMapper().writeValueAsString(inputs); // dry-run serialize before calling handler

Try / catch

try { results = handler.request(batch); } catch (RuntimeException e) { if (e.getCause() instanceof JsonProcessingException jpe) { LOG.error("Jackson path: {}", jpe.getPath()); } throw e; }

Prevention

When it happens

Trigger: request() called with InputT values that cannot be serialized to the expected JSON structure, or when the model returns JSON that Jackson cannot map during structured-output parsing.

Common situations: Custom input types lacking Jackson annotations or getters, non-UTF8/binary data in inputs, and model responses that are not valid JSON despite the JSON-mode request.

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/3e08b0a50e7e1e0f. Report an issue: GitHub.

Appendix: source

Thrown at sdks/java/ml/inference/openai/src/main/java/org/apache/beam/sdk/ml/inference/openai/OpenAIModelHandler.java:133

              .flatMap(content -> content.outputText().stream())
              .findFirst()
              .orElse(null);

      if (structuredOutput == null || structuredOutput.responses == null) {
        throw new RuntimeException("Model returned no structured responses");
      }

      // return PredictionResults
      return structuredOutput.responses.stream()
          .map(
              response ->
                  PredictionResult.create(
                      OpenAIModelInput.create(response.input),
                      OpenAIModelResponse.create(response.output)))
          .collect(Collectors.toList());

    } catch (JsonProcessingException e) {
      throw new RuntimeException("Failed to serialize input batch", e);
    }
  }

  /**
   * Schema class for structured output response.
   *
   * <p>Represents a single input-output pair returned by the OpenAI API.
   */
  public static class Response {
    @JsonProperty(required = true)
    @JsonPropertyDescription("The input string")
    public String input;

    @JsonProperty(required = true)
    @JsonPropertyDescription("The output string")
    public String output;
  }

View on GitHub (pinned to 12126d8942)