Clarifai Protocol
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
-
ModuleNotFoundError: No module named 'clarifai_grpc.grpc.api.resources_pb2'
cause The `clarifai-protocol` library is not installed, or the import path is incorrect.fixEnsure the library is installed with `pip install clarifai-protocol`. Verify that the import path is `from clarifai_grpc.grpc.api...` and not `from clarifai_protocol.grpc.api...`. -
AttributeError: 'Image' object has no attribute 'image_url'
cause A Protobuf message field has been renamed or removed in a newer version of the protocol definition.fixConsult the `clarifai-protocol` documentation or the `.proto` files in the GitHub repository for the correct field name. For example, `image_url` might have been changed to simply `url`.
Warnings
- breaking Changes to the underlying Clarifai Protocol Buffer (.proto) definitions directly lead to breaking changes in the generated Python classes (e.g., field renames, type changes, field removals).
- gotcha The `clarifai-protocol` library is NOT the high-level Clarifai Python Client Library (which is typically `clarifai` or `clarifai-client`). This library provides the low-level gRPC/Protobuf definitions.
- gotcha Confusing `*_pb2` and `*_pb2_grpc` modules. `_pb2` modules contain the generated Protobuf message classes (data structures), while `_pb2_grpc` modules contain the generated gRPC service stubs (client interfaces) and servicer classes (server interfaces).
Install
-
pip install clarifai-protocol
Imports
- Image
from clarifai_protocol.grpc.api.resources_pb2 import Image
from clarifai_grpc.grpc.api.resources_pb2 import Image
- V2Stub
from clarifai_protocol.grpc.api.service_pb2_grpc import V2Stub
from clarifai_grpc.grpc.api.service_pb2_grpc import V2Stub
- UserAppIDSet
from clarifai_grpc.grpc.api.resources_pb2 import UserAppIDSet
Quickstart
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}")