Clarifai Protocol

0.0.61 · active · verified Thu Apr 16

clarifai-protocol is a Python library that provides the core Protocol Buffer definitions and generated Python code for interacting with Clarifai's gRPC services. It encapsulates the data structures (messages) and service interfaces (stubs) used for communication within the Clarifai platform, especially relevant for custom model deployment and advanced integrations. The current version is 0.0.61, and it follows a frequent release cadence to align with protocol definition updates.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to import and instantiate core Protocol Buffer messages like `Image` and `UserAppIDSet`, and how to access a gRPC service stub like `V2Stub`. While it shows creating a request message, actual API interaction requires setting up a gRPC channel and credentials, which falls outside the scope of `clarifai-protocol` itself, but relies on its definitions.

from clarifai_grpc.grpc.api.resources_pb2 import Image, UserAppIDSet
from clarifai_grpc.grpc.api.service_pb2 import PostModelOutputsRequest
from clarifai_grpc.grpc.api.service_pb2_grpc import V2Stub
import grpc
import os

# This library primarily provides the definitions. Actual API calls require grpcio and a channel.

# Example: Instantiate a protobuf message
image_message = Image(url="https://samples.clarifai.com/face-det.jpg")
print(f"Created Image message with URL: {image_message.url}")

# Example: Create a stub (though actual usage requires a gRPC channel and credentials)
# For demonstration purposes, we'll just show the import and instantiation of the stub class.
# In a real scenario, you'd pass a channel: V2Stub(grpc.insecure_channel('api.clarifai.com:443'))
try:
    channel = grpc.insecure_channel('api.clarifai.com:443') # Using a dummy channel for concept
    stub = V2Stub(channel)
    print(f"Successfully created a V2Stub instance.")
    channel.close()
except Exception as e:
    print(f"Could not create a real gRPC channel for demonstration: {e}")
    print("Stub instantiation shown for clarity, but requires a functional gRPC channel.")

# Example: A request message, typically constructed using these resources
request = PostModelOutputsRequest(
    user_app_id=UserAppIDSet(user_id=os.environ.get('CLARIFAI_USER_ID', ''), app_id=os.environ.get('CLARIFAI_APP_ID', '')),
    model_id="my-model",
    inputs=[] # inputs would go here
)
print(f"Created PostModelOutputsRequest for model: {request.model_id}")

view raw JSON →