Batch Messages

Warning

The BATCH message functionality is currently in preview and is subject to change. You can open an issue or join the discussion on GitHub to provide feedback during the preview period.

The Singer message specification defines the three basic types of messages: RECORD, STATE, and SCHEMA. The RECORD message is used to send data from the tap to the target. The STATE message is used to send state data from the tap to the target. The SCHEMA message is used to send schema data from the tap to the target, and for example, create tables with the correct column types.

However, the Singer specification can be extended to support additional types of messages. For example, the ACTIVATE_VERSION message is used to manage hard deletes in the target.

This library’s implementation of the BATCH message is used to send records in bulk from the tap to the target, using an intermediate filesystem to store batch files. This is useful, for example

  • when the tap outputs records at a much higher rate than the target can consume them, creating backpressure

  • when the source system can directly export data in bulk (e.g. a database dump)

Currently only a local filesystem or AWS S3 are supported, but other filesystems like FTP, etc. could be supported in the future.

The BATCH Message

Local

{
  "type": "BATCH",
  "stream": "users",
  "encoding": {
    "format": "jsonl",
    "compression": "gzip"
  },
  "manifest": [
    "file://path/to/batch/file/1",
    "file://path/to/batch/file/2"
  ]
}

AWS S3

{
  "type": "BATCH",
  "stream": "users",
  "encoding": {
    "format": "jsonl",
    "compression": "gzip"
  },
  "manifest": [
    "s3://path/to/batch/file/1",
    "s3://path/to/batch/file/2"
  ]
}

encoding

The encoding field is used to specify the format and compression of the batch files. Currently the supported formats are: jsonl, parquet and arrow. GZip (gzip) compression is supported for JSONL and Parquet files.

JSONL Batch File Format

When using the jsonl encoding format, batch files should contain raw JSON records only, with one record per line. They should NOT contain Singer protocol RECORD messages.

Correct format (raw JSON records):

{"id": 1, "name": "Alice", "email": "alice@example.com"}
{"id": 2, "name": "Bob", "email": "bob@example.com"}
{"id": 3, "name": "Charlie", "email": "charlie@example.com"}

Incorrect format (Singer RECORD messages):

{"type": "RECORD", "stream": "users", "record": {"id": 1, "name": "Alice", "email": "alice@example.com"}}
{"type": "RECORD", "stream": "users", "record": {"id": 2, "name": "Bob", "email": "bob@example.com"}}
{"type": "RECORD", "stream": "users", "record": {"id": 3, "name": "Charlie", "email": "charlie@example.com"}}

The Singer protocol messages (like RECORD, SCHEMA, STATE, and BATCH) are used for communication between taps and targets, but the batch files themselves contain only the record data.

manifest

The manifest field is used to specify the paths to the batch files. The paths are relative to the root directory specified in the batch_config storage configuration.

Batch configuration

When local storage is used, targets do no require special configuration to process BATCH messages. Use of AWS S3 assumes S3/AWS credentials are already discoverable via the underlying S3 libraries (AWS_ACCESS_KEY_ID / AWS_SECRET_ACCESS_KEY / AWS_DEFAULT_REGION)

Taps may be configured to specify a root storage root directory, file path prefix, and encoding for batch files using a configuration like the below:

In config.json:

{
  // ...
  "batch_config": {
    "encoding": {
      "format": "jsonl",
      "compression": "gzip",
    },
    "storage": {
      "root": "file://tests/core/resources",
      "prefix": "test-batch-",
    }
  }
}

prefix is prepended directly to each generated batch filename, e.g. prefix: "test-batch-" produces filenames like test-batch-<stream>-<uuid>-1.jsonl.gz. It is not a subdirectory, and no path separator is inserted between prefix and the filename.

Default storage location

If storage.root is left unset (or explicitly set to "file://", its schema default), batch files are written to a singer-sdk subdirectory of the OS temporary directory (as returned by Python’s tempfile.gettempdir()), e.g. /tmp/singer-sdk on Linux/macOS. This directory is created on demand and is not managed or cleaned up by the SDK.

This default assumes the common case: a target reads and processes batch files shortly after the tap writes them, within the same sync run. It is not meant as durable or long-term storage — OS temp-cleanup policies (systemd-tmpfiles, Windows Disk Cleanup, ephemeral container filesystems that reset /tmp between runs) may remove files under this path independently of the sync’s lifecycle. If your tap/target pairing has a significant delay between writing and reading batch files, or runs in an environment that aggressively purges temp storage, configure an explicit storage.root pointing at durable storage instead.

Custom batch file creation and processing

Tap side

Taps can optionally customize the batch file creation by implementing the get_batches. This method should return a tuple of an encoding and a list of batch files:

class MyStream(Stream):
    def get_batches(self, records):
        return (
            BaseBatchFileEncoding(format="parquet", compression="snappy"),
            [
                "s3://my-bucket/my-batch-file-1.parquet",
                "s3://my-bucket/my-batch-file-2.parquet",
            ],
        )

Native Arrow data

If the source can produce Apache Arrow data directly (for example through an ADBC driver, a database client’s fetch_arrow_batches(), or PyMongoArrow), implement get_record_batches instead of overriding get_batches. When batch_config.encoding.format is arrow, the SDK writes those tables straight to Arrow IPC batch files, without converting each row to a Python dictionary:

class MyStream(Stream):
    replication_key = "id"
    is_sorted = True

    def get_record_batches(self, context):
        start = self.get_starting_replication_key_value(context)
        if start is None:  # First sync: no bookmark yet
            reader = my_driver.query_arrow("SELECT * FROM t ORDER BY id")
        else:
            # `>=`: rows sharing the bookmark's value may span two files
            reader = my_driver.query_arrow(
                "SELECT * FROM t WHERE id >= ? ORDER BY id",
                start,
            )
        yield from reader  # pyarrow.RecordBatch or pyarrow.Table objects

The SDK re-slices the tables into files of batch_size rows, drops deselected top-level columns, handles columns missing from the stream schema the same way RECORD mode does, and emits a STATE message after each file is written. For a partitioned stream, get_record_batches is called once per partition.

If the tables include the replication key column, state advances to each file’s maximum value. As in RECORD mode:

  • When is_sorted is True, the bookmark itself advances, so an interrupted sync resumes from the last written file. Filter with >= the bookmark, as in the example above, so that rows sharing its value aren’t skipped when they span two files. The rows at the bookmark’s value are read again on resume.

  • Otherwise, progress is kept in progress markers and the bookmark only advances once the sync completes, so an interrupted sync starts over from the previous bookmark.

The tables must already match the stream schema. In particular, cast types that JSON Schema can’t express the same way as Arrow (e.g. integer-encoded booleans or date columns declared as date-time) before yielding them. For free-form values with no fixed Arrow type, such as properties declared as a bare object, use Arrow’s canonical JSON extension type (pyarrow.json_()) with the JSON text: SDK targets decode those values into the same objects a RECORD message would carry. Stream maps and post_process aren’t applied. Other encoding formats, RECORD mode, and streams with child streams still use get_records.

Target side

Targets can optionally customize the batch file processing by implementing the process_batch_files.

class MySink(Sink):
    def process_batch_files(self, encoding, storage, files):
        # process the batch files

Known Limitations of BATCH

  1. Apart from native Arrow data, the built-in BATCH implementation does not support incremental bookmarks or STATE tracking. This work is tracked in Issue #976.

  2. The BATCH implementation is not currently compatible with Stream Maps. This is certainly possible to implement in theory, but it would also introduce some performance penalties. This limitation is tracked in Issue 1117#.

If you are interested in contributing to one or both of these features, please add a comment in the respective issue.