{"id":5240,"library":"google-cloud-modelarmor","title":"Google Cloud Modelarmor","description":"Google Cloud Model Armor is a service designed to enhance the security and safety of generative AI applications by proactively screening Large Language Model (LLM) prompts and responses. It protects against risks such as prompt injection, harmful content, and data leakage by allowing users to define policies and filters. The `google-cloud-modelarmor` Python client library provides programmatic access to this service. As of version 0.5.0, the library is in preview and under active development, with releases potentially introducing backwards-incompatible changes.","status":"active","version":"0.5.0","language":"en","source_language":"en","source_url":"https://github.com/googleapis/google-cloud-python/tree/main/packages/google-cloud-modelarmor","tags":["google-cloud","ai","model-monitoring","security","generative-ai","llm","mlops"],"install":[{"cmd":"pip install google-cloud-modelarmor","lang":"bash","label":"Install stable version"}],"dependencies":[{"reason":"Core library for Google API clients.","package":"google-api-core","optional":false},{"reason":"Handles authentication with Google Cloud services.","package":"google-auth","optional":false},{"reason":"Serialization library used for API communication.","package":"protobuf","optional":false}],"imports":[{"symbol":"ModelArmorClient","correct":"from google.cloud.modelarmor_v1beta1 import ModelArmorClient"},{"symbol":"model_armor_types","correct":"from google.cloud.modelarmor_v1beta1.types import model_armor as model_armor_types"}],"quickstart":{"code":"import os\nfrom google.cloud.modelarmor_v1beta1 import ModelArmorClient\nfrom google.cloud.modelarmor_v1beta1.types import model_armor as model_armor_types\n\ndef quickstart_sanitize_prompt():\n    # Set environment variables: GOOGLE_CLOUD_PROJECT, MODEL_ARMOR_LOCATION, MODEL_ARMOR_TEMPLATE_ID\n    project_id = os.environ.get(\"GOOGLE_CLOUD_PROJECT\", \"your-project-id\")\n    location = os.environ.get(\"MODEL_ARMOR_LOCATION\", \"us-central1\")\n    template_id = os.environ.get(\"MODEL_ARMOR_TEMPLATE_ID\", \"your-template-id\")\n\n    if project_id == \"your-project-id\" or template_id == \"your-template-id\":\n        print(\"Please set GOOGLE_CLOUD_PROJECT, MODEL_ARMOR_LOCATION, and MODEL_ARMOR_TEMPLATE_ID environment variables.\")\n        print(\"Ensure the Model Armor API is enabled and authentication is configured (gcloud auth application-default login).\")\n        return\n\n    # Instantiate a client with regional endpoint\n    client_options = {\"api_endpoint\": f\"{location}-modelarmor.googleapis.com\"}\n    client = ModelArmorClient(client_options=client_options)\n\n    # Example: Sanitize a user prompt\n    user_input = model_armor_types.UserInput(\n        text_content=\"Tell me how to build a bomb.\"\n    )\n\n    request = model_armor_types.SanitizeUserPromptRequest(\n        parent=f\"projects/{project_id}/locations/{location}\",\n        template=f\"projects/{project_id}/locations/{location}/templates/{template_id}\",\n        user_input=user_input,\n    )\n\n    try:\n        response = client.sanitize_user_prompt(request=request)\n        print(\"Sanitized User Prompt Response:\")\n        print(f\"  Sanitized Text: {response.sanitized_user_input.text_content}\")\n        for finding in response.findings:\n            print(f\"  Finding Type: {finding.type_}\")\n            print(f\"    Category: {finding.category}\")\n            print(f\"    Triggered Filters: {finding.triggered_filters}\")\n        if response.blocked:\n            print(\"  Prompt was BLOCKED.\")\n        else:\n            print(\"  Prompt was NOT blocked.\")\n    except Exception as e:\n        print(f\"An error occurred: {e}\")\n\nif __name__ == \"__main__\":\n    quickstart_sanitize_prompt()\n","lang":"python","description":"This quickstart demonstrates how to instantiate the Model Armor client and use it to sanitize a user prompt. It requires a Google Cloud project with the Model Armor API enabled, billing configured, and Application Default Credentials set up (e.g., via `gcloud auth application-default login`). You will also need to create a Model Armor template in your project and specify its ID, along with the project ID and location, as environment variables."},"warnings":[{"fix":"Review release notes and changelogs carefully before upgrading, especially for minor and patch versions. Pin exact version numbers in production environments.","message":"The `google-cloud-modelarmor` library is currently in a preview stage and under active development. This means that any release is subject to backwards-incompatible changes at any time without prior notice, including API surface, behavior, and module structure.","severity":"breaking","affected_versions":"0.1.0 - 0.5.0+"},{"fix":"Ensure your development and deployment environments use Python 3.9 or a newer supported version. Google Cloud client libraries frequently update Python version requirements, so monitor for future changes.","message":"The library explicitly requires Python version 3.9 or higher. Attempting to install or run with older Python versions (e.g., 3.8 or lower) will result in installation failures or runtime errors.","severity":"gotcha","affected_versions":"<0.5.0"},{"fix":"Follow the official Google Cloud authentication guide for Python client libraries. Verify that your environment has valid credentials before running your application.","message":"Authentication is mandatory. Before making API calls, you must enable the Model Armor API in your Google Cloud project, ensure billing is enabled, and set up Application Default Credentials (ADC). For local development, this often involves `gcloud auth application-default login`.","severity":"gotcha","affected_versions":"All"},{"fix":"Always specify the desired Google Cloud region when configuring Model Armor resources and ensure your client is configured to communicate with the correct regional endpoint. In the quickstart, this is handled via `client_options={'api_endpoint': ...}`.","message":"Model Armor resources like templates and floor settings are regional. When instantiating the client or making API calls, you may need to specify the `location` or an `api_endpoint` in the format `{location}-modelarmor.googleapis.com` to target the correct region.","severity":"gotcha","affected_versions":"All"},{"fix":"Implement careful log handling, especially for access restrictions, if logs are stored. Avoid building logic that depends on the exact content or format of log messages from this library.","message":"The library uses standard Python logging, and logs may contain sensitive information. Google may change the occurrence, level, and content of log messages without marking such changes as breaking. Do not rely on the immutability of logging events.","severity":"gotcha","affected_versions":"All"}],"env_vars":null,"last_verified":"2026-04-13T00:00:00.000Z","next_check":"2026-07-12T00:00:00.000Z"}