SkillAgentSearch skills...

Srsly

🦉 Modern high-performance serialization utilities for Python (JSON, MessagePack, Pickle)

Install / Use

/learn @explosion/Srsly
About this skill

Quality Score

0/100

Supported Platforms

Universal

README

<a href="https://explosion.ai"><img src="https://explosion.ai/assets/img/logo.svg" width="125" height="125" align="right" /></a>

srsly: Modern high-performance serialization utilities for Python

This package bundles some of the best Python serialization libraries into one convenience package, with a high-level API that makes it easy to write code that's correct across platforms and Pythons. This allows us to provide all the serialization utilities we need in a single binary wheel. Currently supports JSON, JSONL, MessagePack, Pickle and YAML.

tests PyPi conda GitHub Python wheels

Motivation

Serialization is hard, especially across Python versions and multiple platforms. After dealing with many subtle bugs over the years (encodings, locales, large files) our libraries like spaCy and Prodigy had steadily grown a number of utility functions to wrap the multiple serialization formats we need to support (especially json, msgpack and pickle). These wrapping functions ended up duplicated across our codebases, so we wanted to put them in one place.

srsly currently includes wrappers around the following packages:

Additionally, it includes a heavily customized fork of msgpack-numpy, with corrected round-trip behaviour for np.float64 objects.

Installation

srsly can be installed from pip.

python -m pip install srsly

Or from conda via conda-forge:

conda install -c conda-forge srsly

This will automatically install/upgrade all dependencies.

numpy and cupy are optional dependencies for msgpack. If numpy is installed, numpy objects can be serialized. If cupy is installed, cupy objects will be automaticaly converted to numpy and then serialized.

Alternatively, you can also install the library from the repository:

# clone the repo
git clone https://github.com/explosion/srsly
cd srsly

# create a virtual environment
python -m venv .env
source .env/bin/activate

# install from source
python -m pip install .

For developers, install requirements separately and then install in editable mode without build isolation:

# install in editable mode
python -m pip install --no-build-isolation --editable .

# run test suite
python -m pytest --pyargs srsly

API

JSON

<kbd>function</kbd> srsly.json_dumps

Serialize an object to a JSON string. Falls back to json if sort_keys=True is used (until it's fixed in ujson).

data = {"foo": "bar", "baz": 123}
json_string = srsly.json_dumps(data)

| Argument | Type | Description | | ----------- | ---- | ------------------------------------------------------ | | data | - | The JSON-serializable data to output. | | indent | int | Number of spaces used to indent JSON. Defaults to 0. | | sort_keys | bool | Sort dictionary keys. Defaults to False. | | RETURNS | str | The serialized string. |

<kbd>function</kbd> srsly.json_loads

Deserialize unicode or bytes to a Python object.

data = '{"foo": "bar", "baz": 123}'
obj = srsly.json_loads(data)

| Argument | Type | Description | | ----------- | ----------- | ------------------------------- | | data | str / bytes | The data to deserialize. | | RETURNS | - | The deserialized Python object. |

<kbd>function</kbd> srsly.write_json

Create a JSON file and dump contents or write to standard output.

data = {"foo": "bar", "baz": 123}
srsly.write_json("/path/to/file.json", data)

| Argument | Type | Description | | -------- | ------------ | ------------------------------------------------------ | | path | str / Path | The file path or "-" to write to stdout. | | data | - | The JSON-serializable data to output. | | indent | int | Number of spaces used to indent JSON. Defaults to 2. |

<kbd>function</kbd> srsly.read_json

Load JSON from a file or standard input.

data = srsly.read_json("/path/to/file.json")

| Argument | Type | Description | | ----------- | ------------ | ------------------------------------------ | | path | str / Path | The file path or "-" to read from stdin. | | RETURNS | dict / list | The loaded JSON content. |

<kbd>function</kbd> srsly.write_gzip_json

Create a gzipped JSON file and dump contents.

data = {"foo": "bar", "baz": 123}
srsly.write_gzip_json("/path/to/file.json.gz", data)

| Argument | Type | Description | | -------- | ------------ | ------------------------------------------------------ | | path | str / Path | The file path. | | data | - | The JSON-serializable data to output. | | indent | int | Number of spaces used to indent JSON. Defaults to 2. |

<kbd>function</kbd> srsly.write_gzip_jsonl

Create a gzipped JSONL file and dump contents.

data = [{"foo": "bar"}, {"baz": 123}]
srsly.write_gzip_json("/path/to/file.jsonl.gz", data)

| Argument | Type | Description | | ----------------- | ------------ | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | path | str / Path | The file path. | | lines | - | The JSON-serializable contents of each line. | | append | bool | Whether or not to append to the location. Appending to .gz files is generally not recommended, as it doesn't allow the algorithm to take advantage of all data when compressing - files may hence be poorly compressed. | | append_new_line | bool | Whether or not to write a new line before appending to the file. |

<kbd>function</kbd> srsly.read_gzip_json

Load gzipped JSON from a file.

data = srsly.read_gzip_json("/path/to/file.json.gz")

| Argument | Type | Description | | ----------- | ------------ | ------------------------ | | path | str / Path | The file path. | | RETURNS | dict / list | The loaded JSON content. |

<kbd>function</kbd> srsly.read_gzip_jsonl

Load gzipped JSONL from a file.

data = srsly.read_gzip_jsonl("/path/to/file.jsonl.gz")

| Argument | Type | Description | | ----------- | ------------ | ------------------------- | | path | str / Path | The file path. | | RETURNS | dict / list | The loaded JSONL content. |

<kbd>function</kbd> srsly.write_jsonl

Create a JSONL file (newline-delimited JSON) and dump contents line by line, or write to standard output.

data = [{"foo": "bar"}, {"baz": 123}]
srsly.write_jsonl("/path/to/file.jsonl", data)

| Argument | Type | Description | | ----------------- | ------------ | ---------------------------------------------------------------------------------------------------------------------- | | path | str / Path | The file path or "-" to write to stdout. | | lines | iterable | The JSON-serializable lines. | | append | bool | Append to an existing file. Will open it in "a" mode and insert a newline before writing lines. Defaults to False. | | append_new_line | bool | Defines whether a new line should first be written when appending to an existing file. Defaults to True. |

<kbd>function</kbd> `srsly.read_jso

View on GitHub
GitHub Stars481
CategoryDevelopment
Updated5d ago
Forks38

Languages

Python

Security Score

100/100

Audited on Mar 23, 2026

No findings