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, errView on GitHub (pinned to 24529f1404)
Solutions
- Provide a *[]byte as the message target and decode it yourself
- Align client and server Content-Types (e.g. both application/grpc+json)
- Use *raw.Frame consistently for raw transports
- 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
- Match codecs on both ends of every call/subscription
- Decode raw bytes into structs explicitly after receiving
- Document codec choice per topic/service
- Cover codec combinations in tests
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
- failed to marshal: %v is not type of *[]byte
- failed to marshal: %v is not type of *bytes.Frame or proto.M
- failed to unmarshal: %v is not type of proto.Message
- ErrInvalidMessage
- unknown request path
AI-assisted analysis of micro/go-micro@24529f1404 (2026-09-01).
Data as JSON: /api/errors/629db775e6d2c067.
Report an issue: GitHub.