# concrete.ml.common.serialization.encoder.md

[![](https://img.shields.io/badge/-source-cccccc?style=flat-square)](https://github.com/zama-ai/concrete-ml/blob/release/1.1.x/src/concrete/ml/common/serialization/encoder.py#L0)

## module `concrete.ml.common.serialization.encoder`

Custom encoder for serialization.

### **Global Variables**

* **INFINITY**
* **USE\_SKOPS**

***

[![](https://img.shields.io/badge/-source-cccccc?style=flat-square)](https://github.com/zama-ai/concrete-ml/blob/release/1.1.x/src/concrete/ml/common/serialization/encoder.py#L32)

### function `dump_name_and_value`

```python
dump_name_and_value(name: str, value: Any, **kwargs) → Dict
```

Dump the value into a custom dict format.

**Args:**

* `name` (str): The custom name to use. This name should be unique for each type to encode, as it is used in the ConcreteDecoder class to detect the initial type and apply the proper load method to the serialized object.
* `value` (Any): The serialized value to dump.
* `**kwargs (dict)`: Additional arguments to dump.

**Returns:**

* `Dict`: The serialized custom format that includes both the serialized value and its type name.

***

[![](https://img.shields.io/badge/-source-cccccc?style=flat-square)](https://github.com/zama-ai/concrete-ml/blob/release/1.1.x/src/concrete/ml/common/serialization/encoder.py#L57)

### class `ConcreteEncoder`

Custom json encoder to handle non-native types found in serialized Concrete ML objects.

Non-native types are serialized manually and dumped in a custom dict format that stores both the serialization value of the object and its associated type name.

The name should be unique for each type, as it is used in the ConcreteDecoder class to detect the initial type and apply the proper load method to the serialized object. The serialized value is the value that was serialized manually in a native type. Additional arguments such as a numpy array's dtype are also properly serialized. If an object has an unexpected type or is not serializable, an error is thrown.

The ConcreteEncoder is only meant to encode Concrete-ML's built-in models and therefore only supports the necessary types. For example, torch.Tensor objects are not serializable using this encoder as built-in models only use numpy arrays. However, the list of supported types might expand in future releases if new models are added and need new types.

***

[![](https://img.shields.io/badge/-source-cccccc?style=flat-square)](https://github.com/zama-ai/concrete-ml/blob/release/1.1.x/src/concrete/ml/common/serialization/encoder.py#L176)

#### method `default`

```python
default(o: Any) → Any
```

Define a custom default method that enables dumping any supported serialized values.

**Arguments:**

* `o` (Any): The object to serialize.

**Returns:**

* `Any`: The serialized object. Non-native types are returned as a dict of a specific format.

**Raises:**

* `NotImplementedError`: If an FHE.Circuit, a Callable or a Generator object is given.

***

[![](https://img.shields.io/badge/-source-cccccc?style=flat-square)](https://github.com/zama-ai/concrete-ml/blob/release/1.1.x/src/concrete/ml/common/serialization/encoder.py#L75)

#### method `isinstance`

```python
isinstance(o: Any, cls: Type) → bool
```

Define a custom isinstance method.

Natively, among other types, the JSONENcoder handles integers, floating points and tuples. However, a numpy.integer (resp. numpy.floating) object is automatically casted to a built-in int (resp. float) object, without keeping their dtype information. Similarly, a tuple is casted to a list, meaning that it will then be loaded as a list, which notably does not have the uniqueness property and therefore might cause issues in complex structures such as QuantizedModule instances. This is an issue as JSONEncoder only calls its customizable `default` method at the end of the parsing. We thus need to provide this custom isinstance method in order to make the encoder avoid handling these specific types until `default` is reached (where they are properly serialized using our custom format).

**Args:**

* `o` (Any): The object to serialize.
* `cls` (Type): The type to compare the object with.

**Returns:**

* `bool`: If the object is of the given type. False if it is a numpy.floating, numpy.integer or a tuple.

***

[![](https://img.shields.io/badge/-source-cccccc?style=flat-square)](https://github.com/zama-ai/concrete-ml/blob/release/1.1.x/src/concrete/ml/common/serialization/encoder.py#L110)

#### method `iterencode`

```python
iterencode(o: Any, _one_shot: bool = False) → Generator
```

Encode the given object and yield each string representation as available.

This method overrides the JSONEncoder's native iterencode one in order to pass our custom isinstance method to the `_make_iterencode` function. More information in `isinstance`'s docstring. For simplicity, iterencode does not give the ability to use the initial `c_make_encoder` function, as it would required to override it in C.

**Args:**

* `o` (Any): The object to serialize.
* `_one_shot` (bool): This parameter is not used since the `_make_iterencode` function has been removed from the method.

**Returns:**

* `Generator`: Yield each string representation as available.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.zama.org/concrete-ml/1.1/developer-guide/api/concrete.ml.common.serialization.encoder.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
