Python SDK reference
Look up the options and behaviour of each part of the Python SDK. Install it and moderate your first content from the SDK page.
Configuration
CalmLensServerClient(...) takes these keyword arguments:
| Argument | Default | Meaning |
|---|---|---|
api_key | CALMLENS_API_KEY | Project API key. |
project_id | CALMLENS_PROJECT_ID | Project ID. |
base_url | https://api.calmlens.com/v1 | API endpoint. |
cdn_base_url | https://cdn.calmlens.com/v1 | CDN endpoint. |
timeout | 120 | Seconds a connection may stay silent before sdk/timeout. It limits inactivity, not the whole transfer, so large uploads and downloads are not cut off. |
upload_timeout | None | The timeout of binary uploads (file content and multipart parts); None uses timeout. Text uploads always use timeout. |
connect_timeout | 10 | Seconds to open a connection, including the proxy CONNECT and the TLS handshake. |
max_retries | 2 | Retries for requests that are safe to resend (see Retries and idempotency); 0 disables them. |
max_response_bytes | 33554432 (32 MiB) | The largest API response body the client reads. A larger body, or a larger declared Content-Length before reading, raises sdk/response-too-large. CDN downloads are not limited. |
part_size_bytes | 8388608 (8 MiB) | Content larger than this is uploaded automatically in parts of this size (see Multipart uploads). From 5242880 (5 MiB) to 67108864 (64 MiB). |
transport | Pooled http.client connections | A transport(request, timeout) callable returning a context-managed response with status, headers and read(). |
Invalid options raise CalmLensValidationError with sdk/invalid-config before the client creates any state. client.with_options(timeout=..., upload_timeout=..., max_retries=..., max_response_bytes=...) returns a client that shares the connections and lens cache with other settings, such as client.with_options(timeout=300).cdn.serve(...); options you leave out keep the original client's values.
Client lifecycle
Reuse one client: it keeps HTTP connections open between calls and is thread-safe. Close its connection pool with client.close(), or use it as a context manager (with CalmLensServerClient() as client:). A custom transport stays yours to close. After os.fork(), as in gunicorn, Celery or multiprocessing workers, the child opens its own connections instead of sharing the parent's sockets.
Redirects are not followed, so the API key is only sent to the configured endpoint: a 3xx response raises sdk/http-error with its status and request ID, and is not retried, including for cdn.serve. repr(client) never includes the API key.
API requests send Accept: application/json, and every request sends User-Agent: calmlens-python/<version> (<implementation>/<Python version>), where <version> is the SDK version, calmlens.__version__, and the implementation is a name such as cpython.
Proxies
The default transport follows HTTP_PROXY, HTTPS_PROXY and NO_PROXY, including user:password@ credentials in the proxy URL. HTTPS requests tunnel through the proxy with CONNECT, so TLS runs end to end to CalmLens. Point HTTPS_PROXY at the proxy's http:// address: an https:// proxy URL is not supported for HTTPS requests and raises sdk/invalid-config.
Arguments and responses
Each method takes snake_case keyword arguments, or one options dict with the API's camelCase keys, but not both:
client.assets.retrieve(asset_id=asset_id)
client.assets.retrieve({"assetId": asset_id})With keyword arguments, the keys of nested dicts are converted to camelCase too, except inside metadata values and transform options, which are sent as written. An options dict is sent as given, so write its nested keys in camelCase.
Responses are plain dicts with the API's camelCase field names, typed as TypedDicts in calmlens.types. Fields added in a newer API version stay in the dict, and enum values such as classification.moderation.status, classification.status, type and visibility are plain strings, so a value this SDK version doesn't list never fails. Treat a moderation status you don't recognise as not allowed.
Errors
Every failure raises CalmLensError or a subclass, with a stable code. The constants in calmlens.error_codes are generated from the API's error codes.
| Exception | Raised when | code | status |
|---|---|---|---|
CalmLensAPIError | The API answers with a non-2xx response. | The API's code, such as 404 asset/not-found; sdk/http-error without an error body. | HTTP status |
CalmLensTimeoutError | Connecting or waiting for data timed out. | sdk/timeout | None |
CalmLensTransportError | Any other network or protocol failure: refused, reset, DNS, TLS or a truncated response. | sdk/transport-error | None |
CalmLensValidationError | Arguments or configuration are invalid, before anything is sent, including an options dict key the method doesn't take. It is also a ValueError. | sdk/invalid-argument, sdk/invalid-config, or the API's code for the same problem, such as 400 upload-payload/invalid or 415 upload/unsupported-type. | None |
CalmLensError | A 2xx response isn't the JSON the operation returns, or is larger than max_response_bytes. | sdk/invalid-response, sdk/response-too-large | HTTP status |
CalmLensError | An iterator received a page that says more follow, with a cursor it had already sent; details["cursor"] holds it. | sdk/invalid-response | None |
CalmLensError | assets.wait_for_decision saw no decision within its timeout. | sdk/timeout | None |
WebhookVerificationError | A webhook delivery fails verification or isn't a CalmLens event. | sdk/invalid-webhook-signature, sdk/invalid-webhook-payload | None |
Each error also has:
| Attribute | Value |
|---|---|
message | A readable description. |
details | The API error body's details object, such as validation issues; empty for transport errors. |
request_id | The response's X-Request-Id, or the error body's requestId without one. Quote it to support. |
headers | The response headers, with lower-case names. |
retry_after | Whole seconds from Retry-After, sent in seconds or as an HTTP date. |
is_not_found | Whether the resource doesn't exist. |
is_retryable | Depends only on code and status, so every error class agrees: True for sdk/transport-error, sdk/timeout (including wait_for_decision's), 409 idempotency/request-in-progress and HTTP 429, 500, 502, 503 and 504. |
Transport errors keep the native exception, such as ConnectionResetError, ssl.SSLError, http.client.IncompleteRead or TimeoutError, as __cause__.
Retries and idempotency
The client retries a request only when sending it again can't duplicate work: GET requests, and uploads (assets.create, assets.classify, text.classify and assets.upload_part), which always carry an Idempotency-Key. It retries network failures, timeouts, 429, 500, 502, 503, 504 and 409 idempotency/request-in-progress, up to max_retries times. Retry n, counting from 0, waits between half and all of min(8, 0.5 × 2ⁿ) seconds, or the Retry-After delay when the response sends one. Retry-After: 0 retries at once, and a Retry-After over 60 seconds raises the error instead.
Other writes are never retried: update, move, retry, explain, decide, batch, deletes, folder and hash list changes, cdn.sign_asset_url and lens definitions. cdn.serve retries network failures, timeouts and 429, 500, 502, 503 and 504 the same way, and returns the last response once retries run out.
Each upload call generates one Idempotency-Key and reuses it for its own retries. The API replays the saved response for 24 hours instead of creating a second asset. To retry from another process, pass idempotency_key=, such as a key derived from your own record ID. Reusing a key for a different request raises 409 idempotency/key-reused. A file body is rewound for a retry when it can seek; uploads from streams that can't seek are not retried. See Retrying requests safely.
Assets
page = client.assets.list(page_size=20, sort="-createdAt")
for asset in client.assets.iter(filter="classification.moderation.status==allowed"): # requests pages lazily
print(asset["id"])
in_folder = client.assets.list(filter=f"folderId=={folder_id}") # or folderId==null for the project root
children = client.assets.list_children(asset_id=asset_id)
client.assets.update(asset_id=asset_id, description=None, tags=["reviewed"])
client.assets.move(asset_id=asset_id, folder_id=folder_id)
client.assets.move(asset_id=asset_id, folder_id=None) # project root
client.assets.retry(asset_id=failed_asset_id) # a failed asset whose content is still held
client.assets.remove_content(asset_id=asset_id) # keeps the record and report
client.assets.delete(asset_id=asset_id) # returns None
client.assets.batch(asset_ids=[first_id, second_id], operation="remove-content") # returns {"batchId": ..., "status": "queued", "assetCount": 2}- List pagination starts with no
cursor; see Pagination.sortis a comma-separated list ofcreatedAtandid, each descending when prefixed with-(newest first by default). Filter by folder withfilter="folderId==...". assets.search(query=..., page_size=10)returns the bestpage_sizematches, 1 to 20.assets.iter_timeline(asset_id=...)yields every segment of an audio or video asset's findings timeline, in time order.assets.decide(asset_id=..., decision="blocked", note="Spam")records a reviewer's decision. Purging also needsconfirm_purge=True.assets.explain(asset_id=..., rerun=True)moderates the asset again under the current policy; withoutrerunit returns the policy evidence.assets.batchtakes up to 500 asset IDs and the operation"delete"or"remove-content", and runs in the background once queued.
Waiting for a decision
assets.wait_for_decision(asset_id=..., timeout=60) retrieves the asset at once, then again after 0.5 seconds, waiting 1.5 times longer each time, up to 2 seconds between checks and never past timeout. It returns once classification.status is completed or failed. Without a decision in time it raises CalmLensError with sdk/timeout and details {"assetId": ..., "classificationStatus": ..., "moderationStatus": ...}, the last statuses it saw. It takes no cancellation argument and blocks the calling thread for at most timeout seconds plus one request, so pass a shorter timeout to stop sooner. Webhooks report decisions without polling.
Asset fields
Asset methods return the asset as a dict typed calmlens.types.Asset, such as asset["externalId"] and asset["classification"]["moderation"]["status"]. id, projectId, name, type, createdAt, upload, classification and content are always present. Fields that don't apply inside classification and content are None. metadata and tags are lists, empty when there are none, and visibility is public or private. description, externalId, externalUrl, folderId and parentId may be missing or None, so read them with .get(). Inside classification["moderation"], automatedStatus, source, rule, explanationComplete, categories, linkSafety and raw are None until moderation decides. classification["ocr"] holds the text regions OCR found in an image ({"status": "evaluated", "textRegions": [...]}), and it and the other feature results are None until the feature has run.
See Reading classification results for the report and Asset lifecycle for upload, processing and content statuses, failure reasons and removal reasons.
Uploads
assets.create and assets.classify take exactly one of content, url or part. text.classify(text=...) sends UTF-8 text/plain content. A content or part payload has exactly one of:
| Key | Value |
|---|---|
plainText (plain_text) | A string, sent as UTF-8. |
base64 | Base64 or a data URL. |
data | bytes, bytearray, memoryview, or a binary file object, such as open(path, "rb"), io.BytesIO or a socket file. |
File objects are streamed in chunks, never read into memory. The size is measured for bytes and for files that can seek, from the current position. A stream that can't seek, such as a pipe, socket or generator wrapped in a reader, is sent to its end with chunked transfer encoding unless you pass sizeInBytes; multipart parts from such streams need sizeInBytes. Exactly sizeInBytes bytes are sent, and a stream that ends early raises 400 upload-payload/invalid. Files opened in text mode are rejected. Give a concrete MIME type, such as image/jpeg, on the first request.
Raw uploads carry the asset's other fields and processing options in the query string; uploads from a url send JSON.
Multipart uploads
assets.create and assets.classify split content larger than part_size_bytes (8 MiB by default) into a multipart upload for you, when the client can read it again: bytes, base64 and binary files that can seek. Text and streams that can't seek go in one request. Parts are part_size_bytes long, except the last. Each part is retried on its own with the call's idempotency_key, and part n from the second onwards uses <idempotency_key>:part-<n>, so the key can be at most 240 characters. Parts the API already received are skipped, and the call returns the asset from the final part. If a call fails partway, call it again with the same idempotency_key to resume: the API replays the parts it has and the client sends the rest.
with open("talk.mp4", "rb") as file:
asset = client.assets.create(name="talk.mp4", content={"type": "video/mp4", "data": file},
idempotency_key=f"talk-{upload_id}")To send the parts yourself:
upload = client.assets.create(
name="large.png",
total_size_in_bytes=12 * 1024 * 1024, # optional: checked before any part is stored
part={"type": "image/png", "data": first_part}, # at least 5 MiB
)
completed = client.assets.upload_part(
asset_id=upload["id"],
part={"data": final_part, "partNumber": 2, "isFinal": True},
)Send parts in order; every part but the last is at least 5 MiB. Images can be sent in parts at any size up to 20 MiB, including as one final part. Split the bytes before base64-encoding each part. The API saves whether the upload started with create (returns without waiting) or classify (waits for the decision) and applies it to the final part, from any client or process; pass wait= to upload_part only to override it. With total_size_in_bytes, parts that don't add up to the declared size raise 400 upload/size-mismatch. See Multipart uploads.
Replies in a conversation
For a reply, pass who wrote it and the earlier posts in the conversation, oldest first. CalmLens moderates only the reply, and uses the earlier posts to understand who it addresses and what it refers to. author_id is your own label for a person; CalmLens only compares it with the author IDs of the earlier posts.
asset = client.text.classify(
text="Same to you",
author_id="user-9",
context={"items": [
{"text": "You're an idiot", "authorId": "user-1"},
{"text": "Calm down, both of you"},
]},
)assets.create and assets.classify take the same options. The SDK sends only the posts CalmLens uses: the 5 most recent that fit within 2,000 characters, always including the most recent one. select_moderation_context_items(items) applies the same rule. The asset's authorId and context hold what was used, and context becomes None once the content is removed. See Threaded moderation.
Upload tokens
signed = client.upload_tokens.sign(
upload_preset_id="preset_4Lk8Qw2Xm9Rt5Vb3Nc7P",
external_id="post-42", # optional: the uploaded asset gets this external ID
expires_in=600, # optional: 1 to 3600 seconds, 600 by default
)
# signed["token"], signed["expiresAt"] (milliseconds since the Unix epoch)Signing happens locally with your API key and makes no request. See Upload presets.
Folders
folders.list() returns a cursor page with items, the visible ancestors, nextCursor and hasMore. Set parent_id to open a branch, or omit it for the project root. page_size is 1 to 100 (default 50), and sort is name (the default) or createdAt, descending when prefixed with -. search keeps the children whose names contain it. folders.iter(...) takes the same options and yields every matching folder. folders.move(folder_id=..., parent_id=None) moves a folder to the root. See Organising assets.
Hash lists
A "flag" list reports image uploads that match its entries, and a "block" list also blocks close matches. Add a PDQ hash, or the hash of an image asset in the project:
removed = client.hash_lists.create(name="Removed images", action="block")
client.hash_lists.add_entry(hash_list_id=removed["id"], asset_id=asset_id, label="Report 1042")
entries = client.hash_lists.list_entries(hash_list_id=removed["id"], page_size=50) # entries["nextCursor"]hash_lists.list(cursor=..., page_size=..., sort=...) and hash_lists.list_entries(...) return one page, newest first unless sort says otherwise; hash_lists.iter() and hash_lists.iter_entries(hash_list_id=...) yield every item. hash_lists.delete and hash_lists.remove_entry return None. See Hash lists.
Policies
Policies belong to the API key's organisation, and its projects share them. policies.list(cursor=..., page_size=..., sort=...) returns one page of them, oldest first unless sort says otherwise, and policies.iter() yields every one. policies.retrieve_assigned() returns the policy the project moderates with. A policy made from a preset has preset with its id, the version it is pinned to, the latestVersion and its mode: live follows every preset release, pinned is the organisation's own preset policy kept at one release, and copy is a policy made from a preset. Settings define every category, each with its action, allowances and restrictions. Every change sends the policy's current revision; a policy changed since is refused with 409 policy/revision-conflict.
kids = client.policies.create(preset_id="children-platform", name="Kids section")
kids = client.policies.update(policy_id=kids["id"], revision=kids["revision"], description="Stricter rules")
kids = client.policies.reset(policy_id=kids["id"], revision=kids["revision"],
preset_version=kids["preset"]["version"])
client.policies.assign(policy_id=kids["id"])
assigned = client.policies.retrieve_assigned()policies.reset restores the settings of the preset release a pinned or copy policy is pinned to, and policies.upgrade(policy_id=..., revision=..., preset_version=...) moves an unchanged one to the latest release, given the preset.latestVersion you reviewed. policies.set_automatic_updates(policy_id=..., enabled=..., revision=..., preset_version=...) switches automatic updates on (live) or off (pinned) for the organisation's own preset policy, and policies.delete returns None. See Manage policies in code.
Lenses
client.ensure_lens(identifier="product-thumbnail", name="Product thumbnail", type="image",
active=True, transform={"width": 128})
url = client.cdn.get_asset_url(asset_id=asset_id, resource="rendered", lens="product-thumbnail")ensure_lens and ensure_lenses create each lens, or change it to the definition sent, by identifier; the CDN serves files made with each lens's current definition. They cache each definition by its full content, and concurrent calls for the same definition share one request. A changed definition replaces the cached one for that identifier.
lenses.list(cursor=..., page_size=..., sort=...) returns one page of the project's lenses, newest first unless sort says otherwise, and lenses.iter() yields every one. lenses.create(...) takes a definition whose identifier is new to the project, and raises 409 lens/identifier-taken otherwise. lenses.retrieve(lens_id=...), lenses.update(lens_id=..., ...) and lenses.delete(lens_id=...) read, change and delete a lens by ID, and raise 404 lens/not-found for one the project doesn't have. A lens changed or deleted through these methods is sent again by the next ensure_lens call. See Lenses and transforms.
CDN
url = client.cdn.get_asset_url(asset_id=asset_id, resource="rendered", lens="thumbnail")
with client.cdn.serve(asset_id=asset_id) as response:
if response.ok:
with open("copy.jpg", "wb") as out:
for chunk in response.iter_bytes(): # streamed
out.write(chunk)Name the asset by exactly one of asset_id and external_id (the ID it was uploaded with, ext: in the URL); project_id defaults to the client's. Without resource, get_asset_url returns the asset URL, which every resource is a path beneath, and serve fetches rendered. resource is rendered (the displayable file), preview, original, placeholder, hls, embed or info. Only rendered and preview take a lens, a transform or both; the transform's options replace the lens's options of the same name, and everything applies in one pass. A transform is an options string or an options mapping in the shape of a lens's transform:
client.cdn.get_asset_url(asset_id=asset_id, resource="rendered", transform="format=png,width=1024")
client.cdn.get_asset_url(asset_id=asset_id, resource="rendered",
transform={"format": "png", "width": 1024})A transform is one path segment of options, such as fit=cover,format=webp,width=640, and a lens comes first in the same segment (/rendered/@thumbnail,width=320). The SDK sorts the options of a mapping, writes nested options dotted (border.color), drops the # from hex colours and percent-encodes values, so both calls above return the same URL. The redact option ({"text": True, "personalInformation": True}, or redact.text=true in a selector) blurs the text regions OCR found when the image was processed; see Text regions and redaction.
cdn.serve returns a CDNResponse even for non-2xx responses, after retrying the retryable ones. It has status, ok, headers, content_type, request_id, read(), iter_bytes() and close(); check response.ok and close it, or use it as a context manager.
Signed asset URLs
cdn.sign_asset_url(asset_id=..., expires_in=900, original=False, ...) signs a private asset's URL on your server and returns {"url": ..., "expiresAt": ...}, with expiresAt in milliseconds since the Unix epoch. expires_in is 1 to 86,400 seconds (900 by default). Every resource is a path beneath the URL; /original only with original=True, and otherwise the CDN answers 403 signed-url/original-not-granted. The grant binds the project, the asset, original access and the expiry: a resource, lens, transform or viewer_claims you pass is appended as get_asset_url appends it. See Signed asset URLs.
signed = client.cdn.sign_asset_url(asset_id=asset_id, expires_in=900)
thumbnail = f"{signed['url']}/rendered/@thumbnail"Display info
cdn.get_asset_info(url, viewer_claims=...) reads an asset URL's display info, public or signed: id, projectId, type, externalUrl, content (format, width, height, durationSeconds, pageCount, rendered, preview, hls) and availability (available, age-restricted or unavailable) for the viewer claims, which default to those the URL carries. It sends no API key, as a browser would, and raises CalmLensAPIError with the CDN's code, such as 403 signed-url/expired, when the CDN refuses it.
Viewer claims
The CDN methods take viewer_claims with region and ageConfirmed. An asset whose moderation status is age-restricted is served only with viewer_claims={"ageConfirmed": True}, even with a signed URL or your API key; otherwise the CDN answers 403 with 403 viewer/age-confirmation-required. Set it only where your app has already decided the viewer may see age-restricted content:
client.cdn.get_asset_url(asset_id=asset_id, resource="rendered", lens="thumbnail", viewer_claims={"ageConfirmed": True})The CDN methods add the claims to the URL as a JSON viewerClaims query parameter, leaving out the defaults. They are unsigned, so add them to public and signed URLs alike. See Viewer claims and Age-restricted assets.
Webhooks
parse_webhook_event and verify_webhook_signature accept only the raw str or bytes body and raise TypeError for anything else. A body that was parsed and serialised again rarely has the same bytes (Python writes 1e-07 where the sender wrote 1e-7), so its signature would not match.
| Argument | Meaning |
|---|---|
headers | The request headers: a mapping, an email.message.Message or http.client.HTTPMessage, or a framework's header object. Names match in any case, and a header repeated in the request is joined with ,. |
signature, timestamp | The x-calmlens-signature and x-calmlens-timestamp values, in place of headers. |
secret | The connector's signing secret. |
tolerance_seconds | How far the timestamp may be from now: WEBHOOK_SIGNATURE_TOLERANCE_SECONDS (300) by default. |
now | The current time in Unix seconds, for tests; the system clock by default. |
A signature list may hold several comma-separated v1= entries, and any matching entry is accepted. parse_webhook_event returns a WebhookEventPayload dict, the event exactly as events.list returns it. It raises WebhookVerificationError with sdk/invalid-webhook-signature for a missing, wrong or stale signature, and with sdk/invalid-webhook-payload when the verified body is not a JSON object with string id, type and projectId, a numeric createdAt, and the API's types for its other known fields (each fact needs a string key and a value). Unknown fields, event types and fact keys are kept. Deliveries can arrive out of order, so store the highest assetSequence you have applied for each asset. is_newer_asset_event(event, last_sequence) is False for an event whose assetSequence isn't higher than last_sequence, and True for a newer one, an event that isn't about an asset, or when last_sequence is None.
The header names are exported as constants:
| Constant | Header | Value |
|---|---|---|
WEBHOOK_SIGNATURE_HEADER | x-calmlens-signature | v1= and the hex HMAC-SHA256 of <timestamp>.<raw body>, possibly a comma-separated list. |
WEBHOOK_TIMESTAMP_HEADER | x-calmlens-timestamp | Unix seconds used in the signature. |
WEBHOOK_DELIVERY_HEADER | x-calmlens-delivery | The delivery ID, the same across a delivery's retries. |
To sign deliveries in your own tests, use create_webhook_headers(payload, delivery_id, secret, timestamp=None) or create_webhook_signature_header(payload, timestamp, secret):
from calmlens import create_webhook_headers, verify_webhook_signature
body = b'{"id":"evt_1","type":"asset.allowed"}'
headers = create_webhook_headers(body, "delivery-1", webhook_secret)
assert verify_webhook_signature(body, headers=headers, secret=webhook_secret)Moderation decisions arrive as asset.allowed, asset.limited, asset.age-restricted, asset.blocked and asset.purged events, and asset.classified reports each finished classification. The event's asset.classification.moderation.status holds the status. See Webhooks.
Events feed
events.list reads the project's event feed, the same events webhooks deliver. It returns an EventsPage dict, {"items": [...], "nextCursor": str, "hasMore": bool}. Each event has id, projectId, type, severity, title, description, assetId, asset, facts (a list of {key, value, url?}) and createdAt; description, assetId and asset are None when the event has none. to_webhook_event_payload(event) builds the webhook body for an event.
- Without
cursor, it returns no events and anextCursorfor the current time. Save that cursor: the feed returns only events created after the cursor you pass. - With the previous
nextCursorascursor, it returns the next events, oldest first, at mostpage_size(1 to 100, default 50).nextCursoris neverNone: with no new events, the same cursor comes back. hasMoreisTruewhen more events are already waiting. An invalid cursor raises 400 cursor/invalid.
events.iter_pages(cursor=...) follows nextCursor until a page reports hasMore False, and events.iter(cursor=...) yields the events of those pages. To keep polling, save the last cursor and call them again:
import time
cursor = client.events.list()["nextCursor"] # start from now; no events
while True:
for page in client.events.iter_pages(cursor=cursor, page_size=50):
for event in page["items"]:
handle(event)
cursor = page["nextCursor"] # persist to resume after a restart
time.sleep(5)See Events for event types and facts.