Skip to main content

polyui for Python

This package lets a Python server (FastAPI, Django, a plain worker) use the PolyUI Rust core. It does the same job the Rust reference server does around a model's show_ui, update_ui and close_ui calls: check them against the catalogs, resolve $ref values for the current user, compile charts, seal actions into handles, and build the ui.open / ui.patch messages plus the short summary the model gets back.

It is a native extension built with PyO3 and maturin. One abi3 wheel works on CPython 3.10 and newer.

Build and install

You need Rust, uv and maturin 1.16 or newer (uv tool install maturin).

cd bindings/python
uv venv .venv --python 3.12
uv pip install --python .venv/bin/python pytest ruff
VIRTUAL_ENV=$PWD/.venv maturin develop --uv --release

To get a wheel instead, run maturin build --release --out dist.

The binding crate is a workspace member but not a default member, so a plain cargo build or cargo test at the repository root skips it. cargo clippy --workspace does build it. If the first python3 on your PATH is older than 3.9, point PyO3 at a newer one with PYO3_PYTHON=$PWD/.venv/bin/python.

A short tour

import polyui

validator = polyui.Validator(
    polyui.Catalogs.builtin(),
    actions={
        "pay_bill": {
            "args": {"bill": {"kind": "string", "required": True, "max": 32}},
            "requires_confirm": True,
        }
    },
    ref_sources={"account": {"kind": "string", "max": 40}},
    resolver=lambda user, source, key: accounts_for(user).get(key),
)

tools = validator.tool_definitions(["core@1", "chart@1"])

try:
    prepared = validator.prepare_show(tool_input, user=current_user, declared=hello["catalogs"])
except polyui.ValidationError as error:
    send_tool_result(str(error), is_error=True)
else:
    send_to_client(prepared.messages[0])
    remember(prepared.model, prepared.client, prepared.actions)
    send_tool_result(prepared.summary)

str(error) is the numbered problem list written for the model. error.problems has the same problems as dicts (at, message, and where it applies list, index, id, prop, path, field), and error.operation says which tool failed.

What you get:

Name What it does
Catalogs builtin(), with_embed() to add the embed catalog, with_catalog(json_or_dict) for extension catalogs, restrict(declared), negotiate(declared), tags, type_names().
Validator(catalogs, actions, ref_sources, resolver, allow_http, ui_resource) show_ui, update_ui, close_ui take the tool input as a dict or JSON text.
Validator.prepare_show / prepare_update The whole server pipeline in one call. Returns Prepared with model, client, messages, summary, actions, seq and touched.
Validator.resolve_refs, compile_charts, attach_apps, seal_actions, downgrade, summary, fallback The same steps one at a time, if you want to run them yourself.
Surface, Update Validated results. to_dict() and Surface.from_dict() let you keep them in a session store.
tool_definitions(catalogs, actions, strict=False, profile="compact") The Anthropic tool JSON for show_ui, update_ui and close_ui. profile="full" puts every prop in the schema and is needed for strict.
ShowStream, Validator.feed_show(stream, chunk, ...), Validator.finish_show(stream, payload=None, ...) Stream a show_ui call: ui.open and ui.patch for each component as it arrives, then the full check. See Streaming.
Validator.ref_keys(payload) The declared source:key refs a tool call uses, to fetch before prepare_show(..., refs=...).
open_message, patch_message, close_message, closed_summary Protocol messages and the close summary.
compile_chart, chart_svg, describe_chart Chart scene, standalone SVG and the spoken summary.
Client(hello) The client core: receive, set_value, fire, confirm, say, match_voice. Handy for tests and server side rendering checks.

Errors all derive from PolyUIError: ValidationError, CatalogError (bad catalog, action or ref source specs, with .path), ChartError, ProtocolError (bad hello).

Refs and the resolver

Every ref source declares the kind of value it gives, using the same prop spec format as the catalogs. The validator uses it to check that a $ref fits the prop before anything is resolved. The resolver is called as resolver(user, source, key) with whatever you passed as user. Return the value, return None when there is nothing, or raise polyui.RefRefusedError when this user may not see it. The model only ever learns that the ref could not be resolved. Any other exception from the resolver is raised again unchanged, so a database error does not turn into a validation problem.

For async lookups, call validator.ref_keys(tool_input), await the values, and pass them as refs={"source:key": value} to prepare_show, prepare_update or resolve_refs; the resolver is then not called and a missing key is refused. See Async lookups.

The model's copy of a surface (prepared.model) keeps the $ref objects, and its summary shows them as [ref]. Only prepared.client and the messages carry the resolved values.

MCP Apps

With Catalogs.builtin().with_embed() the model can show an MCP App through embed:Embed. Pass ui_resource= to the validator; it is called as ui_resource(user, uri) for each distinct ui:// URI in the call and returns the resources/read entry, a dict {"resource": entry, "tool_input": ..., "tool_result": ...}, or None when this user has no such app. prepare_show and prepare_update check the entry (exact text/html;profile=mcp-app mime type, matching uri, text or a base64 blob) and put it in the server-only app prop of the client copy. A missing or wrong entry, or one too large for a 256 KiB message, is a ValidationError for the model. prepared.model and the summary never contain the app. When declared leaves out embed@1, no resource is read. Exceptions from the callback are raised again unchanged. docs/embed.md has the rules.

Actions

seal_actions and the prepare_* calls replace every action object with a handle like {"action_id": "act_3f2a...", "confirm": true}. The real action name and arguments come back in prepared.actions; keep them on the server and look them up when the client sends ui.action. Pass next_id= if you want your own ids.

FastAPI example

examples/fastapi_app.py is a small app with a POST /show_ui endpoint that checks the payload and returns the ui.open message, or a 422 with the problems. FastAPI is an optional extra:

uv pip install --python .venv/bin/python fastapi uvicorn httpx
.venv/bin/uvicorn --app-dir examples fastapi_app:app --reload

Tests

.venv/bin/python -m pytest
.venv/bin/ruff check . && .venv/bin/ruff format --check .

The client tests replay every fixture in tests/conformance/client, so the binding is checked against the same expectations as the Rust core. The example tests are skipped when FastAPI is not installed.

Not there yet

ui_resource is synchronous; read MCP App resources before the call or run it in a worker thread. Async ref lookups go through ref_keys and refs (above).

Metadata

Release files for polyui 0.1.2

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

Source distribution (sdist)

Source distribution for polyui 0.1.2
File Size Uploaded Provenance Actions
polyui-0.1.2.tar.gz 223.8 kB With provenance Details for polyui-0.1.2.tar.gz

Built distributions (wheels)

Table of built distributions (wheels) for polyui 0.1.2
File Actions
polyui-0.1.2-cp310-abi3-win_amd64.whl CPython 3.10 abi3 Windows x86-64 Details for polyui-0.1.2-cp310-abi3-win_amd64.whl
polyui-0.1.2-cp310-abi3-musllinux_1_2_x86_64.whl CPython 3.10 abi3 Linux musl 1.2+ x86-64 Details for polyui-0.1.2-cp310-abi3-musllinux_1_2_x86_64.whl
polyui-0.1.2-cp310-abi3-musllinux_1_2_aarch64.whl CPython 3.10 abi3 Linux musl 1.2+ ARM64 Details for polyui-0.1.2-cp310-abi3-musllinux_1_2_aarch64.whl
polyui-0.1.2-cp310-abi3-manylinux2014_x86_64.manylinux_2_17_x86_64.whl CPython 3.10 abi3 Linux glibc 2.17+ x86-64 Details for polyui-0.1.2-cp310-abi3-manylinux2014_x86_64.manylinux_2_17_x86_64.whl
polyui-0.1.2-cp310-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.whl CPython 3.10 abi3 Linux glibc 2.17+ ARM64 Details for polyui-0.1.2-cp310-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.whl
polyui-0.1.2-cp310-abi3-macosx_11_0_arm64.whl CPython 3.10 abi3 macOS 11.0+ ARM64 Details for polyui-0.1.2-cp310-abi3-macosx_11_0_arm64.whl
polyui-0.1.2-cp310-abi3-macosx_10_12_x86_64.whl CPython 3.10 abi3 macOS 10.12+ x86-64 Details for polyui-0.1.2-cp310-abi3-macosx_10_12_x86_64.whl

Total release size: 6.1 MB

Release files / polyui-0.1.2.tar.gz

Download URL polyui-0.1.2.tar.gz
Size 223.8 kB
Tags Source
SHA-256 checksum
How to use checksums
815d10c7c2e0b4883831f69144e89e62e670d165e6e0bbf24706f6fd26ff7459
BLAKE2b-256 checksum
How to use checksums
c4733ce0c13f187084358d11f6f58d7b2cb566d93ef002d665eef3b8e5bbc0fd
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 Nov 25, 2026.

Transparency log

Release files / polyui-0.1.2-cp310-abi3-win_amd64.whl

Download URL polyui-0.1.2-cp310-abi3-win_amd64.whl
Size 802.2 kB
Tags CPython 3.10 Windows x86-64 abi3
SHA-256 checksum
How to use checksums
8e475fe7d8d5de3e303cc316c4d3803034f3012b5280d050e136e81cfb9bb024
BLAKE2b-256 checksum
How to use checksums
6e539ab5ed1931945ed046d961814683c6753f015d679e96fca1f2bd4adbf74f
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 Nov 25, 2026.

Transparency log

Release files / polyui-0.1.2-cp310-abi3-musllinux_1_2_x86_64.whl

Download URL polyui-0.1.2-cp310-abi3-musllinux_1_2_x86_64.whl
Size 1.0 MB
Tags CPython 3.10 Linux musl 1.2+ x86-64 abi3
SHA-256 checksum
How to use checksums
4f3e3b4735d6958fbcdeec52b8e53a9cfd00730ca88b0d0e01f5f78a92637fc4
BLAKE2b-256 checksum
How to use checksums
64e3f87c4fc432e4b69018ad40c07bc3006e1d14bfd58bcefb091360134775bb
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 Nov 25, 2026.

Transparency log

Release files / polyui-0.1.2-cp310-abi3-musllinux_1_2_aarch64.whl

Download URL polyui-0.1.2-cp310-abi3-musllinux_1_2_aarch64.whl
Size 932.4 kB
Tags CPython 3.10 Linux musl 1.2+ ARM64 abi3
SHA-256 checksum
How to use checksums
59f8233b3ed134220b3f1f06b0a0522be2be7c91e5225df6f7f3e6f88d029d45
BLAKE2b-256 checksum
How to use checksums
89916b0c94785fad9e75748f00e2bd3b909f490e54e211144df534ad3eb7ed24
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 Nov 25, 2026.

Transparency log

Release files / polyui-0.1.2-cp310-abi3-manylinux2014_x86_64.manylinux_2_17_x86_64.whl

Download URL polyui-0.1.2-cp310-abi3-manylinux2014_x86_64.manylinux_2_17_x86_64.whl
Size 812.7 kB
Tags CPython 3.10 Linux glibc 2.17+ x86-64 abi3
SHA-256 checksum
How to use checksums
b642ac224c2330577fe911cf87b664799e9eb2ff8a3decb37b5a653f39fbafbf
BLAKE2b-256 checksum
How to use checksums
6d64284d055e1c2815f34001cf4eda3083fefdc71e68fffced163eee8272adaf
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 Nov 25, 2026.

Transparency log

Release files / polyui-0.1.2-cp310-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.whl

Download URL polyui-0.1.2-cp310-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.whl
Size 755.3 kB
Tags CPython 3.10 Linux glibc 2.17+ ARM64 abi3
SHA-256 checksum
How to use checksums
1f72f2f4e2d233dccebc6f8425ff611f65b1fc3b48fd73f0828b0613d9a15cdb
BLAKE2b-256 checksum
How to use checksums
c121868c8f6abfc8ae0209b452291f0b9bc09ba7cd0902b9268b732cd37df0da
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 Nov 25, 2026.

Transparency log

Release files / polyui-0.1.2-cp310-abi3-macosx_11_0_arm64.whl

Download URL polyui-0.1.2-cp310-abi3-macosx_11_0_arm64.whl
Size 741.4 kB
Tags CPython 3.10 abi3 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
0018adde9a63ca6980392cdacffcc6ff6131929c81e7e1454f0221f71c0f02a6
BLAKE2b-256 checksum
How to use checksums
f0753f6caa84bdf3f64dd2366466167fa9103b9b0d71c1f19a0c4136fda12bde
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 Nov 25, 2026.

Transparency log

Release files / polyui-0.1.2-cp310-abi3-macosx_10_12_x86_64.whl

Download URL polyui-0.1.2-cp310-abi3-macosx_10_12_x86_64.whl
Size 776.8 kB
Tags CPython 3.10 abi3 macOS 10.12+ x86-64
SHA-256 checksum
How to use checksums
f2ca8d3effeee8b4ea6d89cbf34ca7f2cc00a133460237c5dfc037670a477b9a
BLAKE2b-256 checksum
How to use checksums
44ee82aefd3f8185f37278f3e9aa2b7436f3721ccc4bc7919839434216cefc41
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 Nov 25, 2026.

Transparency log

Security for polyui 0.1.2

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

All 8 files in this release have been uploaded with provenance metadata.

Select from 8 files

Attestation for polyui-0.1.2.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 Nov 25, 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