Clarifai gRPC API Client
The official Clarifai gRPC Python client provides an interface for interacting with the Clarifai API, offering powerful AI capabilities for computer vision and natural language processing. Currently at version 12.3.0, the library maintains a frequent release cadence, often aligning its major and minor versions with the backend API.
Warnings
- breaking The `clarifai-grpc` library does not adhere to strict semantic versioning. The first two version numbers (X.Y) align with the backend API version, meaning even minor version changes can introduce breaking changes.
- breaking As of June 11th, 2025, there are important changes to the use of PATs (Personal Access Tokens) and API keys, which may not be backward compatible.
- gotcha When using PATs (Personal Access Tokens) for authentication, you must explicitly specify both your `user_id` and the `app_id` to which the request should be applied. App-specific API keys do not require this. PATs are also required for certain API operations like creating new applications.
- deprecated The `clarifai-python-utils` repository is deprecated. While `clarifai-grpc` remains available for granular API access, Clarifai encourages the use of the newer, object-oriented `clarifai-python` SDK (which wraps `clarifai-grpc`) for most use cases to simplify AI workflows.
- breaking For applications created via the API without explicitly specifying a base workflow, the default changed from 'General' to 'Universal' on July 2nd, 2024. This affects the default behavior of newly created applications.
Install
-
pip install clarifai-grpc
Imports
- ClarifaiChannel
from clarifai_grpc.channel.clarifai_channel import ClarifaiChannel
- V2Stub
from clarifai_grpc.grpc.api import service_pb2_grpc stub = service_pb2_grpc.V2Stub(...)
- resources_pb2
from clarifai_grpc.grpc.api import resources_pb2
- service_pb2
from clarifai_grpc.grpc.api import service_pb2
- status_code_pb2
from clarifai_grpc.grpc.api.status import status_code_pb2
Quickstart
import os
from clarifai_grpc.channel.clarifai_channel import ClarifaiChannel
from clarifai_grpc.grpc.api import resources_pb2, service_pb2, service_pb2_grpc
from clarifai_grpc.grpc.api.status import status_code_pb2
# Your Clarifai PAT (Personal Access Token) and Application ID
CLARIFAI_PAT = os.environ.get('CLARIFAI_PAT', '')
CLARIFAI_APP_ID = os.environ.get('CLARIFAI_APP_ID', 'YOUR_CLARIFAI_APP_ID')
# Initialize the gRPC client
channel = ClarifaiChannel.get_grpc_channel()
stub = service_pb2_grpc.V2Stub(channel)
metadata = (('authorization', 'Key ' + CLARIFAI_PAT),)
userDataObject = resources_pb2.UserAppIDSet(user_id='YOUR_USER_ID', app_id=CLARIFAI_APP_ID)
# Example: Predict concepts in an image URL
image_url = 'https://samples.clarifai.com/metro-north.jpg'
post_model_outputs_response = stub.PostModelOutputs(
service_pb2.PostModelOutputsRequest(
# You can use your own model ID or a public one like 'general-image-recognition'
model_id='general-image-recognition',
user_app_id=userDataObject,
inputs=[
resources_pb2.Input(
data=resources_pb2.Data(
image=resources_pb2.Image(url=image_url)
)
)
]
),
metadata=metadata
)
if post_model_outputs_response.status.code != status_code_pb2.SUCCESS:
raise Exception(
f"Post model outputs failed, status: {post_model_outputs_response.status.description}"
)
# Print the results
print('Predicted concepts:')
for concept in post_model_outputs_response.outputs[0].data.concepts:
print(f"{concept.name}: {concept.value:.4f}")