Skip to main content

pykicak

pykicak makes it easy to build RabbitMQ-connected agent nodes with idempotent message processing. It provides abstract base classes — Injector and Executor — that handle RabbitMQ connection setup and queue/exchange declaration, so subclasses only need to implement their specific behavior. Delivery is at-least-once, so the processing your agents implement must be idempotent; see Requirement: idempotent processing.

  • Injector — short-lived node that creates and publishes an initial message to start a processing flow (e.g. a document ingestion trigger).
  • Executor — long-running node that consumes messages from a queue, executes them, and optionally publishes the results to an exchange. Messages that can never be processed are dead-lettered instead of blocking the queue. It keeps its part of the RabbitMQ topology in place, restoring deleted queues, exchanges, and bindings on both its input and its output side. Can operate in "terminator" mode where it consumes but does not publish (e.g. a database writer or quality checker).

This README covers what every agent must meet, installation, and a quick start; the full documentation is in docs/.

Requirement: idempotent processing

pykicak is designed for idempotent agents only. It delivers every message at least once, never exactly once: wherever a failure leaves a choice, it processes a message again rather than risk losing it, and the price is that a message can be processed, and its results published, more than once. The few cases in which RabbitMQ itself can still lose a message, two of them with classic queues, are listed in When a message can still be lost. Every agent built on pykicak must therefore be idempotent: processing the same message twice, or twice at the same time, must leave the system in the same state as processing it once. Using pykicak for processing that is not idempotent can corrupt data or repeat external actions.

A message is processed more than once when:

  • an Executor is killed or stopped without stop() (crash, SystemExit, KeyboardInterrupt, a supervisor's forced kill) while it processes a message, so RabbitMQ redelivers the message, which is executed again, and its results are published again if they had already been published; a graceful stop() finishes the message first and avoids this;
  • the connection is lost while an Executor commits its results, after RabbitMQ committed them but before the Executor learned of it, so the input is executed again and all its results are published again;
  • processing a message, retries of execute() included, takes longer than heartbeat_seconds or than RabbitMQ's consumer_timeout, so the input is redelivered while it is still being executed, and with several instances it runs concurrently on another instance (see Long-running execute());
  • execute() raises TransientProcessingError, so it is called again, and after max_processing_attempts failed calls the message waits in the retry queue and is executed again, up to max_retry_rounds times; if the Executor loses its connection between putting the message into the retry queue and acknowledging it, it is executed once more on top (see Retry queue);
  • a dead-lettered message is sent back from the dead-letter queue, for example once a service that was down is back, so it is executed again; if the connection is lost while it is sent back, it can be sent back, and executed, twice (see Sending dead letters back);
  • an Executor dead-letters a message, and loses its connection, or consumer_timeout takes the message back, between putting a copy of it into the dead-letter queue and acknowledging it, so the message is executed again: it may be dead-lettered a second time, or succeed and be in the dead-letter queue and processed (see Dead-letter queue); and if RabbitMQ does not take the copy, the Executor requeues the message, which is executed again;
  • an Executor holds results that RabbitMQ returned as unroutable (see Unroutable results are held), and stop() is called, so it requeues the input, or the connection is lost or consumer_timeout expires meanwhile, so RabbitMQ requeues it; either way the input is executed again;
  • RabbitMQ refuses the commit of an Executor's results, or one of the declarations that verify its output side, so the input is requeued and executed again;
  • the acknowledgement fails, for example because the connection was lost, or RabbitMQ's consumer_timeout took the input back right after the results were published, so RabbitMQ redelivers the input, which is executed again and its results published again;
  • an Injector loses its connection before RabbitMQ confirms a message, so it publishes the message again;
  • a RabbitMQ memory or disk alarm blocks an Injector's connection for longer than its blocked_timeout_seconds (60 by default), so the Injector gives up on that attempt and publishes the message again. RabbitMQ may still enqueue the earlier publish once the alarm clears, even when the injection ends with InjectionError.

What this requires from an application:

  • Idempotent side effects in execute(). Enforce them in the database (unique constraints, upserts keyed by a message ID), not by checking first and writing afterwards, which fails when two instances run the same message concurrently. This includes partial work done by a call that then raises TransientProcessingError.
  • Stable message IDs. Every message has a required message_id (see Message types). Give each message a unique ID when it is first created, and derive the IDs of results from the input's ID (for example with uuid.uuid5), not from uuid4(), so that executing an input again produces results with the same IDs, which downstream agents can deduplicate.
  • Idempotent downstream agents. Every Executor in a pipeline receives duplicates, so the requirement applies to all of them.
  • No at-most-once actions without a deduplication key. Actions that must not be repeated, such as sending an email, charging a payment, or calling a non-idempotent API, need an idempotency key derived from the message ID, or must not be performed by a pykicak agent.

Installation

pip install pykicak

Requires Python 3.12+. Depends on pika for the RabbitMQ connection.

Configuration

Agents take their RabbitMQ connection details from a KicakConfig, which reads a .kicak file of KEY=VALUE lines, the environment variables of the same names, or a mapping built in code:

RABBIT_MQ_USERNAME=guest
RABBIT_MQ_PASSWORD=guest
RABBIT_MQ_HOST=localhost
RABBIT_MQ_PORT=5672
RABBIT_MQ_VIRTUAL_HOST=/
RABBIT_MQ_TLS=false
from pykicak import KicakConfig

config = KicakConfig.from_file(".kicak")  # or KicakConfig.from_env(), or KicakConfig(values={...})

RABBIT_MQ_TLS is required, so an unencrypted connection is always an explicit choice: use false only on a network you fully trust. Never commit a real .kicak file. See Configuration for every key, environment variables, TLS, and client certificates.

Message types

Messages sent and received by agents are declared as KicakMessage subclasses, independent of any agent class, so the same type can be imported by both the publishing side (injector/executor) and the consuming side (executor). Each class names its type with kicak_type (see Type names: the contract between agents):

import dataclasses
from pykicak import KicakMessage


@dataclasses.dataclass(frozen=True, slots=True)
class CrawledItem(KicakMessage):
    kicak_type = "crawled-item.v1"
    url: str
    content: str


@dataclasses.dataclass(frozen=True, slots=True)
class ClassifiedItem(KicakMessage):
    kicak_type = "classified-item.v1"
    url: str
    category: str


item = CrawledItem(url="https://example.com", content="...", message_id="5f0c7b1e-...")

Every message has a required, keyword-only message_id, declared by KicakMessage, so your classes do not declare it. See Message types for the rules for fields and IDs, how type names form the contract between agents, and how to introduce a new version of a message without stopping the flow.

Usage

The examples use the messages defined in Message types and literal topology names for brevity. In an application, define these names centrally in its messaging/ module as described in RabbitMQ topology.

Executor (terminator mode — no publishing):

from pykicak import (
    KicakConfig,
    KicakExecutorAbstract,
    MalformedMessageError,
    TransientProcessingError,
)


class SaveToDbExecutor(KicakExecutorAbstract[ClassifiedItem, None]):  # None: publishes nothing
    def execute(self, message: ClassifiedItem) -> None:
        if not message.url:
            raise MalformedMessageError("url is empty")  # dead-lettered
        try:
            ...  # upsert the item into a database, keyed by message.message_id
        except ConnectionError as error:
            raise TransientProcessingError("database unavailable") from error  # retried
        return None  # a terminator publishes nothing


config = KicakConfig.from_file(".kicak")
executor = SaveToDbExecutor(
    config,
    source_queue="db-writer",  # dead-letters to db-writer.dlx, retained in db-writer.dlq
    source_exchange="items.classified",
    max_retry_rounds=360,  # 360 rounds of 10 seconds: waits out a database down for an hour
)
executor.run()

The exception execute() raises decides what happens to the message and the Executor: any exception other than these two stops the Executor. See What execute() raises is your decision.

Injector (publishes an initial message):

import uuid

from pykicak import KicakConfig, KicakInjectorAbstract


class StartProcessingInjector(KicakInjectorAbstract[CrawledItem]):  # the type it publishes
    def generate(self) -> CrawledItem:
        # A new ID where the message is first created; it never changes afterwards
        return CrawledItem(message_id=str(uuid.uuid4()), url="https://example.com", content="...")


config = KicakConfig.from_file(".kicak")
injector = StartProcessingInjector(config, exchange_name="items.inbox")
injector.run()  # publishes once, then returns

Exactly one queue may be bound to an Injector's exchange: the input queue of the first Executor, here classifier, whose Executor binds it (see RabbitMQ topology).

Running an Injector describes how an injection is retried, what run() raises when an injection was not confirmed, publish(), and repeated injections.

Executor (with publishing to an exchange):

import uuid

from pykicak import KicakConfig, KicakExecutorAbstract


class ClassifyingExecutor(KicakExecutorAbstract[CrawledItem, ClassifiedItem]):
    def execute(self, message: CrawledItem) -> ClassifiedItem:
        # The result's ID is derived from the input's, so executing the input again
        # publishes a result with the same ID, which downstream agents can deduplicate
        result_id = uuid.uuid5(uuid.NAMESPACE_URL, f"classified:{message.message_id}")
        return ClassifiedItem(message_id=str(result_id), url=message.url, category="news")


config = KicakConfig.from_file(".kicak")
executor = ClassifyingExecutor(
    config,
    source_queue="classifier",
    source_exchange="items.inbox",
    destination_exchange="items.classified",
    destination_queues=["db-writer", "quality-checker"],  # every queue bound to items.classified
    max_retry_rounds=0,  # its execute() never raises TransientProcessingError
)
executor.run()

execute() returns a message, a sequence of messages, or None. The Executor publishes the results itself, only after execute() succeeds, so a failed message never produces partial output. It publishes all results of one input in a single RabbitMQ transaction and acknowledges the input only after the transaction is committed (see Results are published in a transaction).

The class header KicakExecutorAbstract[CrawledItem, ClassifiedItem] declares that this Executor accepts CrawledItem and publishes ClassifiedItem. Type checkers check execute() against it, and the Executor refuses any other type in both directions (see Type names: the contract between agents). Here the queues db-writer and quality-checker must belong to Executors that accept ClassifiedItem, as SaveToDbExecutor does.

Agents declare their queues and exchanges automatically (idempotently) when they connect, and Executors declare them again while they run (see Topology verification).

run() consumes until stop() is called, from any thread or a signal handler. See Running and stopping an Executor for a graceful stop, execute() calls that take long, and running several instances of an Executor.

Error handling and dead-lettering

What execute() raises is your decision

The library cannot tell whether a failure inside execute() will clear by itself, is caused by the message, or is a bug in your code. Your execute() tells it, through the exception it raises, and that decides what happens to the message and to the Executor:

execute() The message The Executor
returns its results are published, then it is acknowledged continues
raises TransientProcessingError: the failure may clear, e.g. a service is down, times out, or limits your rate execute() is called again, up to max_processing_attempts times; if it still fails, the message is moved to the retry queue and executed again after retry_delay_seconds (10 by default), up to max_retry_rounds times; still failing after that, it is dead-lettered with the reason retries-exhausted (see Retry queue) continues
raises MalformedMessageError: the message can never succeed, e.g. its content is invalid dead-lettered to <queue>.dlq at once, without another attempt, with headers saying why (see Dead-letter queue) continues
raises any other exception: treated as a bug dead-lettered, so it cannot block the queue stops: run() raises the exception, and the status is "CRASHED"
raises a BaseException such as KeyboardInterrupt or SystemExit not settled; RabbitMQ requeues it stops at once, in the middle of execute(): run() raises it, and the status is "CRASHED"

An exception never leads to "STOPPED": that status follows only a graceful stop().

"Any other exception" includes everything raised by the libraries execute() calls: a client library's timeout, a ConnectionError, a KeyError for a missing field. Left uncaught, such an error dead-letters the message and stops the Executor, even if it would have cleared a moment later. So it is your job to catch the errors you expect, where they happen, and raise one of the two classified exceptions from them:

from pykicak import MalformedMessageError, TransientProcessingError


def execute(self, message: Document) -> Summary:
    if not message.text.strip():
        raise MalformedMessageError("document has no text")  # dead-lettered; the Executor continues
    try:
        text = self.summarizer.summarize(message.text)  # your client of an external service
    except (TimeoutError, ConnectionError) as error:
        raise TransientProcessingError("summarizer unavailable") from error  # retried later
    # Any other exception, e.g. a bug, dead-letters the message and stops the Executor
    return Summary(message_id=f"{message.message_id}.summary", text=text)

Stopping on an unexpected exception is deliberate: a bug would otherwise fail, and dead-letter, every message in the queue. Stopping limits the damage to one message per instance (see Scaling an Executor) and makes the failure visible. Once the bug is fixed, start the Executor again and, if needed, send the dead-lettered message back from <queue>.dlq (see Sending dead letters back).

Raise TransientProcessingError only for failures that can clear. A failure that never clears keeps the message going round through the retry queue, calling execute() again every round, until max_retry_rounds are used up and it is dead-lettered (see Retry queue). Every call may have done part of the work before failing, which is one more reason execute() must be idempotent (see Requirement: idempotent processing).

Where failed messages go

A message that keeps raising TransientProcessingError waits in the Executor's retry queue <queue>.retry between rounds (see Retry queue). A dead-lettered message is kept in <queue>.dlq with headers saying why (see Dead-letter queue), and can be sent back once the cause is fixed (see Sending dead letters back). Every outcome of a delivery lists what happens to a delivery in every case, connection failures included.

Documentation

  • Configuration: the .kicak file, environment variables, every key, TLS, and client certificates (mutual TLS).
  • RabbitMQ topology: the constructor arguments, naming conventions, who declares what, topology verification, and queue types.
  • Message types: message classes, message_id, type names, and introducing a new version of a message.
  • Running an Injector: how an injection is retried, InjectionError, publish(), and repeated injections.
  • Every outcome of a delivery: what happens to a delivery in every case, held unroutable results, and the results' transaction.
  • Retry queue: where a message waits between retries, max_retry_rounds, and its headers.
  • Dead-letter queue: where failed messages are kept, the headers that say why, and sending them back.
  • Running and stopping an Executor: graceful stop, long-running execute(), and scaling to several instances.
  • Monitoring: get_info(), logging, statistics in the logs, and connection names.
  • Delivery guarantees: what publishing guarantees, who published a message and when, and connection retries.
  • Proof of concept: the runnable example in examples/poc/, including a message upgrade in progress.
  • Development: running the tests and checks, and CI.

Public API

pykicak exports DeadLetterCounts, ExecutorCounts, ExecutorInfo, ExecutorStatus, InjectionError, KicakConfig, KicakConfigError, KicakExecutorAbstract, KicakInjectorAbstract, KicakMessage, MalformedMessageError, QueueType, TransientProcessingError, tls_options, and __version__. Build agents by subclassing KicakInjectorAbstract or KicakExecutorAbstract; their shared base class is internal. tls_options(config) returns the TLS settings agents connect with, for a pika connection of your own (see TLS for a connection of your own).

Development

See Development for running the tests and the checks CI runs, and RELEASING.md for how a version is released.

License

pykicak is released under the MIT License.

Metadata

Release files for pykicak 0.4.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pykicak 0.4.0
File Size Uploaded Provenance Actions
pykicak-0.4.0.tar.gz 73.0 kB With provenance Details for pykicak-0.4.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for pykicak 0.4.0
File Interpreter ABI Platform Actions
pykicak-0.4.0-py3-none-any.whl Python 3 none any Details for pykicak-0.4.0-py3-none-any.whl

Total release size: 148.8 kB

Release files / pykicak-0.4.0.tar.gz

Download URL pykicak-0.4.0.tar.gz
Size 73.0 kB
Tags Source
SHA-256 checksum
How to use checksums
f67f1ab358bde955c07599968732ede76e3f7b7b993386288096d8f46968b258
BLAKE2b-256 checksum
How to use checksums
d8bebd50b70ba696078122114abc738c6979cfaccaa2267ae252ba42ca63edcd
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 26, 2026.

Transparency log

Release files / pykicak-0.4.0-py3-none-any.whl

Download URL pykicak-0.4.0-py3-none-any.whl
Size 75.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
407336a57c87c6a114c8bef38624ec6eecadef954b407e06c9e34a9762b726dc
BLAKE2b-256 checksum
How to use checksums
021dad2bfebc2d972175e26f4c7aa08501d40c100a6ea8904f23761f8ff1d6e3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 26, 2026.

Transparency log

Security for pykicak 0.4.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for pykicak-0.4.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 26, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page