Hatchet Python SDK

1.32.1 · active · verified Sat Apr 11

This is the official Python SDK for Hatchet, a distributed, fault-tolerant task queue. The SDK allows you to easily integrate Hatchet's task scheduling and workflow orchestration capabilities into your Python applications, supporting the development of mission-critical AI agents, durable workflows, and background tasks. It is actively maintained, with regular updates and a focus on durability and scalability.

Warnings

Install

Imports

Quickstart

This quickstart initializes the Hatchet client using environment variables for the API token and host, defines a simple task with the `@hatchet.task()` decorator, registers it with a worker, and starts the worker to process incoming tasks. Replace 'YOUR_HATCHET_CLIENT_TOKEN' with your actual token.

import os
from hatchet_sdk import Hatchet, Context, EmptyModel

# Initialize the Hatchet client. HATCHET_CLIENT_TOKEN is required.
# HATCHET_CLIENT_HOST_PORT defaults to 'localhost:7077' if not set.
hatchet = Hatchet(
    token=os.environ.get('HATCHET_CLIENT_TOKEN', 'YOUR_HATCHET_CLIENT_TOKEN'),
    host_port=os.environ.get('HATCHET_CLIENT_HOST_PORT', 'localhost:7077')
)

# Define a simple Hatchet task using the decorator
@hatchet.task()
def hello_world(input: EmptyModel, context: Context) -> dict[str, str]:
    print(f"Executing task hello_world with input: {input.model_dump_json()}")
    return {"message": "Hello, world from Hatchet!"}

# Create a worker and register the task
worker = hatchet.worker("my-python-worker")
worker.register_task(hello_world)

print("Starting Hatchet worker... Press Ctrl+C to stop.")
# Start the worker to listen for and execute tasks
worker.start()
print("Hatchet worker stopped.")

view raw JSON →