Skip to main content

slackblocks

Licence: MIT Licence: BSD-3-Clause Python Versions PyPI PyPI downloads Build Status Docs

Build Slack messages in Python — without writing JSON by hand.

slackblocks is a typed, validating Python wrapper around the Slack Block Kit API. It exists because Block Kit JSON is verbose, easy to get subtly wrong, and unpleasant to maintain in source control.

This release conforms to the shared cross-language slackblocks specification.

Why slackblocks?

  • Concise — SectionBlock("Hello, *world*!") instead of a 10-line JSON object.
  • Validated — character limits, required fields, mutually-exclusive options, and element-type restrictions are enforced at construction time, so you find out before hitting Slack's API.
  • Drop-in compatible with both the official slack-sdk and the legacy slackclient — unpack a Message directly into client.chat_postMessage(**message).
  • Typed — full type hints; ships py.typed.
  • Zero runtime dependencies.

Installation

pip install slackblocks

slackblocks 2.x requires Python 3.10 or newer. Users on Python 3.8 / 3.9 should pin to the 1.x line — see the Compatibility page.

Quickstart

from slackblocks import (
    ActionsBlock,
    Button,
    DividerBlock,
    HeaderBlock,
    Message,
    SectionBlock,
)

message = Message(
    channel="#general",
    text="Build #482 passed",  # plain-text fallback for notifications
    blocks=[
        HeaderBlock("Build #482 passed :white_check_mark:"),
        SectionBlock(
            fields=[
                "*Branch*\n`main`",
                "*Author*\n@nick",
                "*Duration*\n3m 12s",
                "*Tests*\n1,247 passed",
            ],
        ),
        DividerBlock(),
        ActionsBlock(
            elements=[
                Button(text="View build", action_id="view", url="https://ci.example.com/482"),
                Button(text="Re-run", action_id="rerun", value="482", style="primary"),
            ],
        ),
    ],
)

message can be sent in one line with the official Slack SDK:

import os
from slack_sdk import WebClient

client = WebClient(token=os.environ["SLACK_API_TOKEN"])
client.chat_postMessage(**message)

The ** operator unpacks slackblocks Message objects directly into the SDK call — no to_dict() boilerplate required.

A simple Slack message rendered in Slack

What's supported

Surface Status
Blocks ✅ All current block types, including Alert, Card, Carousel, Container, Context Actions, Data Table, Data Visualization, Plan, and Task Card
Elements ✅ Buttons, all select menus, date/time pickers, checkboxes, radio groups, all input types, overflow menus, workflow buttons
Composition Objects ✅ Text (+ PlainText / Markdown aliases), Option, Confirm, Conversation/Dispatch filters, Workflow, Trigger
Rich Text ✅ Sections, lists, quotes, code blocks, inline links/users/channels/emoji
Modals & Home Tabs ✅ Full views API
Messages ✅ chat.postMessage, webhook messages, slash-command/interaction responses, threaded replies, ephemeral messages
Attachments ⚠️ Supported but deprecated by Slack
Round-tripping ✅ Block.from_dict(data) and per-class from_dict for parsing incoming Slack JSON back into objects

What's new in 2.0

slackblocks 2.x is the first major release in the modernised line. Highlights:

  • Two new block types: MarkdownBlock (GitHub-flavored Markdown) and VideoBlock.
  • PlainText and Markdown thin aliases for Text(type_=...).
  • block_kit_builder_url(payload) — turn any block, message, or view into a Block Kit Builder URL for browser preview.
  • Workflow.from_url(url, **params) — one-line workflow construction.
  • Block.from_dict(data) + per-class from_dict parsers — round-trip incoming Slack JSON back into slackblocks objects.
  • Typed exception subclasses of InvalidUsageError (LengthError, RangeError, TypeMismatchError, MutualExclusivityError, MissingRequiredError) — existing except InvalidUsageError blocks continue to work.
  • Tighter type signatures: Literal narrowing on Button.style, ColumnSettings.align, ConversationFilter.include; @overload on Text.to_text so the return type narrows on allow_none.
  • Modern annotation syntax (list[X], X | Y) throughout.

Existing 1.x code continues to work unchanged; see the Migration Guide for the full diff.

Documentation

Comparison with slack-sdk block classes

The official slack-sdk ships its own block classes. slackblocks predates them and offers a more concise API, stricter up-front validation, and independent versioning. They produce equivalent JSON; pick whichever you find more ergonomic.

# slackblocks
SectionBlock("Hello, *world*!")

# slack-sdk equivalent
from slack_sdk.models.blocks import SectionBlock as SDKSectionBlock
from slack_sdk.models.blocks.basic_components import MarkdownTextObject
SDKSectionBlock(text=MarkdownTextObject(text="Hello, *world*!"))

Licensing

slackblocks is dual-licensed under MIT and BSD-3-Clause. Use whichever fits your project — this makes it safe to vendor into projects under either license.

Contributing

Contributions are welcome. Quick start (the project uses uv for dependency management):

git clone https://github.com/nicklambourne/slackblocks.git
cd slackblocks/python
uv sync --all-groups

uv run pytest test/unit test/conformance test/docs
uv run ruff format --check slackblocks test
uv run ruff check slackblocks test
uv run mypy slackblocks

Preview the documentation from the repository root with pnpm --filter @slackblocks/docs start.

For the full development guide — testing conventions, validation patterns, docstring style, release process, and a PR checklist — see the Contributing page.

Bug reports and feature requests: https://github.com/nicklambourne/slackblocks/issues.

Metadata

Release files for slackblocks 2.8.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 slackblocks 2.8.0
File Size Uploaded Provenance
slackblocks-2.8.0.tar.gz 201.6 kB Without provenance Details

Built distribution (wheel)

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

Total release size: 265.6 kB

Release files / slackblocks-2.8.0.tar.gz

Download URL slackblocks-2.8.0.tar.gz
Size 201.6 kB
Tags Source
SHA-256 checksum
How to use checksums
9cbdea57a096d03704098734b8e8715ce492cf14b02c6e8f4af30b042bc131c6
BLAKE2b-256 checksum
How to use checksums
cce02de94bb1b76036b96e81fc7e4364cd54d7b276e48c400771b39873cb79fc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.13.1 {"installer":{"name":"uv","version":"0.13.1","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / slackblocks-2.8.0-py3-none-any.whl

Download URL slackblocks-2.8.0-py3-none-any.whl
Size 64.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ba98e04987f34c0c35308c385f470cd42087fea870fb4f90894f1e83a6e0a421
BLAKE2b-256 checksum
How to use checksums
50e5e15ee4f3d8afb8b483aa53cc6413e3623b62002f8699376551dbd99aa8ba
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.13.1 {"installer":{"name":"uv","version":"0.13.1","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Security for slackblocks 2.8.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.

No provenance found

None of the files in this release were uploaded with provenance metadata.

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.

Release history Release notifications | RSS feed

This release

2.8.0 This release

2 release files No provenance

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