{"record":{"id":"ab7bdd93e9952a22","repo":"apache/beam","slug":"could-not-write-value-error-s","errorCode":null,"errorMessage":"Could not write value. Error: %s","messagePattern":"Could not write value\\. Error: (.+?)","errorType":"exception","errorClass":"CoderException","httpStatus":null,"severity":"error","filePath":"sdks/java/io/thrift/src/main/java/org/apache/beam/sdk/io/thrift/ThriftCoder.java","lineNumber":81,"sourceCode":"\n  /**\n   * Encodes the given value of type {@code T} onto the given output stream using provided {@link\n   * ThriftCoder#protocolFactory}.\n   *\n   * @param value {@link org.apache.thrift.TBase} to encode.\n   * @param outStream stream to output encoded value to.\n   * @throws IOException if writing to the {@code OutputStream} fails for some reason\n   */\n  @Override\n  public void encode(T value, OutputStream outStream) throws CoderException, IOException {\n    try {\n      TProtocol protocol = protocolFactory.getProtocol(new TIOStreamTransport(outStream));\n      TBase<?, ?> tBase = (TBase<?, ?>) value;\n      tBase.write(protocol);\n    } catch (TTransportException tte) {\n      throw new CoderException(\"Could not transport value. Error: \" + tte.getMessage());\n    } catch (Exception te) {\n      throw new CoderException(\"Could not write value. Error: \" + te.getMessage());\n    }\n  }\n\n  /**\n   * Decodes a value of type {@code T} from the given input stream using provided {@link\n   * ThriftCoder#protocolFactory}. Returns the decoded value.\n   *\n   * @param inStream stream of input values to be decoded\n   * @throws IOException if reading from the {@code InputStream} fails for some reason\n   * @throws CoderException if the value could not be decoded for some reason\n   * @return {@link TBase} decoded object\n   */\n  @Override\n  public T decode(InputStream inStream) throws CoderException, IOException {\n    try {\n      TProtocol protocol = protocolFactory.getProtocol(new TIOStreamTransport(inStream));\n      TBase<?, ?> value = (TBase<?, ?>) type.getDeclaredConstructor().newInstance();\n      value.read(protocol);","sourceCodeStart":63,"sourceCodeEnd":99,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/java/io/thrift/src/main/java/org/apache/beam/sdk/io/thrift/ThriftCoder.java#L63-L99","documentation":"ThriftCoder.encode serializes a TBase value to the output stream via a Thrift protocol. If value.write(protocol) throws any exception other than TTransportException, it is wrapped in a CoderException with this message. It typically means the Thrift object itself failed to serialize (bad field state, union with unset field, etc.), not a transport problem.","triggerScenarios":"Calling encode on a ThriftCoder with a TBase whose write(TProtocol) throws, e.g. TProtocolException from deeply nested or oversized data, TException subclasses thrown by custom thrift structs, or null/unset required struct fields.","commonSituations":"Pipelines serializing Thrift records to Kafka/BigQuery; a thrift class compiled against a mismatched .thrift IDL version fails to write; union fields left unset; stream closed mid-write surfacing as a generic exception.","solutions":["Inspect the wrapped exception message (the cause text after 'Error:') to identify the failing field or protocol issue","Ensure the thrift class on the classpath was generated from the same .thrift IDL used to produce the data","Validate the TBase value (required fields set, unions have exactly one field) before encoding","If it is a transport-level problem, it would be reported as 'Could not transport value' instead — check the underlying OutputStream for closure/corruption"],"exampleFix":"// before\nCoder<ThriftRecord> coder = ThriftCoder.of();\nout.write(coder.encode(record));\n// after\nif (record == null || !record.isSetRequiredField()) {\n  throw new IllegalArgumentException(\"ThriftRecord missing required field before encode\");\n}\nout.write(coder.encode(record));","handlingStrategy":"try-catch","validationCode":"if (value == null || !value.isSetRequiredField()) { throw new IllegalArgumentException(\"value not ready for thrift encoding\"); }","typeGuard":null,"tryCatchPattern":"try { bytes = coder.encode(record); } catch (CoderException e) { LOG.error(\"thrift encode failed: {}\", e.getMessage(), e); throw new UntranslatableException(\"encode\", e); }","preventionTips":["Validate required/union fields before encoding","Keep generated thrift classes in sync with the IDL on both pipeline sides","Log the CoderException cause, not just the wrapper"],"tags":["java","thrift","serialization","beam-coder"],"backgroundTag":"json-serialization-failed","analyzedSha":"12126d8942aaf848030c478b4c6a28c6af861c66","analyzedAt":"2026-09-13T01:50:10.254Z","contentChangedAt":"2026-09-13T01:50:10.254Z","schemaVersion":2},"datasetVersion":"2026-09-20T03:17:13.778Z"}