jackc/pgx · error

invalid length for Describe.ObjectType

Error message

invalid length for Describe.ObjectType

What it means

Returned by Describe.UnmarshalJSON when the JSON `ObjectType` field is not exactly one character. On the wire ObjectType is a single byte ('S' = prepared statement, 'P' = portal), so the JSON form must be a one-char string. Longer words or empty strings are rejected.

Source

Thrown at pgproto3/describe.go:74

	})
}

// UnmarshalJSON implements encoding/json.Unmarshaler.
func (dst *Describe) UnmarshalJSON(data []byte) error {
	// Ignore null, like in the main JSON package.
	if string(data) == "null" {
		return nil
	}

	var msg struct {
		ObjectType string
		Name       string
	}
	if err := json.Unmarshal(data, &msg); err != nil {
		return err
	}
	if len(msg.ObjectType) != 1 {
		return errors.New("invalid length for Describe.ObjectType")
	}

	dst.ObjectType = msg.ObjectType[0]
	dst.Name = msg.Name
	return nil
}

View on GitHub (pinned to ec1a0befd2)

Solutions

  1. Use the single-char wire form: "S" (prepared statement) or "P" (portal).
  2. Map full words before unmarshalling: `"statement" -> "S"`, `"portal" -> "P"`.
  3. Regenerate fixtures with MarshalJSON so the field shape matches.
  4. Pre-validate with a schema requiring a one-char ObjectType.

Example fix

// before
raw := []byte(`{"ObjectType":"portal","Name":"p1"}`)
err := json.Unmarshal(raw, &d) // error

// after
raw := []byte(`{"ObjectType":"P","Name":"p1"}`)
err := json.Unmarshal(raw, &d)
Defensive patterns

Strategy: validation

Validate before calling

func validateDescribeJSON(raw []byte) error {
	var probe struct{ ObjectType string }
	if err := json.Unmarshal(raw, &probe); err != nil { return err }
	if len(probe.ObjectType) != 1 {
		return fmt.Errorf("ObjectType must be a single char (\"S\" or \"P\"), got %q", probe.ObjectType)
	}
	return nil
}

Type guard

func isValidDescribeObjectType(s string) bool {
	return len(s) == 1 && (s == "S" || s == "P")
}

Try / catch

var d pgproto3.Describe
if err := json.Unmarshal(raw, &d); err != nil {
    // clarify ObjectType must be "S"/"P"
    return err
}

Prevention

When it happens

Trigger: `json.Unmarshal(data, &describe)` where `ObjectType` has length != 1 — e.g. `{"ObjectType": "portal"}` or `{"ObjectType": ""}`.

Common situations: Fixtures or replay tools serialise ObjectType as a full word ("statement"/"portal") or a numeric code. Hand-edited JSON with the field missing also triggers it.

Related errors


AI-assisted analysis of jackc/pgx@ec1a0befd2 (2026-08-04). Data as JSON: /data/errors/1974f9985a3f5d1a.json. Report an issue: GitHub.