coze-coding-utils
Python utilities for the Coze coding client. Provides a simple runtime Context object and an async-task subsystem.
Installation
pip install coze-coding-utils
Modules
runtime_ctx
from coze_coding_utils.runtime_ctx.context import Context
ctx = Context(run_id="r", space_id="s", project_id="p", method="fetchData")
print(ctx.run_id, ctx.method)
async_tasks
Per-process async task runtime backed by PostgreSQL (single table, JSONB payload, partial indexes). Public API:
from coze_coding_utils.async_tasks import (
AsyncTaskRuntime, # construct one per process
AsyncTask, # SQLAlchemy ORM (single PK on task_id)
AsyncTaskStatus, # pending / running / completed / failed / timeout
AsyncTaskBase, # DeclarativeBase if caller needs to drive DDL
AsyncTaskStorageError, # raised on DB init/query failure (caller turns into 503)
config, # env-overridable constants module
)
runtime = AsyncTaskRuntime(
session_factory=get_session, # Callable[[], sqlalchemy.orm.Session]
engine=get_engine(), # sqlalchemy.engine.Engine
graph=async_graph, # any object with .ainvoke(payload, config=...)
checkpointer=checkpointer, # any LangGraph-compatible checkpointer
)
# Lifespan / FastAPI usage:
async with lifespan(...):
await runtime.submit(payload={...}, biz_context={"x-user-id": "u1"}, deadline_sec=600)
row = await runtime.get(task_id)
...
await runtime.shutdown()
The runtime is intentionally framework-agnostic:
- No FastAPI dependency — caller maps
AsyncTaskStorageErrorto HTTP 503 themselves. - No LangGraph dependency in
[project.dependencies]— the runtime only callsgraph.ainvoke(payload, config=run_config)and holds thecheckpointerreference for resume scenarios.
Caller contracts (pre-conditions)
The runtime makes three assumptions about the caller's environment. Violating any one will silently break checkpoint cleanup or task lifecycle:
- Checkpointer compatible with PG. Construct the LangGraph with a checkpointer that writes to the schema configured by
COZE_ASYNC_TASK_CHECKPOINT_SCHEMA(defaultmemory), tablescheckpoints/checkpoint_writes.langgraph-checkpoint-postgresworks out of the box. thread_id == task_id. The runtime fixes{"configurable": {"thread_id": task_id}}when callinggraph.ainvoke. Thememory_cleanup_oncescan deletes orphan checkpoint rows by joining on this equality. Don't overridethread_idin your graph.- Session schema access. The injected
session_factorymust produce sessions with read+write privileges onCOZE_ASYNC_TASK_CHECKPOINT_SCHEMAschema (so cleanup can DELETE orphan rows).
Environment variables
All prefixed with COZE_ASYNC_TASK_. Override only when there's a specific ops reason.
| Var | Default | Meaning |
|---|---|---|
HEARTBEAT_INTERVAL_SEC |
30 | Per-task heartbeat write frequency |
HEARTBEAT_FAIL_THRESHOLD |
3 | Consecutive failures → task self-aborts |
ORPHAN_TIMEOUT_SEC |
300 | Running → failed after this many seconds of silent heartbeat |
MAX_CLAIM_COUNT |
3 | max claim attempts before a row is left for scans to reap |
DEFAULT_DEADLINE_SEC |
86400 | 24h wall-clock cap; override per task via deadline_sec |
CLEANUP_INTERVAL_SEC |
21600 | memory cleanup loop period |
ORPHAN_SCAN_INTERVAL_SEC |
60 | orphan scan period |
DEADLINE_SCAN_INTERVAL_SEC |
60 | deadline scan period |
MAX_CONCURRENT_PER_INSTANCE |
50 | in-process concurrency semaphore |
SCAN_BATCH_LIMIT |
500 | rows per orphan/deadline scan tick |
MEMORY_CLEANUP_BATCH |
200 | rows per memory.checkpoints cleanup sub-batch |
MEMORY_CLEANUP_MAX_ITERS |
50 | max batches per cleanup tick before deferring |
RECURSION_LIMIT |
100 | LangGraph runtime recursion ceiling |
CHECKPOINT_SCHEMA |
memory |
PG schema for LangGraph checkpoint tables |
TASK_RETENTION_DAYS |
7 | terminal-status rows older than this are DELETEd |
TASK_CLEANUP_INTERVAL_SEC |
3600 | task_cleanup_loop tick period |
TASK_CLEANUP_BATCH |
500 | max rows DELETEd per task_cleanup tick |
The runtime lazily issues CREATE TABLE IF NOT EXISTS on the first submit / get, so a fresh deployment needs no manual DDL.
Python Version
Requires Python 3.10+.
License
MIT
Metadata
Release files for coze-coding-utils 0.2.8
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| coze_coding_utils-0.2.8.tar.gz | 71.2 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| coze_coding_utils-0.2.8-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 160.8 kB
Release files / coze_coding_utils-0.2.8.tar.gz
| Download URL | coze_coding_utils-0.2.8.tar.gz |
|---|---|
| Size | 71.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
3aadf17975be046c5bde18732cb6a0cb956ff6124746af3fefe214787fb2618b
|
|
BLAKE2b-256 checksum How to use checksums |
5f18241ed27c827d0edc18765ad0e62d2922e2787c63dec3dbc0a37c0dd747ea
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.9.6
|
Release files / coze_coding_utils-0.2.8-py3-none-any.whl
| Download URL | coze_coding_utils-0.2.8-py3-none-any.whl |
|---|---|
| Size | 89.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
6e49bf2bdc2f2b85c1a37c7eaa4f1d80b843638ba691da15a7e3e982225e6f09
|
|
BLAKE2b-256 checksum How to use checksums |
44d33dd67badde9219c0ccccbab5625ab24b3a76e3f1ca9bc9c0caf5d447af27
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.9.6
|