Skip to main content

Sokel Plugin SDK — Python

简体中文

Write Sokel plugins in Python. The contract lives in a language-neutral manifest.yml; sokel-gen turns it into pydantic models and typed registration functions, and the SDK handles registration, transport, credentials, files, heartbeats and reconnects.

async def issues_list(ctx: Ctx, in_: IssuesListIn) -> IssuesListOut:
    issues = await client.list_issues(in_.project, in_.state)
    return IssuesListOut(issues=issues, count=len(issues))

on_issues_list(p, issues_list)

A typo in in_.project is a red squiggle in your editor, not a failed call in production — there is no dict["key"] anywhere in your code.

Install

pip install sokel-plugin-sdk
go install github.com/sokel-dev/sokel-plugin-sdk/cmd/sokel-gen@latest   # the generator

sokel-gen is a single binary (written in Go) used only at generation time; running a plugin does not need it.

Four steps

sokel-gen init -lang python ./my-plugin
cd my-plugin
pip install -r requirements.txt
sokel-gen generate .     # manifest.yml → sokel_gen.py
python main.py
  1. Declare — manifest.yml: operations, events, credentials, authentication. Format: docs/manifest.md, or run sokel-gen docs.
  2. Generate — sokel-gen generate . writes sokel_gen.py: an XxxIn / XxxOut model pair and an on_xxx(p, fn) per operation; a payload model and a trigger_xxx(ctx, event_id, payload) per event.
  3. Implement — handler signatures are fully concrete. async def or a plain function, both work.
  4. Connect — asyncio.run(p.run()). A plugin dials out: no inbound port, no public IP, no firewall hole.

What you can do

Task How
Read credentials credential(ctx) → the generated Credential model
Read an input file's bytes await ctx.fetch(in_.file)
Produce a file await ctx.upload(name, mime, data), or await ctx.upload_file(path) for large files
Stream output out.text(...) frame by frame for humans, out.vars(Out(...)) for downstream nodes
Push an event await trigger_message(ctx, event_id, MessageEvent(...))
Long-running event source p.register_source(id, label, fn); loop while not ctx.stopping.is_set()
Handle a platform-relayed webhook p.register_webhook(fn), return ok() / text(401, "...")
Collaborative authentication p.register_auth(start=…, poll=…, submit=…)
Refresh a session credential await ctx.update_credential({"session": "…"})
Report runtime state ctx.report_status("auth_required", "…")

upload_file(path) streams from disk: memory stays at one chunk (1 MiB) regardless of file size. Anything above a few hundred megabytes should use it — upload(bytes) reads the whole file into memory first, and the symptom of that is a container mysteriously killed by the OOM reaper.

Configuration

The SDK reads SOKEL_-prefixed environment variables:

Variable Required Meaning
SOKEL_ENDPOINT yes nats://broker:4222, or an https:// platform URL to discover the broker from
SOKEL_BROKER_NETWORK no Empty / public: use the advertised broker; internal: require the platform's nats.internal_url
SOKEL_TOKEN yes Access-group token (skp_…) identifying plugin + workspace
SOKEL_NATS_TOKEN no Broker-level auth
SOKEL_NATS_CA no Custom CA bundle for tls:// brokers
SOKEL_INSTANCE_ID no Pin a replica identity (otherwise derived from the token and cached on disk)
SOKEL_REGION no Region label for the replica

Credentials are never stored by the plugin: the platform injects the resolved fields with each call.

For a replica in the platform's Kubernetes cluster, set SOKEL_BROKER_NETWORK=internal and keep SOKEL_ENDPOINT pointing to the platform's HTTP URL. The platform supplies a private address of the same broker together with the existing group credentials, CA and inbox prefix. The internal address must be one nats:// or tls:// host, optionally with a port; credentials, paths and server lists are rejected. A missing or invalid internal address stops startup instead of falling back to public. Internal mode ignores NATS connect_urls updates, so reconnects stay on the private address. External replicas leave this variable empty or use public. Internal mode requires HTTP discovery; the legacy direct NATS endpoint does not supply internal_url.

Connection recovery

Registration renewals use the existing NATS connection about every 20 seconds and wait up to 8 seconds for a reply. Transport PINGs run every 10 seconds with at most two outstanding PINGs. The native NATS client reconnects on socket errors or missing PONGs and restores subscriptions; a platform reply timeout alone keeps the connection intact. Heartbeat errors include connection state and elapsed time. Upgrade the SDK dependency and rebuild the plugin image to apply these defaults.

Example

examples/kitchen-sink covers every shape — each field type, files, streaming, events, webhooks, collaborative auth — and the Python and Node implementations share one declaration.

cd examples/kitchen-sink/python
pip install -r requirements.txt
SOKEL_ENDPOINT=nats://localhost:4222 SOKEL_TOKEN=skp_xxx python main.py

Developing this SDK

uv venv && uv pip install -e '.[dev]'
python -m pytest -q

The internal-network adapter uses nats-py's private _process_info hook because the library has no public switch to disable discovered servers. Keep the initial/updated INFO and real TCP reconnect tests when updating that dependency; public connections use the unmodified client. The adapter preserves both the synchronous hook in nats-py 2.6 and the asynchronous hook in 2.16.

License

Apache-2.0.

Metadata

Release files for sokel-plugin-sdk 0.8.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 sokel-plugin-sdk 0.8.0
File Size Uploaded Provenance Actions
sokel_plugin_sdk-0.8.0.tar.gz 47.3 kB With provenance Details for sokel_plugin_sdk-0.8.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for sokel-plugin-sdk 0.8.0
File Interpreter ABI Platform Actions
sokel_plugin_sdk-0.8.0-py3-none-any.whl Python 3 none any Details for sokel_plugin_sdk-0.8.0-py3-none-any.whl

Total release size: 85.1 kB

Release files / sokel_plugin_sdk-0.8.0.tar.gz

Download URL sokel_plugin_sdk-0.8.0.tar.gz
Size 47.3 kB
Tags Source
SHA-256 checksum
How to use checksums
811f3fe0b7c3705c43e6570dad16dd715141937fcb035d42388b12f5c23165c9
BLAKE2b-256 checksum
How to use checksums
8d7aceb0f5c3dabd10c7a4c45368bd5513023cd3f9d0394691883fb292d80d36
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 12, 2026.

Transparency log

Release files / sokel_plugin_sdk-0.8.0-py3-none-any.whl

Download URL sokel_plugin_sdk-0.8.0-py3-none-any.whl
Size 37.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e088c6754cc3157669bccbdb352f5e7a03073912ce749c21bf9e093d58a5f3db
BLAKE2b-256 checksum
How to use checksums
c48b5c1b6eb7667dfe506384d4acdfaa8a35eb4d89d3134ea8452b3c01156379
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 12, 2026.

Transparency log

Security for sokel-plugin-sdk 0.8.0

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

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for sokel_plugin_sdk-0.8.0.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 12, 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