Google Cloud Natural Language API Client Library
raw JSON → 2.20.0 verified Tue May 12 auth: no python install: verified quickstart: stale
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.
pip install google-cloud-language Common errors
error ModuleNotFoundError: No module named 'google.cloud' ↓
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.
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. error ImportError: cannot import name 'enums' from 'google.cloud.language_v1' ↓
cause A recent update to the `google-cloud-language` library moved `enums` definitions into the `types` submodule, causing older import statements to fail.
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). error 403 Permission Denied ↓
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.
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.
error Invalid API key ↓
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.
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. Warnings
breaking 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. ↓
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.
gotcha Direct API Key authentication is generally not supported for Google Cloud client libraries. Attempting to use an API key might result in authentication errors. ↓
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.
deprecated 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`). ↓
fix Update imports to access enums directly via the versioned module, for example, `language_v1.Document.Type.PLAIN_TEXT`.
gotcha 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`. ↓
fix Always use distinct names for your local Python files that do not conflict with installed package names.
Install compatibility verified last tested: 2026-05-12
python os / libc status wheel install import disk
3.10 alpine (musl) - - 1.56s 68.2M
3.10 slim (glibc) - - 1.02s 66M
3.11 alpine (musl) - - 2.37s 72.9M
3.11 slim (glibc) - - 1.51s 71M
3.12 alpine (musl) - - 2.29s 64.3M
3.12 slim (glibc) - - 1.92s 62M
3.13 alpine (musl) - - 2.31s 63.9M
3.13 slim (glibc) - - 1.90s 62M
3.9 alpine (musl) - - 1.47s 68.3M
3.9 slim (glibc) - - 1.14s 66M
Imports
- LanguageServiceClient wrong
from google.cloud.language import LanguageServiceClientcorrectfrom google.cloud import language_v1 client = language_v1.LanguageServiceClient() - Document.Type wrong
from google.cloud.language import enumscorrectfrom google.cloud import language_v1 document = language_v1.types.Document(content=text, type_=language_v1.Document.Type.PLAIN_TEXT)
Quickstart stale last tested: 2026-04-23
import os
from google.cloud import language_v1
# Set up Application Default Credentials if running locally
# This line is usually not needed when running on Google Cloud services (e.g., Compute Engine, Cloud Run)
# For local development, ensure GOOGLE_APPLICATION_CREDENTIALS points to a service account key file
# or run `gcloud auth application-default login`
# os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = os.environ.get('GOOGLE_APPLICATION_CREDENTIALS', '')
def analyze_sentiment(text_content):
"""Analyzes the sentiment of the provided text."""
client = language_v1.LanguageServiceClient()
# Available document types: PLAIN_TEXT, HTML
document = language_v1.types.Document(
content=text_content, type_=language_v1.Document.Type.PLAIN_TEXT
)
# Detects the sentiment of the document
sentiment = client.analyze_sentiment(request={'document': document}).document_sentiment
print(f"Text: {text_content}")
print(f"Sentiment Score: {sentiment.score}")
print(f"Sentiment Magnitude: {sentiment.magnitude}")
# Example usage
if __name__ == '__main__':
analyze_sentiment("Hello, world! This is a great day.")
analyze_sentiment("I am very unhappy with the service.")