AutoML预测上传至Google Cloud Functions后报错的问题咨询
I’ve run into this exact issue before—version mismatches between your local google-cloud-automl library and the one installed by default in Cloud Functions are almost always the culprit here. Let’s break down what’s happening and how to fix it:
Why This Error Happens
Your local code works because you’re using a version of the AutoML client where predict() accepts multiple positional arguments (name, payload, params). But when deployed to Cloud Functions, the default installed library version uses a different method signature—likely expecting only a single PredictRequest object instead of separate arguments. This mismatch causes the "takes 1 to 2 positional arguments but X were given" error.
Step 1: Align Library Versions
First, make sure Cloud Functions uses the exact same version of google-cloud-automl that you tested locally.
- Run
pip show google-cloud-automlon your local machine to get your version (e.g.,2.10.0). - Create or update a
requirements.txtfile in your Cloud Functions deployment folder with this explicit version:google-cloud-automl==2.10.0
This ensures Cloud Functions installs the same library you used locally, eliminating signature mismatches.
Step 2: Use the Correct Predict Call for Your Version
Depending on your library version, adjust the predict() call to match the expected signature:
For Newer Versions (v1.0+)
The predict() method expects a single PredictRequest object. Update your code like this:
from google.cloud import automl_v1beta1 from google.cloud.automl_v1beta1.types import PredictRequest def get_sentiment(content): """ Returns a google cloud platform payload class containing the sentiment score given by our NLP sentiment analyser. :param content: STRING (UTF-8 encoded, ASCII) :return: <class 'google.cloud.automl.types.PredictResponse'> """ options = automl_v1beta1.ClientOptions(api_endpoint='automl.googleapis.com') prediction_client = automl_v1beta1.PredictionServiceClient(client_options=options) name = model_sentiment # Ensure this is set to your full model resource name (projects/.../models/...) # Build the payload payload = {'text_snippet': {'content': content, 'mime_type': 'text/plain'}} # Create the request object request = PredictRequest( name=name, payload=payload, params={} ) # Call predict with the request object response = prediction_client.predict(request) return response
For Older Versions (Pre-v1.0)
If you’re stuck on an older library version, use keyword arguments to avoid positional mismatches:
# Replace your existing predict call with this request = prediction_client.predict( name=name, payload=payload, params=params )
Note: Using keyword arguments makes the call more robust to signature changes, but aligning versions is still the better long-term fix.
Step 3: Verify Model Resource Name
Double-check that model_sentiment is set to the full, valid resource name of your AutoML model, formatted like:projects/YOUR_PROJECT_ID/locations/YOUR_LOCATION/models/YOUR_MODEL_ID
If this variable is hardcoded locally but not properly configured in Cloud Functions (e.g., missing environment variables), that could cause additional issues.
Final Checks
- Test your code locally with the exact version specified in
requirements.txtto ensure it works before deploying. - Confirm Cloud Functions has the necessary IAM permissions to access your AutoML model (the service account used by Cloud Functions should have the
AutoML Predictorrole).
内容的提问来源于stack exchange,提问作者Alfred Lindholm

