{"id":9582,"library":"clarifai","title":"Clarifai Python SDK","description":"The Clarifai Python SDK provides a client library for interacting with the Clarifai platform, enabling developers to build and deploy AI models for computer vision, natural language processing, and multimodal applications. The current version is 12.4.0, and it maintains a rapid release cadence with frequent updates.","status":"active","version":"12.4.0","language":"en","source_language":"en","source_url":"https://github.com/Clarifai/clarifai-python","tags":["AI","ML","computer vision","NLP","multimodal","generative AI","sdk"],"install":[{"cmd":"pip install clarifai","lang":"bash","label":"Install stable version"}],"dependencies":[],"imports":[{"symbol":"ClarifaiClient","correct":"from clarifai.client import ClarifaiClient"},{"symbol":"Input","correct":"from clarifai.client.input import Input"},{"note":"The `clarifai.rest` module and `ClarifaiApp` were removed in v12. Use `clarifai.client.ClarifaiClient` instead.","wrong":"from clarifai.rest import ClarifaiApp","symbol":"ClarifaiApp (deprecated)","correct":"from clarifai.client import ClarifaiClient"}],"quickstart":{"code":"import os\nfrom clarifai.client import ClarifaiClient\nfrom clarifai.client.input import Input\n\n# Initialize the Clarifai client with your Personal Access Token (PAT).\n# It's highly recommended to set CLARIFAI_PAT as an environment variable.\n# Alternatively, you can pass it directly: ClarifaiClient(pat=\"YOUR_PAT_HERE\")\nclarifai_pat = os.environ.get('CLARIFAI_PAT', 'YOUR_CLARIFAI_PAT_HERE')\nif clarifai_pat == 'YOUR_CLARIFAI_PAT_HERE':\n    print(\"WARNING: CLARIFAI_PAT environment variable not set. Using placeholder.\")\n    print(\"Please replace 'YOUR_CLARIFAI_PAT_HERE' or set the environment variable.\")\n\nclient = ClarifaiClient(pat=clarifai_pat)\n\n# Example: Predict with a public image recognition model\nmodel_id = \"general-image-recognition\"\n# model_version_id = \"aa7f35c01e0642fda5ff3a5a3bcce187\" # Optional, uses default if not specified\nimage_url = \"https://samples.clarifai.com/wedding.jpg\"\n\n# Create an input object for prediction\ninput_obj = Input(url=image_url)\n\nprint(f\"Predicting with model '{model_id}' using image from {image_url}...\")\n\ntry:\n    # Get the model and make a prediction\n    model_prediction = client.get_model(model_id).predict(inputs=[input_obj])\n    \n    # Access and print the prediction results\n    if model_prediction.outputs:\n        for concept in model_prediction.outputs[0].data.concepts:\n            print(f\"  {concept.name}: {concept.value:.2f}\")\n    else:\n        print(\"No outputs found in the prediction.\")\n        print(model_prediction) # Print full response for debugging if no outputs\n\nexcept Exception as e:\n    print(f\"An error occurred during prediction: {e}\")\n    if \"Invalid authentication token\" in str(e):\n        print(\"Please check your CLARIFAI_PAT environment variable or provided PAT.\")\n    elif \"Invalid request\" in str(e):\n        print(\"Please check your model ID, version ID, or input format.\")\n","lang":"python","description":"This quickstart demonstrates how to initialize the Clarifai client, load a public image recognition model, and perform a prediction using an image URL. It emphasizes using environment variables for authentication and shows how to process the prediction results."},"warnings":[{"fix":"Migrate all `from clarifai.rest import ClarifaiApp` imports and related usage to `from clarifai.client import ClarifaiClient`. Client initialization and method calls have significantly changed.","message":"The `clarifai.rest` module, including `ClarifaiApp`, was completely removed in version 12. The primary client is now `clarifai.client.ClarifaiClient`.","severity":"breaking","affected_versions":">=12.0.0"},{"fix":"Always use a Personal Access Token (PAT) for new applications. Set it as an environment variable (`CLARIFAI_PAT`) or pass it directly when initializing `ClarifaiClient(pat='YOUR_PAT')`. Ensure the PAT has the necessary scopes for your operations.","message":"Authentication token types (PAT vs. API Key) and initialization methods have evolved. Incorrect or missing tokens are a frequent source of errors.","severity":"gotcha","affected_versions":"All versions, especially >=12.0.0"},{"fix":"Before calling a model's `predict` method, construct an `Input` object (e.g., `Input(url='...')`, `Input(base64='...')`, `Input(file_bytes=...)`) and pass a list of these objects: `model.predict(inputs=[input_obj_1, input_obj_2])`.","message":"Input handling for models requires creating `Input` objects. Directly passing URLs or byte streams to `predict` methods without wrapping them will fail.","severity":"gotcha","affected_versions":">=12.0.0"}],"env_vars":null,"last_verified":"2026-04-17T00:00:00.000Z","next_check":"2026-07-16T00:00:00.000Z","problems":[{"fix":"Update your code to use `from clarifai.client import ClarifaiClient` and the new client API. Consult the v12 migration guide for full details.","cause":"Attempting to import the `clarifai.rest` module, which was removed in Clarifai Python SDK v12.","error":"ModuleNotFoundError: No module named 'clarifai.rest'"},{"fix":"Verify that your `CLARIFAI_PAT` environment variable is correctly set, or that you are passing a valid PAT to `ClarifaiClient(pat='YOUR_PAT')`. Ensure the PAT has sufficient scopes for the operations you are performing.","cause":"The Personal Access Token (PAT) or API Key provided is missing, incorrect, expired, or lacks the necessary permissions.","error":"clarifai_grpc.grpc.status.Status.StatusCode.UNAUTHENTICATED: Invalid authentication token."},{"fix":"Carefully review the arguments passed to your client methods, especially when constructing `Input` objects and calling `predict`. Ensure URLs are valid and accessible, and model IDs/versions are correct.","cause":"The input data or request parameters sent to the Clarifai API are malformed, missing required fields, or do not conform to the expected format (e.g., incorrect input type, bad URL, invalid model ID).","error":"clarifai_grpc.grpc.status.Status.StatusCode.INVALID_ARGUMENT: Invalid request."}]}