Skip to main content

hayate-openapi

Hayate ecosystem: Start here · Production golden app · Tested compatibility

OpenAPI 3.1 generation for hayate — built from what your app already knows: routes from app.routes, request schemas from validators or explicit typed parameters, and response schemas from one decorator. No implicit body/query guessing and no schema-library lock-in.

Status: alpha (0.6.x). The emitted OpenAPI 3.1.1 document passes openapi-spec-validator and feeds openapi-typescript 7.13 for end-to-end TypeScript types in a locked CI interoperability gate. The internal design memo (Japanese, per project convention) lives in DESIGN.md. Interactive Scalar docs, security schemes, multipart uploads, and strict inline typing are included. Release history is in CHANGELOG.md.

The typed endpoint surface removes repeated validation and response glue while keeping every request source explicit:

from typing import Annotated, TypedDict
from uuid import UUID

from hayate import Hayate
from hayate_openapi import Body, Constraints, OpenApi, Path, Query, endpoint


class BookIn(TypedDict):
    title: str


class BookOut(TypedDict):
    id: UUID
    title: str


app = Hayate()


@app.post("/books/:book_id")
@endpoint(status=201, summary="Create a book")
async def create(
    book_id: Annotated[UUID, Path()],
    book: Annotated[BookIn, Body()],
    notify: Annotated[bool, Query()] = False,
    limit: Annotated[int, Constraints(ge=1, le=100), Query()] = 25,
) -> BookOut:
    if notify:
        ...
    return {"id": book_id, "title": book["title"]}


OpenApi(app, title="Bookstore", version="1.0.0").register(app)

@endpoint validates and converts every marked input, validates the declared return type, emits JSON, and attaches the exact same schemas to OpenAPI. Invalid inputs use Hayate's RFC 9457 400 response; response contract failures remain non-leaking 500 errors. Put the decorator closest to the function, below the Hayate route decorator.

Constraints supplies portable numeric bounds (gt, ge, lt, le) and string rules (min_length, max_length, pattern) without Pydantic or msgspec. The same metadata validates converted HTTP values and emits JSON Schema keywords, including on the Cloudflare Workers/Pyodide path.

Dependencies can have request parameters and sub-dependencies. A dependency runs once per request even when several consumers use it:

from hayate import Context
from hayate_openapi import Depends, Header


def request_id(
    c: Context,
    value: Annotated[UUID, Header(alias="x-request-id")],
) -> UUID:
    return value


@app.get("/jobs/:job_id")
@endpoint
async def job(
    job_id: Annotated[UUID, Path()],
    trace: Annotated[UUID, Depends(request_id)],
) -> dict[str, str]:
    return {"job_id": str(job_id), "trace": str(trace)}

Use Depends(callable, use_cache=False) only when a fresh result is required. Synchronous dependencies run in a worker thread on native Python and inline on threadless Pyodide; async dependencies work on both.

The original Context-first surface remains fully supported:

from hayate import Hayate
from hayate_openapi import OpenApi, describe, validated
import msgspec


class BookIn(msgspec.Struct):
    title: str


app = Hayate()


@app.post("/books", validated("json", BookIn))  # validator + schema tag in one
@describe(status=201, summary="Create a book")
async def create(c):
    book = c.req.valid("json")  # BookIn instance — validation still runs
    return c.json({"title": book.title}, status=201)


OpenApi(app, title="Bookstore", version="1.0.0").register(app)
# GET /openapi.json and interactive GET /docs are live; or emit statically:
#   python -m hayate_openapi main:app --title Bookstore --version 1.0.0

How it works

Source What it provides
app.routes (hayate ≥ 0.8) every method + path, converted to OpenAPI templating (:id{id})
@endpoint + Annotated markers typed binding, dependencies, response validation, JSON serialization, and matching schemas
validated(target, T) request body plus query / path / header / cookie schemas — a tagging wrapper around the core validator, behavior-identical
@describe(...) summary, tags, response schemas, operationId — all optional, all additive

hayate-auth middleware can supply operation security automatically:

@app.get("/documents", auth.require_oauth_token("documents:read"))
async def documents(c):
    return c.json([])


OpenApi(
    app,
    title="API",
    version="1",
    security_schemes=auth.openapi_security_schemes(),
).register(app)

Use @describe(security=[]) for an explicitly public operation. For uploads, use Hayate's own File type in a typed multipart field:

from hashlib import sha256
from typing import Annotated

from hayate import File, FormDataLimits
from hayate_openapi import Form, endpoint

UPLOAD_LIMITS = FormDataLimits(
    max_body_bytes=10 * 1024 * 1024,
    max_file_bytes=10 * 1024 * 1024,
    file_memory_bytes=1024 * 1024,
)


@app.post("/uploads")
@endpoint(status=201)
async def upload(
    file: Annotated[
        File,
        Form(media_type="multipart/form-data", limits=UPLOAD_LIMITS),
    ],
) -> dict[str, str | int]:
    digest = sha256()
    async for chunk in file.stream():
        digest.update(chunk)
    return {"name": file.name, "size": file.size, "sha256": digest.hexdigest()}

The same declaration binds the runtime File, emits an OpenAPI string/binary part, applies parser limits, and documents both 400 and 413 failures. On native streamed requests, files above file_memory_bytes spill to an unnamed temporary file; Workers remain disk-free and bounded. @endpoint closes parsed files after the handler returns or raises, so consume or persist them during the handler call. All typed fields in one form, including fields declared by dependencies, must share one media type and one FormDataLimits value.

The Context-first surface remains available: combine validated("form", schema, media_type="multipart/form-data") with binary_file() in a raw schema.

Schema conversion goes through a SchemaProvider protocol. The built-in StdlibProvider covers primitives, UUID/date/time, enums, literals, unions, collections, mappings, and TypedDict without another dependency. msgspec and pydantic are auto-detected through guarded imports; install them with hayate-openapi[msgspec] or hayate-openapi[pydantic] when their models and constraints are desired. A plain dict is taken as literal JSON Schema Draft 2020-12 and compiled once for runtime validation, including supported format checks such as UUID. CPython checks raw schemas when routes register. Pyodide compiles them on the first matching request and caches the validator because Workers forbids extension entropy during module-global evaluation. Raw schemas validate the literal request representation, so query, path, header, and cookie values are strings. Use @endpoint with typed parameters when the wire value should be converted to an integer, number, or boolean.

validated() supports the core's six targets: json, form, query, param, header, and cookie. The last four expand object properties into OpenAPI parameters. A param property must match a route parameter and overrides its generated schema while remaining required as OpenAPI mandates. Header schemas use the lowercase names exposed by Fetch; model aliases such as x-request-id keep a Python field name separate when needed. OpenAPI-reserved Accept, Content-Type, and Authorization header parameters are rejected instead of emitting fields the specification ignores.

TypeScript types, the recommended recipe:

python -m hayate_openapi main:app --title API --version 1.0.0 -o openapi.json
npx openapi-typescript openapi.json -o src/api-types.ts

The repository continuously runs the same flow against a representative app:

npm ci --ignore-scripts
scripts/check_interoperability.sh

The generator deliberately emits OpenAPI 3.1.1. Although OpenAPI 3.2.0 is published, openapi-typescript documents support for 3.0 and 3.1. Staying on 3.1.1 keeps validation, documentation, and typed client generation on one tested interoperability profile instead of claiming an unverified version bump. The private Node lock overrides vulnerable transitive parser/glob versions with patched releases; it is CI tooling and is not part of the Python distribution.

Interactive API reference

register() serves a Scalar API reference at /docs by default. It can execute requests, render schemas and security requirements, and generate client examples directly from the same OpenAPI 3.1 document.

The page has no inline JavaScript. It pins Scalar to an immutable version with Subresource Integrity and sends a restrictive Content Security Policy. Scalar is loaded from jsDelivr at browser time, while Scalar's telemetry, external client, sharing, deployment, MCP-generation, developer-tools, and AI-agent integrations are disabled by default. The Python package therefore keeps its single hayate dependency without sending the API document to another service. Disable the page or self-host the script when needed:

# JSON only
OpenApi(app, title="API", version="1", docs_path=None).register(app)

# Same-origin, self-hosted Scalar bundle
OpenApi(
    app,
    title="API",
    version="1",
    scalar_script_url="/assets/scalar.js",
).register(app)

The OpenAPI JSON and docs routes are intentionally excluded from the generated application schema.

What is documented (and what is not)

  • Routes with real HTTP verbs; WebSocket routes and wildcard mounts (/api/auth/*) are skipped.
  • Responses you declare. Undeclared operations get a bare 200 — the generator never invents schemas.
  • Operations with a validator automatically document the 400 application/problem+json failure the framework actually returns; form operations also document parser-limit failures as 413.
  • Cookie, Bearer, and OAuth 2.0 security requirements, including combined route middleware requirements.
  • JSON, URL-encoded, and multipart request bodies, including typed binary File parts with shared resource limits.
  • Explicit typed endpoint parameters, including request fields declared by cached sub-dependencies, and validated return annotations.

License

MIT

Download files

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

Source Distribution

hayate_openapi-0.7.0.tar.gz (24.3 kB view details)

Uploaded Source

Built Distribution

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

hayate_openapi-0.7.0-py3-none-any.whl (28.1 kB view details)

Uploaded Python 3

File details

Details for the file hayate_openapi-0.7.0.tar.gz.

File metadata

  • Download URL: hayate_openapi-0.7.0.tar.gz
  • Upload date:
  • Size: 24.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.14

File hashes

Hashes for hayate_openapi-0.7.0.tar.gz
Algorithm Hash digest
SHA256 da51e2d0b385c5304e59f6ffd1ba4661684966d8a5b1b714ac59aae86498d6a8
MD5 128439d7891702837915ef815095d8b6
BLAKE2b-256 cea01491b63d3478b5121a3aee4311a7df90cc4185b6ccb03d030c5c3a1b59ee

See more details on using hashes here.

Provenance

The following attestation bundles were made for hayate_openapi-0.7.0.tar.gz:

Publisher: release.yml on hayatepy/hayate-openapi

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file hayate_openapi-0.7.0-py3-none-any.whl.

File metadata

  • Download URL: hayate_openapi-0.7.0-py3-none-any.whl
  • Upload date:
  • Size: 28.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.14

File hashes

Hashes for hayate_openapi-0.7.0-py3-none-any.whl
Algorithm Hash digest
SHA256 5f0a195a578e9ed2f1d2716187141ecb28b537931c255f509e8a9e9f29c6368f
MD5 4f25530597feb5f01ba7ca11cad387c4
BLAKE2b-256 e36131c81df16dd33e9c3f34d59c5ab9a53ec9a39253583d42016e0b91497cb5

See more details on using hashes here.

Provenance

The following attestation bundles were made for hayate_openapi-0.7.0-py3-none-any.whl:

Publisher: release.yml on hayatepy/hayate-openapi

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Release history Release notifications | RSS feed

0.8.2

2 files

0.8.1

2 files

0.8.0

2 files

This release

0.7.0 This release

2 files

0.6.0

2 files

0.5.0

2 files

0.4.2

2 files

0.4.1

2 files

0.4.0

2 files

0.3.1

2 files

0.3.0

2 files

0.2.0

2 files

0.1.1

2 files

0.1.0

2 files

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