{"id":496,"library":"google-cloud-language","title":"Google Cloud Natural Language API Client Library","description":"The `google-cloud-language` library provides a Python client for the Google Cloud Natural Language API. It enables developers to perform sentiment analysis, entity analysis, entity sentiment analysis, content classification, and syntax analysis on text. This library is part of the larger `google-cloud-python` monorepo, meaning it receives regular updates and patches as part of Google Cloud's client library ecosystem, with frequent releases for individual services.","status":"active","version":"2.20.0","language":"python","source_language":"en","source_url":"https://github.com/googleapis/google-cloud-python/tree/main/packages/google-cloud-language","tags":["google cloud","natural language processing","nlp","ai","machine learning","sentiment analysis","entity analysis","text classification"],"install":[{"cmd":"pip install google-cloud-language","lang":"bash","label":"Install latest version"}],"dependencies":[{"reason":"Core library for Google API clients, handling common functionality like retries and API versioning.","package":"google-api-core"},{"reason":"Manages authentication with Google Cloud, typically via Application Default Credentials (ADC).","package":"google-auth"},{"reason":"Used for serializing and deserializing API request and response data.","package":"protobuf"}],"imports":[{"note":"Client classes are typically versioned under `language_v1` or `language_v2` for stability and to distinguish API versions.","wrong":"from google.cloud.language import LanguageServiceClient","symbol":"LanguageServiceClient","correct":"from google.cloud import language_v1\nclient = language_v1.LanguageServiceClient()"},{"note":"Enums are now accessed directly via the `language_v1` or `language_v2` module's types, e.g., `language_v1.Document.Type` (note `type_` for the field name to avoid conflict with the Python keyword `type`). The `enums` submodule is deprecated.","wrong":"from google.cloud.language import enums","symbol":"Document.Type","correct":"from google.cloud import language_v1\ndocument = language_v1.types.Document(content=text, type_=language_v1.Document.Type.PLAIN_TEXT)"}],"quickstart":{"code":"import os\nfrom google.cloud import language_v1\n\n# Set up Application Default Credentials if running locally\n# This line is usually not needed when running on Google Cloud services (e.g., Compute Engine, Cloud Run)\n# For local development, ensure GOOGLE_APPLICATION_CREDENTIALS points to a service account key file\n# or run `gcloud auth application-default login`\n# os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = os.environ.get('GOOGLE_APPLICATION_CREDENTIALS', '')\n\ndef analyze_sentiment(text_content):\n    \"\"\"Analyzes the sentiment of the provided text.\"\"\"\n    client = language_v1.LanguageServiceClient()\n\n    # Available document types: PLAIN_TEXT, HTML\n    document = language_v1.types.Document(\n        content=text_content, type_=language_v1.Document.Type.PLAIN_TEXT\n    )\n\n    # Detects the sentiment of the document\n    sentiment = client.analyze_sentiment(request={'document': document}).document_sentiment\n\n    print(f\"Text: {text_content}\")\n    print(f\"Sentiment Score: {sentiment.score}\")\n    print(f\"Sentiment Magnitude: {sentiment.magnitude}\")\n\n# Example usage\nif __name__ == '__main__':\n    analyze_sentiment(\"Hello, world! This is a great day.\")\n    analyze_sentiment(\"I am very unhappy with the service.\")","lang":"python","description":"This quickstart demonstrates how to perform sentiment analysis using the `google-cloud-language` library. It initializes the client and sends a text document for sentiment detection. Ensure your Google Cloud project has the Natural Language API enabled and that you've set up authentication, preferably using Application Default Credentials, for local development or deployed applications."},"warnings":[{"fix":"For new projects or if you need v2 features, explicitly import `language_v2` (e.g., `from google.cloud import language_v2`). Review the v2 API documentation for specific field changes.","message":"The Natural Language API v2 introduces breaking changes, including renaming the `language` field to `language_code` in responses and removing fields like `salience` and `wikipedia_url` for entity analysis. Ensure you use the correct client (`language_v1` or `language_v2`) and adapt your code accordingly.","severity":"breaking","affected_versions":"API v2 (client library versions supporting v2, e.g., from `google.cloud import language_v2`)"},{"fix":"Use Application Default Credentials (ADC). For local development, run `gcloud auth application-default login` in your terminal. For deployed applications on Google Cloud, a service account attached to the resource is the preferred method.","message":"Direct API Key authentication is generally not supported for Google Cloud client libraries. Attempting to use an API key might result in authentication errors.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Update imports to access enums directly via the versioned module, for example, `language_v1.Document.Type.PLAIN_TEXT`.","message":"The direct import `from google.cloud.language import enums` is deprecated. Enums are now nested under the versioned types module (e.g., `language_v1.types.Document.Type`).","severity":"deprecated","affected_versions":"Older versions allowed this; newer versions (2.0.0+) strongly encourage direct access via versioned `types`."},{"fix":"Always use distinct names for your local Python files that do not conflict with installed package names.","message":"Naming your Python script `google.py`, `language.py`, or similar names can lead to import conflicts (shadowing) with the installed `google.cloud.language` library, causing `ImportError`.","severity":"gotcha","affected_versions":"All versions (Python-specific issue)"}],"env_vars":null,"last_verified":"2026-05-12T14:21:02.461Z","next_check":"2026-06-26T00:00:00.000Z","problems":[{"fix":"Ensure the library is installed using `pip install google-cloud-language`. If you previously installed `google-cloud`, uninstall it (`pip uninstall google-cloud`) as it is deprecated and can cause conflicts, then install the specific service library.","cause":"The `google-cloud-language` library or its parent `google-cloud` metapackage (which is deprecated) is not installed in the current Python environment, or there is an environment path issue.","error":"ModuleNotFoundError: No module named 'google.cloud'"},{"fix":"Update your import statement from `from google.cloud.language_v1 import enums` to `from google.cloud.language_v1 import types`, and access enums via `types.Document.Type.PLAIN_TEXT` (or similar for other enum types).","cause":"A recent update to the `google-cloud-language` library moved `enums` definitions into the `types` submodule, causing older import statements to fail.","error":"ImportError: cannot import name 'enums' from 'google.cloud.language_v1'"},{"fix":"In the Google Cloud Console, ensure the Natural Language API is enabled for your project and that the authenticated principal (service account or user) has appropriate roles, such as 'Cloud Natural Language User' or 'Project Editor'. Also, verify billing is enabled for the project.","cause":"The service account or user credentials used to authenticate with the Google Cloud Natural Language API lack the necessary IAM permissions, or the Natural Language API is not enabled for the Google Cloud project.","error":"403 Permission Denied"},{"fix":"Navigate to the 'APIs & Services' -> 'Credentials' section in the Google Cloud Console. Verify the API key, ensure it is active, and confirm that the Natural Language API is enabled for the project associated with the key. If using Application Default Credentials (ADC), ensure your environment is configured correctly via `gcloud auth application-default login`.","cause":"The API key provided for authentication is incorrect, has expired, is restricted incorrectly, or the API is not enabled for the key's associated project.","error":"Invalid API key"}],"ecosystem":"pypi","meta_description":null,"install_score":100,"install_tag":"verified","quickstart_score":0,"quickstart_tag":"stale","pypi_latest":null,"install_checks":{"last_tested":"2026-05-12","tag":"verified","tag_description":"installs cleanly on critical runtimes, fast import, recently tested","results":[{"runtime":"python:3.10-alpine","python_version":"3.10","os_libc":"alpine (musl)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":1.56,"mem_mb":22.4,"disk_size":"68.2M"},{"runtime":"python:3.10-slim","python_version":"3.10","os_libc":"slim (glibc)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":1.02,"mem_mb":19.9,"disk_size":"66M"},{"runtime":"python:3.11-alpine","python_version":"3.11","os_libc":"alpine (musl)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":2.37,"mem_mb":24.3,"disk_size":"72.9M"},{"runtime":"python:3.11-slim","python_version":"3.11","os_libc":"slim (glibc)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":1.51,"mem_mb":22,"disk_size":"71M"},{"runtime":"python:3.12-alpine","python_version":"3.12","os_libc":"alpine (musl)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":2.29,"mem_mb":23.6,"disk_size":"64.3M"},{"runtime":"python:3.12-slim","python_version":"3.12","os_libc":"slim (glibc)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":1.92,"mem_mb":21.4,"disk_size":"62M"},{"runtime":"python:3.13-alpine","python_version":"3.13","os_libc":"alpine (musl)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":2.31,"mem_mb":24.6,"disk_size":"63.9M"},{"runtime":"python:3.13-slim","python_version":"3.13","os_libc":"slim (glibc)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":1.9,"mem_mb":22.3,"disk_size":"62M"},{"runtime":"python:3.9-alpine","python_version":"3.9","os_libc":"alpine (musl)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":1.47,"mem_mb":22.1,"disk_size":"68.3M"},{"runtime":"python:3.9-slim","python_version":"3.9","os_libc":"slim (glibc)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":1.14,"mem_mb":19.9,"disk_size":"66M"}]},"quickstart_checks":{"last_tested":"2026-04-23","tag":"stale","tag_description":"widespread failures or data too old to trust","results":[{"runtime":"python:3.10-alpine","exit_code":1},{"runtime":"python:3.10-slim","exit_code":1},{"runtime":"python:3.11-alpine","exit_code":1},{"runtime":"python:3.11-slim","exit_code":1},{"runtime":"python:3.12-alpine","exit_code":1},{"runtime":"python:3.12-slim","exit_code":1},{"runtime":"python:3.13-alpine","exit_code":1},{"runtime":"python:3.13-slim","exit_code":1},{"runtime":"python:3.9-alpine","exit_code":1},{"runtime":"python:3.9-slim","exit_code":1}]}}