jackc/pgx · error
invalid length for CopyBothResponse.OverallFormat
Error message
invalid length for CopyBothResponse.OverallFormat
What it means
Returned by CopyBothResponse.UnmarshalJSON when the JSON `OverallFormat` field is not exactly one character long. OverallFormat is a single byte on the wire (0 = text, 1 = binary), so the JSON form must be a one-character string. An empty string, a multi-char value, or a numeric literal fails this check.
Source
Thrown at pgproto3/copy_both_response.go:89
}
// UnmarshalJSON implements encoding/json.Unmarshaler.
func (dst *CopyBothResponse) UnmarshalJSON(data []byte) error {
// Ignore null, like in the main JSON package.
if string(data) == "null" {
return nil
}
var msg struct {
OverallFormat string
ColumnFormatCodes []uint16
}
if err := json.Unmarshal(data, &msg); err != nil {
return err
}
if len(msg.OverallFormat) != 1 {
return errors.New("invalid length for CopyBothResponse.OverallFormat")
}
dst.OverallFormat = msg.OverallFormat[0]
dst.ColumnFormatCodes = msg.ColumnFormatCodes
return nil
}
View on GitHub (pinned to ec1a0befd2)
Solutions
- Use the single-character wire form: "0" for text, "1" for binary.
- Map full words before unmarshalling: `"text" -> "0"`, `"binary" -> "1"`.
- Regenerate fixtures from MarshalJSON output so the field shape matches.
- Pre-validate the JSON with a schema requiring a one-char string for OverallFormat.
Example fix
// before
raw := []byte(`{"OverallFormat":"text","ColumnFormatCodes":[0]}`)
err := json.Unmarshal(raw, &cb) // error
// after
raw := []byte(`{"OverallFormat":"0","ColumnFormatCodes":[0]}`)
err := json.Unmarshal(raw, &cb) Defensive patterns
Strategy: validation
Validate before calling
func validateCopyBothOverallFormat(raw []byte) error {
var probe struct{ OverallFormat string }
if err := json.Unmarshal(raw, &probe); err != nil { return err }
if len(probe.OverallFormat) != 1 {
return fmt.Errorf("OverallFormat must be one char (\"0\"/\"1\"), got %q", probe.OverallFormat)
}
return nil
} Type guard
func isValidCopyOverallFormat(s string) bool {
return len(s) == 1 && (s == "0" || s == "1")
} Try / catch
var cb pgproto3.CopyBothResponse
if err := json.Unmarshal(raw, &cb); err != nil {
// tell caller OverallFormat must be "0" (text) or "1" (binary)
return err
} Prevention
- Emit OverallFormat as the single wire character "0" (text) or "1" (binary).
- Generate fixtures from MarshalJSON.
- Reject numeric OverallFormat in incoming JSON and map words to codes before unmarshalling.
When it happens
Trigger: Calling `json.Unmarshal(data, ©Both)` on JSON where `OverallFormat` has length != 1 — e.g. `{"OverallFormat": "text"}` or `{"OverallFormat": 0}` (number, not string).
Common situations: Test fixtures or replay tools serialise OverallFormat as a word ("text"/"binary") or as a JSON number instead of the one-char string the unmarshaler expects. Also seen when a hand-edited fixture leaves the field empty.
Related errors
- invalid length for CopyInResponse.OverallFormat
- invalid length for CopyOutResponse.OverallFormat
- invalid length for Close.ObjectType
- too many column format codes
- invalid length for Describe.ObjectType
AI-assisted analysis of jackc/pgx@ec1a0befd2 (2026-08-04).
Data as JSON: /data/errors/7b04d912180c9234.json.
Report an issue: GitHub.