> ## Documentation Index
> Fetch the complete documentation index at: /docs/llms.txt
> Use this file to discover all available pages before exploring further.

# serializers

# `prefect.serializers`

Serializer implementations for converting objects to bytes and bytes to objects.

All serializers are based on the `Serializer` class and include a `type` string that
allows them to be referenced without referencing the actual class. For example, you
can get often specify the `JSONSerializer` with the string "json". Some serializers
support additional settings for configuration of serialization. These are stored on
the instance so the same settings can be used to load saved objects.

All serializers must implement `dumps` and `loads` which convert objects to bytes and
bytes to an object respectively.

## Functions

### `prefect_json_object_encoder` <sup>[https://github.com/PrefectHQ/prefect/blob/main/src/prefect/serializers.py#L59](https://github.com/PrefectHQ/prefect/blob/main/src/prefect/serializers.py#L59)</sup>

```python theme={null}
prefect_json_object_encoder(obj: Any) -> Any
```

`JSONEncoder.default` for encoding objects into JSON with extended type support.

Raises a `TypeError` to fallback on other encoders on failure.

### `prefect_json_object_decoder` <sup>[https://github.com/PrefectHQ/prefect/blob/main/src/prefect/serializers.py#L83](https://github.com/PrefectHQ/prefect/blob/main/src/prefect/serializers.py#L83)</sup>

```python theme={null}
prefect_json_object_decoder(result: dict[str, Any]) -> Any
```

`JSONDecoder.object_hook` for decoding objects from JSON when previously encoded
with `prefect_json_object_encoder`

## Classes

### `Serializer` <sup>[https://github.com/PrefectHQ/prefect/blob/main/src/prefect/serializers.py#L110](https://github.com/PrefectHQ/prefect/blob/main/src/prefect/serializers.py#L110)</sup>

A serializer that can encode objects of type 'D' into bytes.

**Methods:**

#### `dumps` <sup>[https://github.com/PrefectHQ/prefect/blob/main/src/prefect/serializers.py#L152](https://github.com/PrefectHQ/prefect/blob/main/src/prefect/serializers.py#L152)</sup>

```python theme={null}
dumps(self, obj: D) -> bytes
```

Encode the object into a blob of bytes.

#### `loads` <sup>[https://github.com/PrefectHQ/prefect/blob/main/src/prefect/serializers.py#L156](https://github.com/PrefectHQ/prefect/blob/main/src/prefect/serializers.py#L156)</sup>

```python theme={null}
loads(self, blob: bytes) -> D
```

Decode the blob of bytes into an object.

### `UnknownSerializer` <sup>[https://github.com/PrefectHQ/prefect/blob/main/src/prefect/serializers.py#L168](https://github.com/PrefectHQ/prefect/blob/main/src/prefect/serializers.py#L168)</sup>

Opaque placeholder for serializers that are unavailable in the current process.

This allows persisted result metadata to be inspected without importing the custom
serializer implementation. Actual serialization work still fails when attempted.

**Methods:**

#### `dumps` <sup>[https://github.com/PrefectHQ/prefect/blob/main/src/prefect/serializers.py#L184](https://github.com/PrefectHQ/prefect/blob/main/src/prefect/serializers.py#L184)</sup>

```python theme={null}
dumps(self, obj: Any) -> bytes
```

#### `dumps` <sup>[https://github.com/PrefectHQ/prefect/blob/main/src/prefect/serializers.py#L152](https://github.com/PrefectHQ/prefect/blob/main/src/prefect/serializers.py#L152)</sup>

```python theme={null}
dumps(self, obj: D) -> bytes
```

Encode the object into a blob of bytes.

#### `loads` <sup>[https://github.com/PrefectHQ/prefect/blob/main/src/prefect/serializers.py#L190](https://github.com/PrefectHQ/prefect/blob/main/src/prefect/serializers.py#L190)</sup>

```python theme={null}
loads(self, blob: bytes) -> Any
```

#### `loads` <sup>[https://github.com/PrefectHQ/prefect/blob/main/src/prefect/serializers.py#L156](https://github.com/PrefectHQ/prefect/blob/main/src/prefect/serializers.py#L156)</sup>

```python theme={null}
loads(self, blob: bytes) -> D
```

Decode the blob of bytes into an object.

### `PickleSerializer` <sup>[https://github.com/PrefectHQ/prefect/blob/main/src/prefect/serializers.py#L197](https://github.com/PrefectHQ/prefect/blob/main/src/prefect/serializers.py#L197)</sup>

Serializes objects using the pickle protocol.

* Uses `cloudpickle` by default. See `picklelib` for using alternative libraries.
* Stores the version of the pickle library to check for compatibility during
  deserialization.
* Wraps pickles in base64 for safe transmission.

**Methods:**

#### `check_picklelib` <sup>[https://github.com/PrefectHQ/prefect/blob/main/src/prefect/serializers.py#L213](https://github.com/PrefectHQ/prefect/blob/main/src/prefect/serializers.py#L213)</sup>

```python theme={null}
check_picklelib(cls, value: str) -> str
```

#### `dumps` <sup>[https://github.com/PrefectHQ/prefect/blob/main/src/prefect/serializers.py#L216](https://github.com/PrefectHQ/prefect/blob/main/src/prefect/serializers.py#L216)</sup>

```python theme={null}
dumps(self, obj: D) -> bytes
```

#### `loads` <sup>[https://github.com/PrefectHQ/prefect/blob/main/src/prefect/serializers.py#L221](https://github.com/PrefectHQ/prefect/blob/main/src/prefect/serializers.py#L221)</sup>

```python theme={null}
loads(self, blob: bytes) -> D
```

### `JSONSerializer` <sup>[https://github.com/PrefectHQ/prefect/blob/main/src/prefect/serializers.py#L226](https://github.com/PrefectHQ/prefect/blob/main/src/prefect/serializers.py#L226)</sup>

Serializes data to JSON.

Input types must be compatible with the stdlib json library.

Wraps the `json` library to serialize to UTF-8 bytes instead of string types.

**Methods:**

#### `dumps` <sup>[https://github.com/PrefectHQ/prefect/blob/main/src/prefect/serializers.py#L270](https://github.com/PrefectHQ/prefect/blob/main/src/prefect/serializers.py#L270)</sup>

```python theme={null}
dumps(self, obj: D) -> bytes
```

#### `dumps_kwargs_cannot_contain_default` <sup>[https://github.com/PrefectHQ/prefect/blob/main/src/prefect/serializers.py#L259](https://github.com/PrefectHQ/prefect/blob/main/src/prefect/serializers.py#L259)</sup>

```python theme={null}
dumps_kwargs_cannot_contain_default(cls, value: dict[str, Any]) -> dict[str, Any]
```

#### `loads` <sup>[https://github.com/PrefectHQ/prefect/blob/main/src/prefect/serializers.py#L281](https://github.com/PrefectHQ/prefect/blob/main/src/prefect/serializers.py#L281)</sup>

```python theme={null}
loads(self, blob: bytes) -> D
```

#### `loads_kwargs_cannot_contain_object_hook` <sup>[https://github.com/PrefectHQ/prefect/blob/main/src/prefect/serializers.py#L265](https://github.com/PrefectHQ/prefect/blob/main/src/prefect/serializers.py#L265)</sup>

```python theme={null}
loads_kwargs_cannot_contain_object_hook(cls, value: dict[str, Any]) -> dict[str, Any]
```

### `CompressedSerializer` <sup>[https://github.com/PrefectHQ/prefect/blob/main/src/prefect/serializers.py#L289](https://github.com/PrefectHQ/prefect/blob/main/src/prefect/serializers.py#L289)</sup>

Wraps another serializer, compressing its output.
Uses `lzma` by default. See `compressionlib` for using alternative libraries.

**Attributes:**

* `serializer`: The serializer to use before compression.
* `compressionlib`: The import path of a compression module to use.
  Must have methods `compress(bytes) -> bytes` and `decompress(bytes) -> bytes`.
* `level`: If not null, the level of compression to pass to `compress`.

**Methods:**

#### `check_compressionlib` <sup>[https://github.com/PrefectHQ/prefect/blob/main/src/prefect/serializers.py#L311](https://github.com/PrefectHQ/prefect/blob/main/src/prefect/serializers.py#L311)</sup>

```python theme={null}
check_compressionlib(cls, value: str) -> str
```

#### `dumps` <sup>[https://github.com/PrefectHQ/prefect/blob/main/src/prefect/serializers.py#L314](https://github.com/PrefectHQ/prefect/blob/main/src/prefect/serializers.py#L314)</sup>

```python theme={null}
dumps(self, obj: D) -> bytes
```

#### `loads` <sup>[https://github.com/PrefectHQ/prefect/blob/main/src/prefect/serializers.py#L319](https://github.com/PrefectHQ/prefect/blob/main/src/prefect/serializers.py#L319)</sup>

```python theme={null}
loads(self, blob: bytes) -> D
```

#### `validate_serializer` <sup>[https://github.com/PrefectHQ/prefect/blob/main/src/prefect/serializers.py#L307](https://github.com/PrefectHQ/prefect/blob/main/src/prefect/serializers.py#L307)</sup>

```python theme={null}
validate_serializer(cls, value: Union[str, Serializer[D]]) -> Serializer[D]
```

### `CompressedPickleSerializer` <sup>[https://github.com/PrefectHQ/prefect/blob/main/src/prefect/serializers.py#L325](https://github.com/PrefectHQ/prefect/blob/main/src/prefect/serializers.py#L325)</sup>

A compressed serializer preconfigured to use the pickle serializer.

### `CompressedJSONSerializer` <sup>[https://github.com/PrefectHQ/prefect/blob/main/src/prefect/serializers.py#L335](https://github.com/PrefectHQ/prefect/blob/main/src/prefect/serializers.py#L335)</sup>

A compressed serializer preconfigured to use the json serializer.
