hyperledger/fabric · error

expected key %s to encode a value of type []byte, but was %T

Error message

expected key %s to encode a value of type []byte, but was %T

What it means

Returned by Serializer.DeserializeFieldAsBytes when the stored StateData oneof is not the Bytes variant — the key encodes a String or Int64 where []byte was expected, so the value cannot be returned as bytes.

Source

Thrown at core/chaincode/lifecycle/serializer.go:441

	}
	oneOf, ok := value.Type.(*lb.StateData_String_)
	if !ok {
		return "", errors.Errorf("expected key %s/fields/%s/%s to encode a value of type String, but was %T", namespace, name, field, value.Type)
	}
	return oneOf.String_, nil
}

func (s *Serializer) DeserializeFieldAsBytes(namespace, name, field string, state ReadableState) ([]byte, error) {
	value, err := s.DeserializeField(namespace, name, field, state)
	if err != nil {
		return nil, err
	}
	if value.Type == nil {
		return nil, nil
	}
	oneOf, ok := value.Type.(*lb.StateData_Bytes)
	if !ok {
		return nil, errors.Errorf("expected key %s to encode a value of type []byte, but was %T", FieldKey(namespace, name, field), value.Type)
	}
	return oneOf.Bytes, nil
}

func (s *Serializer) DeserializeFieldAsProto(namespace, name, field string, state ReadableState, msg proto.Message) error {
	bin, err := s.DeserializeFieldAsBytes(namespace, name, field, state)
	if err != nil {
		return err
	}
	err = proto.Unmarshal(bin, msg)
	if err != nil {
		return errors.Wrapf(err, "could not unmarshal key %s to %T", FieldKey(namespace, name, field), msg)
	}
	return nil
}

func (s *Serializer) DeserializeFieldAsInt64(namespace, name, field string, state ReadableState) (int64, error) {
	value, err := s.DeserializeField(namespace, name, field, state)

View on GitHub (pinned to 2736b63f8f)

Solutions

  1. Ensure the field was committed as a bytes value (e.g. approval or validation data)
  2. Re-commit the definition with the correctly typed field
Defensive patterns

Strategy: type-guard

When it happens

Trigger: Thrown at core/chaincode/lifecycle/serializer.go:441 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of hyperledger/fabric@2736b63f8f (2026-09-04). Data as JSON: /api/errors/9f99be3dc62d6f32. Report an issue: GitHub.