micro/go-micro · error

failed to unmarshal: %v is not type of *[]byte

Error message

failed to unmarshal: %v is not type of *[]byte

What it means

The bytes codec Unmarshal was given a destination that is not a *[]byte. Since this codec copies the raw response bytes into the target, only a pointer to a byte slice can receive them.

Source

Thrown at client/grpc/codec.go:106

	return fmt.Errorf("failed to unmarshal: %v is not type of proto.Message", v)
}

func (protoCodec) Name() string {
	return "proto"
}

func (bytesCodec) Marshal(v interface{}) ([]byte, error) {
	b, ok := v.(*[]byte)
	if !ok {
		return nil, fmt.Errorf("failed to marshal: %v is not type of *[]byte", v)
	}
	return *b, nil
}

func (bytesCodec) Unmarshal(data []byte, v interface{}) error {
	b, ok := v.(*[]byte)
	if !ok {
		return fmt.Errorf("failed to unmarshal: %v is not type of *[]byte", v)
	}
	*b = data
	return nil
}

func (bytesCodec) Name() string {
	return "bytes"
}

func (jsonCodec) Marshal(v interface{}) ([]byte, error) {
	if b, ok := v.(*bytes.Frame); ok {
		return b.Data, nil
	}

	if pb, ok := v.(proto.Message); ok {
		bytes, err := protojson.Marshal(pb)
		if err != nil {
			return nil, err

View on GitHub (pinned to 24529f1404)

Solutions

  1. Provide a *[]byte as the message target and decode it yourself
  2. Align client and server Content-Types (e.g. both application/grpc+json)
  3. Use *raw.Frame consistently for raw transports
  4. Add a codec test in CI that round-trips a message to catch mismatches

Example fix

// before
var resp MyStruct
client.Call(ctx, req, &resp)
// after
var rawBytes []byte
client.Call(ctx, req, &rawBytes)
json.Unmarshal(rawBytes, &resp)
Defensive patterns

Strategy: type-guard

Validate before calling

if _, ok := target.(*[]byte); !ok {
    return fmt.Errorf("bytes codec response target must be *[]byte")
}

Type guard

func isBytePtr(v interface{}) bool {
    _, ok := v.(*[]byte)
    return ok
}

Try / catch

err := client.Call(ctx, req, target)
if err != nil && strings.Contains(err.Error(), "failed to unmarshal") {
    // target must be *[]byte for the bytes codec
}

Prevention

When it happens

Trigger: Subscribing or calling with the bytes codec (application/grpc+bytes) but providing a struct/map pointer (or a non-pointer []byte) as the message destination.

Common situations: Mixed codecs across client/server — server sends bytes, subscriber decodes into a struct; refactored code changed the response type but kept the bytes Content-Type.

Related errors


AI-assisted analysis of micro/go-micro@24529f1404 (2026-09-01). Data as JSON: /api/errors/629db775e6d2c067. Report an issue: GitHub.