DeepSeek API response_format: json_object 避坑指南

发布时间:2026/8/14 23:03:22
DeepSeek API response_format: json_object 避坑指南 把 AI 输出接进业务系统最怕的就是格式不固定。DeepSeek 提供了response_format: json_object强制输出 JSON但真正落地时坑不少。这篇把我踩过的坑一次说清。一、先跑通JSON 模式怎么开curl https://api.deepseek.com/chat/completions \ -H Content-Type: application/json \ -H Authorization: Bearer $DEEPSEEK_API_KEY \ -d { model: deepseek-chat, messages: [ {role: system, content: 你是一个输出 JSON 的助手}, {role: user, content: 分析这句话的情感输出 json} ], response_format: {type: json_object} }返回结果里的choices[0].message.content就是一段 JSON 字符串。看着简单下面每个坑都藏在这里。二、坑1prompt 里没有 json 这个词直接翻车这是 DeepSeek 官方文档特意强调、也最容易踩的坑。现象明明设置了response_format: {type: json_object}模型却返回空内容或者返回一段不带 JSON 的纯文本。原因DeepSeek 要求消息里必须出现 json 这个单词不区分大小写否则即使你声明了 response_format模型也可能不认。正例{role: system, content: 你是一个输出 JSON 格式的助手} {role: user, content: 把下面这段文字转成 json 输出}反例会翻车{role: system, content: 你是一个结构化输出助手} {role: user, content: 分析这句话的情感}一句话记住别用结构化代替jsonprompt 里老老实实写 json。三、坑2max_tokens 太小JSON 被腰斩现象返回的 JSON 明显不完整结尾是{name: 张三, tags: [Java,就没了解析必抛异常。原因JSON 模式默认输出比纯文本长字段名、引号、逗号、花括号都占 token。之前按纯文本习惯设的 max_tokens 不够。解决给足预算。单次结构化输出建议max_tokens至少 1024字段多、内容长直接给 2048 或 4096。四、坑3模型爱给 JSON 套 markdown 代码块现象content拿回来长这样json {name: 张三, age: 30} 直接JSON.parse会报错。解决解析前先清洗去掉 包裹和前后空白private String cleanJson(String content) { String s content.trim(); if (s.startsWith()) { s s.replaceFirst([a-zA-Z]*\\s*, ); s s.replaceFirst(\\s*$, ); } return s.trim(); }五、坑4字段类型漂移数字变字符串现象同一个字段这次返回count: 3下次返回count: 3。字段缺失也常见这次有tags下次没有。原因LLM 不保证类型稳定尤其是没给示例时。解决两条路prompt 里给一个完整的输出示例模型会照着抄输出示例{sentiment: 正面, score: 0.9, tags: [服务, 价格]}拿到结果后做类型归一读值时对类型做兜底处理。实战建议示例优先兜底其次。示例能解决 90% 的类型漂移。六、坑5字符串值里夹了未转义的换行和引号现象让模型总结一段文本放进 JSON 字段结果文本里的换行、双引号没转义产出非法 JSON{summary: 他说服务很好。 体验不错。}这个 JSON 直接解析必挂。原因模型输出的是看起来像 JSON的文本不是真正经过序列化的 JSON特殊字符转义不可靠。解决prompt 里明确要求字符串内的换行请用 \n 表示双引号请转义解析失败时重试一次把报错信息回喂给模型让它修正这是最有效的兜底七、Java 侧稳健封装可直接抄import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import lombok.extern.slf4j.Slf4j; import org.springframework.http.*; import org.springframework.stereotype.Service; import org.springframework.web.client.RestTemplate; ​ import java.util.HashMap; import java.util.List; import java.util.Map; ​ Service Slf4j public class DeepSeekJsonService { ​ private static final String API_URL https://api.deepseek.com/chat/completions; private static final String API_KEY sk-xxxxxxxx; ​ private final RestTemplate restTemplate new RestTemplate(); ​ /** * 调用 DeepSeek 强制输出 JSON返回清洗后的合法 JSON 字符串 */ public String chatForJson(String userPrompt) { MapString, Object body new HashMap(); body.put(model, deepseek-chat); body.put(max_tokens, 2048); body.put(response_format, Map.of(type, json_object)); body.put(messages, List.of( Map.of(role, system, content, 你是 JSON 输出助手只输出合法 JSON不要 markdown 代码块), Map.of(role, user, content, userPrompt 请用 json 格式输出) )); ​ HttpHeaders headers new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_JSON); headers.set(Authorization, Bearer API_KEY); ​ String resp restTemplate.postForObject( API_URL, new HttpEntity(body, headers), String.class); ​ JSONObject json JSON.parseObject(resp); String content json.getJSONArray(choices) .getJSONObject(0) .getJSONObject(message) .getString(content); ​ return cleanJson(content); } ​ private String cleanJson(String content) { String s content.trim(); if (s.startsWith()) { s s.replaceFirst([a-zA-Z]*\\s*, ); s s.replaceFirst(\\s*$, ); } return s.trim(); } }要点都封装进去了system user 两个消息都带了 json 字样max_tokens给到 2048 防截断cleanJson统一去 markdown 包裹八、总结坑一句话解法prompt 没有 json消息里老老实实写 jsonJSON 被截断max_tokens 给足 2048markdown 代码块包裹解析前 cleanJson 清洗字段类型漂移prompt 给输出示例特殊字符没转义明确转义要求 失败重试结构化输出不是开了response_format就万事大吉真正稳的是一套提示词约束 清洗 兜底重试的组合拳。关于作者独立开发者主业 Java 后端。一个人用 SpringBoot AI 交付过企业级管理平台和微信小程序业余接外包。代码和架构图放 Gitee 了https://gitee.com/yao113088/jiguang-dev微信/邮箱luckluffy顺手推荐小程序面试刷题狮是我用 SpringBoot DeepSeek 一个人做的 AI 面试刷题工具本文的response_format: json_object就是它的核心实现。微信搜索面试刷题狮就能搜到免费刷题 AI 定制面试。