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

调用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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.22 21:49:50