调用Sagemaker Endpoint出现“Failed to deserialize Error”问题求助
问题分析
错误提示Failed to deserialize the JSON body into the target type: invalid type: string "{"name": "john mclain"}", expected struct CompatGenerateRequest,核心问题是请求Body格式错误——你把JSON字符串重复做了序列化,导致模型收到的是被字符串包裹的JSON内容,而非它期望的JSON结构体。
修复方案
直接将字典转为JSON字符串作为请求Body即可,去掉冗余的序列化操作:
import json import boto3 sagemaker_runtime = boto3.client("sagemaker-runtime", region_name='ap-south-1') endpoint_name = "jumpstart-dft-hf-llm-falcon-40b" inference="huggingface-llm-falcon-40b-instruct" payload={"name": "john mclain"} response = sagemaker_runtime.invoke_endpoint( EndpointName=endpoint_name, ContentType="application/json", Body=json.dumps(payload), InferenceComponentName='huggingface-llm-falcon-40b' )
补充说明
- 原代码里
jsonserializer.serialize(json.dumps(jsonstr))是多余操作:json.dumps()已经把字典转成了JSON字符串,再用序列化工具处理会把这个字符串本身当成对象序列化,最终Body内容会是类似"\"{\\\"name\\\": \\\"john mclain\\\"}\""的格式,模型自然无法解析。 - 另外注意,Falcon 40B Instruct模型通常需要标准的请求结构,比如包含
inputs字段的请求体才能正常生成内容,示例如下:payload = { "inputs": "Hello, my name is john mclain. Can you introduce yourself?", "parameters": {"max_new_tokens": 150, "temperature": 0.7} }
内容的提问来源于stack exchange,提问作者Rrrrr
相关产品推荐
相关产品推荐

