使用Cloud Function将NDJSON上传至GCS时遭遇api_request()参数错误:unexpected keyword argument 'extra_api_info'
extra_api_info TypeError in Google Cloud Function for GCS Upload Let's break down what's happening here and how to resolve this issue:
Root Cause
The TypeError: api_request() got an unexpected keyword argument 'extra_api_info' error stems directly from incompatible versions of Google Cloud Python libraries in your Cloud Function environment.
Recent updates to the google-cloud-storage library added the extra_api_info parameter to internal API request calls. However, if your installed version of google-api-core (a critical dependency for Cloud Storage's Python client) is outdated, it won't recognize this new parameter. This version mismatch is why your previously working code started failing—most likely an automatic dependency update pulled in incompatible versions without your explicit action.
Solutions
1. Pin Dependencies to Compatible Versions
The most reliable fix is to explicitly define compatible library versions in your Cloud Function's requirements.txt file. Here's a tested, compatible set that avoids the parameter mismatch:
google-cloud-storage==2.10.0 google-api-core==2.11.1 pandas==2.0.3
Adjust the pandas version to match your project's needs, but keep the Google Cloud library versions pinned to this working combination.
2. Upgrade All Related Dependencies
If you prefer using newer library versions, you can sync all relevant dependencies to their latest compatible releases. Run this command locally (and ensure the updated versions are reflected in requirements.txt):
pip install --upgrade google-cloud-storage google-api-core
This will align the library versions so that the api_request method accepts the extra_api_info parameter.
3. Clean Up Unused Code
While not related to the error, your code includes an unused import that can be removed for clarity:
from google.cloud.storage import blob
You're already creating a blob instance via bucket.blob(), so this line serves no purpose and just adds unnecessary clutter.
Verification
After applying one of the fixes above, redeploy your Cloud Function and test the NDJSON upload again. The string generated from your pandas DataFrame should be successfully written to your GCS bucket without the TypeError.
内容的提问来源于stack exchange,提问作者hawkaterrier

