{"record":{"id":"2ccf4f570ab1869e","repo":"apache/beam","slug":"encoding-value-v-failed-got-v","errorCode":null,"errorMessage":"encoding value %v failed, got %v","messagePattern":"encoding value (.+?) failed, got (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"sdks/go/pkg/beam/testing/teststream/teststream.go","lineNumber":129,"sourceCode":"// Element types must have built-in coders in Beam.\nfunc (c *Config) AddElements(timestamp int64, elements ...any) error {\n\tt := reflect.TypeOf(elements[0])\n\tif c.elmType == nil {\n\t\tc.elmType = typex.New(t)\n\t} else if c.elmType.Type() != t {\n\t\treturn fmt.Errorf(\"element type mismatch, previous additions were of type %v, tried to add type %v\", c.elmType, t)\n\t}\n\tfor i, ele := range elements {\n\t\tif reflect.TypeOf(ele) != c.elmType.Type() {\n\t\t\treturn fmt.Errorf(\"element %d was type %T, previous additions were of type %v\", i, ele, c.elmType)\n\t\t}\n\t}\n\tnewElements := []*pipepb.TestStreamPayload_TimestampedElement{}\n\tenc := beam.NewElementEncoder(t)\n\tfor _, e := range elements {\n\t\tvar buf bytes.Buffer\n\t\tif err := enc.Encode(e, &buf); err != nil {\n\t\t\treturn fmt.Errorf(\"encoding value %v failed, got %v\", e, err)\n\t\t}\n\t\tnewElements = append(newElements, &pipepb.TestStreamPayload_TimestampedElement{EncodedElement: buf.Bytes(), Timestamp: timestamp})\n\t}\n\taddElementsEvent := &pipepb.TestStreamPayload_Event_AddElements{Elements: newElements}\n\telementEvent := &pipepb.TestStreamPayload_Event_ElementEvent{ElementEvent: addElementsEvent}\n\tc.events = append(c.events, &pipepb.TestStreamPayload_Event{Event: elementEvent})\n\treturn nil\n}\n\n// AddElementList inserts a slice of elements into the stream at the specified event timestamp. Must be called with\n// at least one element.\n//\n// Calls into AddElements, which panics if an inserted type does not match a previously inserted element type.\n//\n// Element types must have built-in coders in Beam.\nfunc (c *Config) AddElementList(timestamp int64, elements any) error {\n\tval := reflect.ValueOf(elements)\n\tif val.Kind() != reflect.Slice && val.Kind() != reflect.Array {","sourceCodeStart":111,"sourceCodeEnd":147,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/go/pkg/beam/testing/teststream/teststream.go#L111-L147","documentation":"AddElements encodes each element with the Beam element coder before embedding it in the TestStream payload. If the coder's Encode call fails (e.g. the type has no built-in coder or the value cannot be serialized), this error wraps the encoding failure.","triggerScenarios":"Adding elements of a type without a built-in Beam coder (per the API doc: element types must have built-in coders), or a value that fails encoding (e.g. unencodable struct fields).","commonSituations":"Using custom structs, maps, or interfaces as TestStream elements; passing unsupported types like func or chan; forgetting to register a custom coder.","solutions":["Use only types with built-in Beam coders (primitives, strings, byte slices, simple structs of those)","Register a custom coder via beam.Encoder/beam.CustomCoder for the element type","Convert elements to an encodable type before adding","Inspect the wrapped %v error for the specific encoding failure"],"exampleFix":"// before\ncfg.AddElements(0, myUnregisteredStruct{...}) // no coder\n// after\ncoder := beam.NewCustomCoder(\"mystruct\", reflect.TypeOf(myStruct{}), encodeFn, decodeFn)\n// or use encodable element types\ncfg.AddElements(0, \"plain-string-element\")","handlingStrategy":"validation","validationCode":"enc := beam.NewElementEncoder(reflect.TypeOf(elems[0]))\nfor _, e := range elems {\n    var buf bytes.Buffer\n    if err := enc.Encode(e, &buf); err != nil { return err } // pre-flight\n}","typeGuard":null,"tryCatchPattern":"if err := cfg.AddElements(ts, elems...); err != nil {\n    if strings.Contains(err.Error(), \"encoding value\") {\n        t.Fatalf(\"element not encodable with built-in coder: %v\", err)\n    }\n    t.Fatal(err)\n}","preventionTips":["Restrict TestStream elements to built-in-coder types","Register a beam.CustomCoder for custom structs","Test encoding round-trip in unit tests before using types in TestStream","Avoid func/chan/interface element types"],"tags":["go","testing","teststream","encoding"],"backgroundTag":"json-marshal-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"}