Skip to main content

chainlit-utils

Small, reusable helpers for Chainlit applications:

  • versioned PostgreSQL migrations for Chainlit's official data layer;
  • exclusion of UI-only and failed messages from model context;
  • conversion of simple JSON Schema settings into Chainlit widgets;
  • compact serialization of changed chat settings; and
  • retrieval of the authenticated Chainlit user identifier.

The package deliberately does not own model-provider protocols, application settings, login callbacks, or completion clients. Its own settings use the CHAINLIT_UTILS_ environment-variable prefix.

Install

uv add chainlit-utils

For local development before publishing:

uv add --editable /path/to/chainlit-utils

PostgreSQL persistence

Chainlit uses DATABASE_URL to enable its native PostgreSQL data layer. Apply the matching schema before starting the application:

uv run --env-file .env chainlit-utils-migrate
uv run --env-file .env chainlit run app.py

Migrations are checksum-protected and serialized with a PostgreSQL advisory lock. Existing applications can keep their current migration history table:

CHAINLIT_UTILS_MIGRATIONS_TABLE=_my_app_chainlit_schema_migrations

Review Chainlit's migration guidance before widening the supported Chainlit version range. The bundled migrations target Chainlit 2.11.1 or newer within the 2.x series.

Chat helpers

import chainlit as cl

from chainlit_utils.chat import (
    mark_persisted_errors_excluded,
    send_ui_message,
    text_only_chat_messages,
)


@cl.on_chat_resume
async def on_chat_resume(thread):
    mark_persisted_errors_excluded(thread)


@cl.on_message
async def on_message(_message):
    messages = text_only_chat_messages()
    # Send messages to an OpenAI-compatible client.


async def report_error(error: Exception):
    await send_ui_message(f"Chat completion failed: {error}")

text_only_chat_messages uses Chainlit's native role/content projection. It is not a lossless tool-call ledger.

UI-only messages use the chainlit_utils.exclude_from_model_context metadata key by default. Override it for an existing application without changing call sites:

CHAINLIT_UTILS_MODEL_CONTEXT_EXCLUDED_KEY=my_app.exclude_from_model_context

Chat settings

from chainlit_utils.chat_settings import settings_widgets, serialize_settings

widgets = settings_widgets(json_schema, defaults, saved_values)
await cl.ChatSettings(widgets).send()

encoded = serialize_settings(defaults, selected_values, max_length=512)
metadata = {"my_runtime_settings": encoded} if encoded is not None else {}

The widget adapter intentionally supports only booleans, string enums, and strings. The receiving application remains responsible for full schema validation.

Development

uv sync
uv run pytest
uv run ruff check src tests
uv run ruff format --check src tests
uv build

Release files for chainlit-utils 0.1.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 chainlit-utils 0.1.0
File Size Uploaded
chainlit_utils-0.1.0.tar.gz 8.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for chainlit-utils 0.1.0
File Interpreter ABI Platform
chainlit_utils-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 20.6 kB

Release files / chainlit_utils-0.1.0.tar.gz

Download URL chainlit_utils-0.1.0.tar.gz
Size 8.1 kB
Tags Source
SHA-256 checksum
How to use checksums
b5fe85342a6d04ef20299b3ddf9d1a69c9e491ff17b04296ad6285972bf18339
BLAKE2b-256 checksum
How to use checksums
7e53d6d57eb06b78963ac6f527d97a787b485aa41678a5656c3103302225fd76
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.12.5 {"installer":{"name":"uv","version":"0.12.5","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 / chainlit_utils-0.1.0-py3-none-any.whl

Download URL chainlit_utils-0.1.0-py3-none-any.whl
Size 12.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
086c4f57a78b450fab83c775b2bddf169bfe83c12db8669315bf76f7ba463b5d
BLAKE2b-256 checksum
How to use checksums
bc449d039bfb32b43835bcce4977c075da2d970e776a26ee64503dc54596ca1b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.12.5 {"installer":{"name":"uv","version":"0.12.5","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 history Release notifications | RSS feed

0.2.5

2 release files

0.2.4

2 release files

0.2.3

2 release files

0.2.2

2 release files

0.2.1

2 release files

0.2.0

2 release files

This release

0.1.0 This release

2 release files

0.0.3

2 release files

0.0.2

1 release file

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