You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

SageMaker调用InvokeEndpoint报ModelError:字符串对象无pop属性

问题排查:SageMaker推理服务报错"'str' object has no attribute 'pop'"

报错原因

  • 部署时指定了JSONSerializer(),predictor会自动将传入的data序列化为JSON格式,但你直接传入了字符串类型的base64编码内容。而模型的推理脚本(如inference.py)中,代码逻辑期望接收字典格式的输入,尝试对输入调用pop()方法提取参数,字符串对象没有该方法,因此触发报错。

解决方法

方法1:调整输入格式为字典

将base64字符串包装成字典结构传入,确保和推理脚本的参数提取逻辑匹配:

predictor.predict(data={"image": img_base64})

同时确认推理脚本中是通过字典的key来提取数据(例如input_data.pop("image"))。

方法2:更换序列化器为字符串类型

如果模型确实期望直接接收字符串格式输入,部署时替换序列化器为StringSerializer():

from sagemaker.serializers import StringSerializer
from sagemaker.deserializers import JSONDeserializer

predictor = huggingface_model.deploy(
    initial_instance_count=1,
    instance_type='ml.m5.xlarge',
    serializer=StringSerializer(),
    deserializer=JSONDeserializer(),
)

修改后predictor会直接将字符串作为请求体发送,避免JSON序列化带来的格式不匹配问题。

内容的提问来源于stack exchange,提问作者lazybot

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.08 09:42:36