apache/beam · error
invalid types for map update
Error message
invalid types for map update: %v != %v
What it means
reflectx.UpdateMap requires both arguments to be maps of exactly the same reflect.Type so keys and values can be copied directly. It panics when updates is not a map at all, or when the base and updates map types differ (e.g. map[string]int vs map[string]string).
Solutions
- Use identical map types for base and updates (same key and value types).
- Verify the updates argument is a map before calling.
- Convert values to the base map's type and rebuild the updates map.
Example fix
// before
reflectx.UpdateMap(map[string]int{}, map[string]string{"a": "1"}) // panics
// after
reflectx.UpdateMap(map[string]int{}, map[string]int{"a": 1}) Defensive patterns
Strategy: type-guard
Validate before calling
mb, mu := reflect.TypeOf(base), reflect.TypeOf(updates)
if mb == nil || mb.Kind() != reflect.Map || mu == nil || mu.Kind() != reflect.Map || mb != mu {
return fmt.Errorf("both args must be same map type, got %v and %v", mb, mu)
} Type guard
func sameMapType(base, updates any) bool {
tb, tu := reflect.TypeOf(base), reflect.TypeOf(updates)
return tb != nil && tu != nil && tb.Kind() == reflect.Map && tb == tu
} Try / catch
func safeUpdateMap(base, updates any) (err error) {
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("UpdateMap failed: %v", r)
}
}()
reflectx.UpdateMap(base, updates)
return nil
} Prevention
- Declare base and updates with identical map types.
- Convert heterogeneous option maps to a common type before merging.
When it happens
Trigger: Calling UpdateMap(base, updates) where updates is not a map, or where base and updates have different map types (key or value type mismatch).
Common situations: Merging option maps built with different value types, or accidentally passing a non-map (e.g. a struct or slice) as updates in registry/Update helper usage.
Understand the failure class
Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.
Related errors
- element type is , want
- Incompatible func type: got func
- Invalid output type in method
- Mismatched output type in method
- Mismatched restriction type in method
AI-assisted analysis of apache/beam@12126d8942 (2026-09-13).
Data as JSON: /api/errors/a81dbe27cbf9c3b2.
Report an issue: GitHub.
Appendix: source
Thrown at sdks/go/pkg/beam/core/util/reflectx/util.go:80
}
}
// UpdateMap merges two maps of type map[K]*V, with the second overwriting values
// into the first (and mutating it). If the overwriting value is nil, the key is
// deleted.
func UpdateMap(base, updates any) {
if updates == nil {
return // ok: nop
}
if base == nil {
panic("base map cannot be nil")
}
m := reflect.ValueOf(base)
o := reflect.ValueOf(updates)
if o.Type().Kind() != reflect.Map || m.Type() != o.Type() {
panic(fmt.Sprintf("invalid types for map update: %v != %v", m.Type(), o.Type()))
}
keys := o.MapKeys()
for _, key := range keys {
val := o.MapIndex(key)
if val.IsNil() {
m.SetMapIndex(key, reflect.Value{}) // delete
} else {
m.SetMapIndex(key, val)
}
}
}
View on GitHub (pinned to 12126d8942)