Skip to main content

A contract-first, Python-native framework for building REST APIs around use cases, ports, and explicit wiring.

Project description

Tenchi

Tenchi is a small, contract-first Python framework for building typed HTTP APIs. Contracts define the boundary, plain async functions implement use cases, and frozen dataclasses carry explicitly wired dependencies.

Tenchi uses Pydantic for validation, Starlette for ASGI, and httpx for its typed client. It requires Python 3.12 or newer and is currently pre-1.0.

Quick start

Create and run a working application:

uvx tenchi new my_app
cd my_app
uv sync
uv run tenchi dev

The generated app includes a todos feature, an in-memory adapter, tests, and explicit server wiring. With the server running:

curl -X POST http://127.0.0.1:8000/todos \
  -H 'content-type: application/json' \
  -d '{"title": "Buy milk"}'

To add Tenchi to an existing project instead:

uv add tenchi

How it works

A contract declares the HTTP boundary:

create_todo_contract = contract(
    method="POST",
    path="/todos",
    request=CreateTodo,
    response=Todo,
    status=201,
)

A use case is an ordinary async function whose dependencies come from the app context:

async def create_todo(request: CreateTodo, context: AppContext) -> Todo:
    return await context.todos.create(title=request.title)

A route binds them together. Tenchi immediately checks that every boundary parameter and the return annotation exactly match the contract, so invalid wiring fails during application composition rather than on a request:

routes = route_group(
    route(create_todo_contract, create_todo),
)

Applications use this structure:

app/
  features/<feature>/   # contracts, schemas, ports, routes, use cases
  shared/               # shared errors and domain concepts
  infra/                # concrete port implementations
  server/               # context, hooks, route composition, ASGI app
tests/                  # HTTP integration tests

The main pieces are:

  • Pydantic validation for request bodies, path parameters, query parameters, headers, and responses; field aliases are the names used on the wire and in OpenAPI, and nullable request types can send JSON null explicitly.
  • typing.Protocol ports and explicit dependency wiring instead of a DI container.
  • Declared application errors with a stable JSON envelope.
  • A named exception hierarchy that distinguishes configuration mistakes from runtime application and transport failures.
  • A contract-driven async client and OpenAPI 3.1 generation.
  • Lifespan resources, request-scoped contexts, authentication hooks, middleware, pagination, health checks, and in-process testing helpers.

See examples/todos for the small teaching app and examples/taskboard for a larger application with authentication, authorization, SQLite transactions, and background work.

CLI

tenchi new my_app
tenchi make feature notes
tenchi make use-case notes create_note
tenchi routes
tenchi openapi
tenchi doctor
tenchi dev

Run tenchi <command> --help for command options.

Development

uv sync
uv run pytest
uv run ruff format --check .
uv run ruff check .
uv run pyright

Tenchi is licensed under the MIT License.

Project details


Download files

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

Source Distribution

tenchi-0.6.0.tar.gz (181.8 kB view details)

Uploaded Source

Built Distribution

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

tenchi-0.6.0-py3-none-any.whl (53.4 kB view details)

Uploaded Python 3

File details

Details for the file tenchi-0.6.0.tar.gz.

File metadata

  • Download URL: tenchi-0.6.0.tar.gz
  • Upload date:
  • Size: 181.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.28 {"installer":{"name":"uv","version":"0.11.28","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 tenchi-0.6.0.tar.gz
Algorithm Hash digest
SHA256 42071805758bdc781585428c352489c4a5eafe6261065f262fe5ee0af7474b9f
MD5 1607d79dee3a025d7a011d1189819beb
BLAKE2b-256 ca498ddd4224aaec49d17cd1c43732a8cbc24fa02acb292fee4cca017dc3297f

See more details on using hashes here.

File details

Details for the file tenchi-0.6.0-py3-none-any.whl.

File metadata

  • Download URL: tenchi-0.6.0-py3-none-any.whl
  • Upload date:
  • Size: 53.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.28 {"installer":{"name":"uv","version":"0.11.28","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 tenchi-0.6.0-py3-none-any.whl
Algorithm Hash digest
SHA256 6daf336e9bf8d2d69362d8b173ab715776e8ffafd09c4f1c92f4f98452d74f34
MD5 69d6a20bd599bdd6f543ab6a6b2a7a4a
BLAKE2b-256 470d2678e876fcb1eb4163cc42c078dd6d804c14920dc611cd1015e719efc8a7

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