Skip to main content

Clavata SDK for Python

Project description

SDK

This is the Clavata SDK for Python.

Quick Start

For most users, you'll be using the default production API, so unless you've been told otherwise, we recommend using the steps below to create a client.

Set your Clavata API Key in the environment:

export CLAVATA_API_KEY="YOUR_API_KEY"

Then, you can instantiate our client with default settings:

from clavata_sdk import ClavataClient

client = ClavataClient.create()

If you wish, you can also inject the API token directly:

from clavata_sdk import ClavataClient

client = ClavataClient.create(auth_token="API_KEY")

Custom host/port

If you've been given a different host and port to connect to, you'll instantiate the client as follows:

from clavata_sdk import ClavataClient

Next, you'll need a Clavata API key to instantiate the client:

api_key = "YOUR_API_KEY"

# Now instantiate the client with your API key:
client = ClavataClient(host="gateway.app.clavata.ai", port=443, auth_token=api_key)

As above, the auth_token parameter can be omitted if your API key is set in the environment before instantiation.

Basic Use

The client includes methods for evaluation and job retrieval. It also provides access to the request and response types you'll need to pass in.

from clavata_sdk import CreateJobRequest, EvaluateRequest, ContentData

# Async request
response = client.create_job(CreateJobRequest(content=ContentData(...), ...))

# Streaming request
async for response in client.evaluate(EvaluateRequest(content="The quick brown fox", ...)):
    print(response)

Webhooks

The client.create_job() method is an async endpoint, which means the job will be started but you'll receive a response before the job has completed. This response is a Job and will include the job's ID and status. You can use the job ID to get the results later using the get_job() method, but if you'd like to be notified when the job is complete (so you don't have to "poll"), you can add a "webhook". A webhook is a URL that Clavata's platform will make a request to when the job is complete.

To add a webhook to a create_job() request, do the following:

from clavata_sdk import ClavataClient, CreateJobRequest

client = ClavataClient.create()
request = CreateJobRequest(content="The quick brown fox", ...).add_webhook(url="https://youdomain.com/path/to/hook")
job = client.create_job(request)

print(job)

You can call add_webhook on the request object and provide a URL to call. Clavata will make a POST request to this URL when the job is complete. You can also optionally add an extra_headers named parameter and provide a dictionary of key/value pairs. Clavata will add these headers to the POST request, if provided.

Dealing with Refusal Errors

Under certain circumstances, the Clavata API may refuse to evaluate a piece of content. Reasons include:

  • The content (image) was found in a database of known CSAM material
  • The content (image) is in a format that is not currently supported (webp, png, jpg supported as of publishing)
  • The content is corrupt, incomplete or otherwise invalid

When the API refuses, an exception will be raised by the SDK. Because exceptions may also be raised for other reasons (i.e., network errors, invalid requests, etc), the SDK provides a distinct error type that both helps you tell when an error is due to a refusal, and also allows you to get more information about why the request was refused.

The EvaluationRefusedError type will only be raised if an evaluation was refused for one of the reasons mentioned above.

When a refusal occurs, the response from the API is processed to hydrate this error with information on which pieces of content included in the request were refused, and why each one was refused (if there was more than one piece of content in the request which was refused).

The simplest way to examine refusals is to look at the refusals attribute on the error type. It will always be a list containing one entry for each piece of content that was refused. Each entry is of type RefusedContent and includes a reason as well as the content_hash that identifies the piece of content.

try:
    client.evaluate(...)
except EvaluationRefusedError as err:
    print("Refused content: ", err.refusals)

The error type also comes with a number of additional properties that are useful if you don't want to have to iterate over the entire list of refusals (for example, if you only sent 1 piece of content in the request). These properties are

  • top_reason: Returns the "most important" reason for the refusal, with CSAM > UNSUPPORTED_FORMAT > INVALID_CONTENT.
  • most_common_reason: If there are many refusals, returns the most common reason among them. In math terms, the "mode".
  • first_reason: Always returns the first reason in the list, regardless of how many refusals there were.

You can also access all the content that was refused in a flattened list by using the refused_content_hashes property on the error type.

try:
    client.evaluate(...)
except EvaluationRefusedError as err:
    print("Refused content: ", err.refusals)

    # Get the first refusal reason
    print("First refusal reason: ", err.first_reason)

    # Get all the content that was refused
    print("Refused hashes: ", err.refused_content_hashes)

Finally, the refusal "reasons" are implemented as a python enum, allowing you to easily compare the "reason" in the error to the canonical "reason" values to determine why the content was refused (and then what action to take).

from clavata_sdk import RefusalReason, EvaluationRefusedError
try:
    client.evaluate(...)
except EvaluationRefusedError as err:
    if err.first_reason == RefusalReason.CSAM:
        # Now you know that CSAM was found. Act accordingly.
    # etc ...

Project details


Download files

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

Source Distribution

clavata_sdk-1.0.1.tar.gz (76.6 kB view details)

Uploaded Source

Built Distribution

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

clavata_sdk-1.0.1-py3-none-any.whl (121.9 kB view details)

Uploaded Python 3

File details

Details for the file clavata_sdk-1.0.1.tar.gz.

File metadata

  • Download URL: clavata_sdk-1.0.1.tar.gz
  • Upload date:
  • Size: 76.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.2.0 CPython/3.12.8 Darwin/25.0.0

File hashes

Hashes for clavata_sdk-1.0.1.tar.gz
Algorithm Hash digest
SHA256 27b44b56f3a338dfc4f89ffbf4f7927cef293f0e104d84bac4be59f6bf1591bf
MD5 c24c3b963e74b6e56b4b98cfb9ad21ac
BLAKE2b-256 76112f5dc1c69a27f79e11c72afdb1fcd30325b1952131900822beed0b3d68ba

See more details on using hashes here.

File details

Details for the file clavata_sdk-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: clavata_sdk-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 121.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.2.0 CPython/3.12.8 Darwin/25.0.0

File hashes

Hashes for clavata_sdk-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 f46efc95b495ede70a6c47679d2c560eca3b9dae0fc17aa0ff9922f4e86ab463
MD5 7910650e094bd169f090007c94c39268
BLAKE2b-256 3e73e6a6372b2e1e4827b88623f06b96703be2312d518f66f37c2b26c65a7076

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page