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

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

chainlit_utils-0.0.3.tar.gz (8.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

chainlit_utils-0.0.3-py3-none-any.whl (12.5 kB view details)

Uploaded Python 3

File details

Details for the file chainlit_utils-0.0.3.tar.gz.

File metadata

  • Download URL: chainlit_utils-0.0.3.tar.gz
  • Upload date:
  • Size: 8.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.12.2 {"installer":{"name":"uv","version":"0.12.2","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}

File hashes

Hashes for chainlit_utils-0.0.3.tar.gz
Algorithm Hash digest
SHA256 2427a1e9a9e06e7424a3d09b1bb76c5cb1a4f550ef014c639d1fd6e0a02fc9d0
MD5 4810834ac3d74b281257f1b5c9856bd7
BLAKE2b-256 ebdb28a2c35c255bd4f2f705af0bc924f4b652edce44011426164056bf8dfd8c

See more details on using hashes here.

File details

Details for the file chainlit_utils-0.0.3-py3-none-any.whl.

File metadata

  • Download URL: chainlit_utils-0.0.3-py3-none-any.whl
  • Upload date:
  • Size: 12.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.12.2 {"installer":{"name":"uv","version":"0.12.2","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}

File hashes

Hashes for chainlit_utils-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 504937bb3bd411f2c0f549eb83f8c4e55064cccb53bdedd6e11db71e52378519
MD5 cf0445923cb8d2140e985821ffd334b0
BLAKE2b-256 5d1472c3f45e1abcefb8ecd7cab665746bf85670f1addcd6151d777cc28ca9cd

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page