Recommendations AI导入产品至目录报错:Long-running operation had neither response nor error set
问题根因
报错google.api_core.exceptions.GoogleAPICallError: None Unexpected state: Long-running operation had neither response nor error set是异步长耗时操作默认轮询逻辑异常导致的:import_products属于异步长耗时接口,ProductServiceAsyncClient的默认调用逻辑不会主动等待操作完成,也没有正确拉取操作的最终状态,才会返回无响应无错误的异常,你的导入请求实际根本没被后端正常接收处理,所以控制台看不到导入活动记录。
修复方案
1. 核心调整点
- 手动处理长耗时操作的轮询:调用接口后不要直接await返回结果,先拿到长耗时操作对象,再主动轮询操作状态,直到返回成功/错误结果
- 补充Product构造的必填字段
type_,可选值为PRIMARY/VARIANT/COLLECTION,否则就算请求发送成功也会被后端拒绝
2. 修复后代码示例
import asyncio from google.cloud import retail_v2 from google.cloud.retail_v2 import Product, ProductInlineSource, ProductInputConfig, ImportProductsRequest from google.api_core.exceptions import GoogleAPICallError async def import_products(): product_client = retail_v2.ProductServiceAsyncClient() product_obj = Product( id='product-id', categories=["Larry", "Steve", "Eric"], title='title', type_=Product.Type.PRIMARY, # 补充必填的type字段 ) product_items = [product_obj] product_inline_source = ProductInlineSource( products = product_items ) product_input_config = ProductInputConfig( product_inline_source = product_inline_source ) import_products_request = ImportProductsRequest( parent="projects/xxxxxxx/locations/global/catalogs/default_catalog/branches/default_branch", input_config=product_input_config ) # 第一步:发起导入请求,拿到长耗时操作对象 operation = await product_client.import_products( request=import_products_request, timeout=1000 ) # 第二步:手动轮询操作状态,间隔5秒,最多等待300秒(可自行调整) try: result = await operation.result(timeout=300, polling_interval=5) print(f"导入成功,结果:{result}") return result except GoogleAPICallError as e: print(f"导入失败,错误:{e}") raise asyncio.run(import_products())
额外排查点
如果修改后还是报错,按下面的顺序排查:
- 检查当前使用的服务账号是否有
retail.products.create、retail.operations.get权限 - 确认
projects/xxxxxxx/locations/global/catalogs/default_catalog/branches/default_branch路径的资源ID填写正确,没有拼写错误 - 升级google-cloud-retail包到最新版本,执行命令:
pip install --upgrade google-cloud-retail
内容的提问来源于stack exchange,提问作者Rafal
相关产品推荐
相关产品推荐

