使用aiplatform_v1异步预测时遇实例不可迭代错误求助
问题:传入Dict到PredictRequest时遇到TypeError:instances不可迭代
尝试将Python Dict传入aiplatform_v1.PredictRequest,通过aiplatform_v1.PredictionServiceAsyncClient执行预测操作时,出现错误:
TypeError: Argument for field google.cloud.aiplatform.v1.PredictRequest.instances is not iterable
已参考官方示例将Python Dict解析为google.protobuf.Value并包装成列表,但仍报错。以下是相关代码:
from google.protobuf.struct_pb2 import Value, Struct, ListValue from google.protobuf import json_format from google.oauth2 import service_account from google.api_core import exceptions from google.cloud import aiplatform_v1 async def model_predict(json_data): # Load credentials from a service account key file key_path = 'path-to-key-file.json' credentials = service_account.Credentials.from_service_account_file( key_path, scopes=['https://www.googleapis.com/auth/cloud-platform'] ) # Set up the endpoint configs and pass user input project_id = "XXXXXX" endpoint_id = "XXXXXXX" instance_dict = json_data location = "us-central1" api_endpoint = "us-central1-aiplatform.googleapis.com" # Format user input to pass into request instance = json_format.ParseDict(instance_dict, Value()) parameters_dict = {} parameters= json_format.ParseDict(parameters_dict, Value()) # Set up async prediction service client client_options = {"api_endpoint": api_endpoint} prediction_client = aiplatform_v1.PredictionServiceAsyncClient( credentials=credentials, client_options=client_options ) # Build Prediction request endpoint_path = prediction_client.endpoint_path( project=project_id, location=location, endpoint=endpoint_id, ) request = aiplatform_v1.PredictRequest( endpoint=endpoint_path, instances= [instance], parameters = parameters ) # Call the predict method asynchronously try: response = await prediction_client.predict(request=request) print(response) predictions = response.predictions return predictions, None except exceptions.GoogleAPIError as error: error_message = f"Prediction request failed: {error}" return None, error_message
打印的传入实例类型及样本:
<class 'google.protobuf.struct_pb2.Value'> struct_value { fields { key: "Ticket_Subject" value { string_value: "Battery Issue" } } fields { key: "Ticket_Status" value { string_value: "Open" } } fields { key: "Ticket_Priority" value { string_value: "High" } } .... (more fields structured as above) }
解决方法
方案1:直接使用Python原生类型(推荐)
Google Cloud AI Platform的客户端库支持直接传入Python原生字典/列表,会自动完成与protobuf结构的转换,避免手动转换可能引发的类型问题。修改代码如下:
- 替换实例和参数的构造代码:
# 直接用原始字典构造实例列表,无需手动转protobuf instances = [json_data] # 空参数直接传空字典 parameters = {}
- 构建请求时传入原生类型:
request = aiplatform_v1.PredictRequest( endpoint=endpoint_path, instances=instances, parameters=parameters )
方案2:手动构造正确的protobuf结构(若必须手动转换)
如果需要手动处理protobuf结构,确保实例列表是正确的可迭代protobuf对象:
from google.protobuf.struct_pb2 import Struct, ListValue # 构造包含实例的ListValue instances_list = ListValue() # 将字典解析为Struct并添加到列表中 instance_struct = json_format.ParseDict(json_data, Struct()) instances_list.values.add(struct_value=instance_struct) # 构建请求时传入ListValue request = aiplatform_v1.PredictRequest( endpoint=endpoint_path, instances=instances_list, parameters=parameters )
验证
两种方案都能解决instances不可迭代的错误,其中方案1更简洁,且减少了手动转换的潜在问题。
内容的提问来源于stack exchange,提问作者jaspercoleman
相关产品推荐
相关产品推荐

