跳到主要内容

结构化输出

大模型在执行信息抽取或结构化数据生成任务时,经常会返回额外文本(例如 `json 标记),导致下游解析失败。

解决方案​

通过设置 response_format 参数启用结构化输出,强制模型返回标准格式的纯 JSON 字符串。

支持的模型​

GLM

  • GLM-5.1
  • GLM-5
  • GLM-4.7

Minimax

  • MiniMax-M2.5
  • MiniMax-M2.1

使用方式​

response_format:指定响应格式,设置为 {"type": "json_object"} 启用 JSON 模式

结构化输出示例​

在 Python 中使用​

import requests

url = "https://api.jalapeno-cloud.ai/v1/chat/completions"

payload = {
"model": "${MODEL_NAME}",
"messages": [
{
"role": "system",
"content": "Extract company full name, city, contact phone number as JSON. Keys: company_full_name, city, contact_phone. If info missing, set value to null."
},
{
"role": "user",
"content": "Global Cloud Solution Co., Ltd. is based in Singapore, no phone number provided."
}
],
"response_format": {
"type": "json_object"
}
}
headers = {
"Authorization": "Bearer ${API_KEY}",
"Content-Type": "application/json"
}

response = requests.request("POST", url, json=payload, headers=headers)

print(response.text)

在 curl 中使用​

curl --location --request POST 'https://api.jalapeno-cloud.ai/v1/chat/completions' \
--header 'Authorization: Bearer ${API_KEY}' \
--header 'Content-Type: application/json' \
--data-raw '{
"model": "${MODEL_NAME}",
"messages": [
{
"role": "system",
"content": "Extract company full name, city, contact phone number as JSON. Keys: company_full_name, city, contact_phone. If info missing, set value to null."
},
{
"role": "user",
"content": "Global Cloud Solution Co., Ltd. is based in Singapore, no phone number provided."
}
],
"response_format": {
"type": "json_object"
}
}'

响应​

{
"company_full_name": "Global Cloud Solution Co., Ltd.",
"city": "Singapore",
"contact_phone": null
}