jackc/pgx · error
invalid length for UUID
Error message
invalid length for UUID: %v
What it means
UnmarshalJSON requires exactly 38 bytes: a 36-character hyphenated UUID enclosed in two JSON quote characters. Any other length — unquoted input, a bare 32-char UUID, missing quotes, or trailing data — trips this length guard before parseUUID is ever invoked.
Solutions
- Feed valid JSON: a quoted 36-character hyphenated UUID
- Unquote the payload yourself and use pgtype.UUID.Scan if input may lack quotes
- Validate the JSON string length before unmarshaling
Defensive patterns
Strategy: validation
When it happens
Trigger: Thrown at pgtype/uuid.go:128 when the library encounters an invalid state.
Common situations: See trigger scenarios.
AI-assisted analysis of jackc/pgx@ec1a0befd2 (2026-08-04).
Data as JSON: /api/errors/1946ff17c767a5e4.
Report an issue: GitHub.
Appendix: source
Thrown at pgtype/uuid.go:128
if !src.Valid {
return []byte("null"), nil
}
var buff bytes.Buffer
buff.WriteByte('"')
buff.WriteString(encodeUUID(src.Bytes))
buff.WriteByte('"')
return buff.Bytes(), nil
}
// UnmarshalJSON implements the [encoding/json.Unmarshaler] interface.
func (dst *UUID) UnmarshalJSON(src []byte) error {
if bytes.Equal(src, []byte("null")) {
*dst = UUID{}
return nil
}
if len(src) != 38 {
return fmt.Errorf("invalid length for UUID: %v", len(src))
}
buf, err := parseUUID(string(src[1 : len(src)-1]))
if err != nil {
return err
}
*dst = UUID{Bytes: buf, Valid: true}
return nil
}
type UUIDCodec struct{}
func (UUIDCodec) FormatSupported(format int16) bool {
return format == TextFormatCode || format == BinaryFormatCode
}
func (UUIDCodec) PreferredFormat() int16 {
return BinaryFormatCode
}View on GitHub (pinned to ec1a0befd2)