Happened to me when I got duplicated metadata keys. How? They were written with different cases.
Lessons learned, always normalize keys, e.g.: by some toLower(string) function.