Skip to main content

z4j-fastapi

PyPI version Python License

The FastAPI framework adapter for z4j.

Adds the z4j agent to a FastAPI app through z4j_lifespan(...) passed to FastAPI(lifespan=...). Register engines by passing their native handles (celery_app=, rq_app=, huey=, and so on); installing an adapter package alone does not select it. Dramatiq is the exception only when its process-global broker already has registered actors.

Compatibility

  • FastAPI 0.109.1+ (no upper cap)
  • Python 3.11+

Pair with an engine adapter (z4j-celery, z4j-rq, z4j-dramatiq, z4j-huey, z4j-arq, z4j-taskiq); each engine adapter carries its own upstream floor.

Full per-adapter matrix at https://docs.z4j.com/reference/compatibility/.

What it ships

  • One-line install, pass z4j_lifespan(...) to FastAPI(lifespan=...) and the agent connects on the next uvicorn worker boot
  • Explicit engine registration, supports multiple native engine handles in one process; adapter installation without the corresponding handle is skipped
  • Loop-safe TaskIQ discovery, taskiq_broker= attaches z4j middleware; TaskIQ broker startup records the actual owner loop
  • Per-task metadata from supporting engine adapters; z4j-celery, z4j-rq, and z4j-dramatiq expose @z4j_meta rather than this framework package defining one
  • Service-user safe, auto-relocates the local outbound buffer to $TMPDIR/z4j-{uid} when $HOME is unwritable (uvicorn under a service account, distroless images, etc.)

Install

pip install z4j-fastapi z4j-celery z4j-celerybeat

Wire it into your app:

from fastapi import FastAPI
from myproject.celery import app as celery_app
from z4j_fastapi import z4j_lifespan

# reads Z4J_TOKEN, Z4J_HMAC_SECRET, Z4J_BRAIN_URL, Z4J_PROJECT_ID from env
app = FastAPI(lifespan=z4j_lifespan(celery_app=celery_app))

Pass explicit config as kwargs (z4j_lifespan(brain_url=..., token=..., project_id=..., hmac_secret=...)) instead of env vars if you prefer. Apps that already own their lifespan can wrap it via inner_lifespan=; apps that cannot use lifespan at all can call install_z4j(app) instead.

With taskiq_broker=, both installation forms attach z4j's middleware but do not assume that FastAPI's ambient loop owns the supplied broker. TaskIQ's later broker startup binds the real owner loop. Until that binding occurs, TaskIQ submit and result probes fail closed. Attach before host startup; if the broker was already started elsewhere, use the direct adapter API and pass its verified live broker_loop explicitly.

Mint the agent from the dashboard's Agents page and retain both values it shows: the bearer token and the HMAC secret.

Scope and worker processes

This package connects the web process and its configured queue handles. Task execution still happens in the queue's worker process, which needs its own engine adapter and agent configuration. It does not provide HTTP request tracing or automatically capture FastAPI BackgroundTasks. Use a supported queue engine for the task lifecycle and controls shown in z4j.

Reliability

  • Agent startup and delivery failures are logged and isolated from FastAPI request handlers and worker code; capture hooks make no brain network request inline.
  • Engine event queues and the SQLite outbound buffer are bounded. A full engine queue loses an event (the Celery, RQ and Dramatiq adapters evict the oldest queued event; the Huey, arq and TaskIQ adapters drop the new one) and buffer pressure evicts oldest rows; both losses are logged.

Documentation

Full docs at docs.z4j.com/frameworks/fastapi/.

License

Apache-2.0, see LICENSE.

Metadata

Release files for z4j-fastapi 1.13.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 z4j-fastapi 1.13.0
File Size Uploaded
z4j_fastapi-1.13.0.tar.gz 25.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for z4j-fastapi 1.13.0
File Interpreter ABI Platform
z4j_fastapi-1.13.0-py3-none-any.whl Python 3 none any Details

Total release size: 47.0 kB

Release files / z4j_fastapi-1.13.0.tar.gz

Download URL z4j_fastapi-1.13.0.tar.gz
Size 25.6 kB
Tags Source
SHA-256 checksum
How to use checksums
cbce553a5bc9acb9e8627be96e9020e6b1fb6301cad99505d1df38edf41f15ac
BLAKE2b-256 checksum
How to use checksums
c8a53718f4c90b1ead2879cc8a87b082e0b17bb76722dae6c37794f6899855fc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.7

Release files / z4j_fastapi-1.13.0-py3-none-any.whl

Download URL z4j_fastapi-1.13.0-py3-none-any.whl
Size 21.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
abb9cb27c94eb3c0f6dc215eed0ef24ec8842332d618bc957eb8bad2276c1c81
BLAKE2b-256 checksum
How to use checksums
c5b7ac14961dc70abb257a5a5b6cafeb6f5ac6f14f410592422ec08f52382605
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.7

Release history Release notifications | RSS feed

This release

1.13.0 This release

2 release files

1.11.0

2 release files

1.10.0

2 release files

1.9.1

2 release files

1.9.0

2 release files

1.8.0

2 release files

1.7.0

2 release files

1.6.9

2 release files

1.6.8

2 release files

1.6.7

2 release files

1.6.0

2 release files

1.5.0

2 release files

1.4.0

2 release files

1.3.0

2 release files

1.2.0

2 release files

1.1.2

2 release files

1.1.0

2 release files

1.0.4

2 release files

1.0.3

1 release file

1.0.2

2 release files

1.0.1

2 release files

1.0.0

2 release 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