JSON Mode

在信息抽取场景,您可以通过提示词来指引大模型生成标准格式的 JSON 字符串。但大模型输出内容具有不确定性,返回的内容可能不符合 JSON 格式(比如输出的内容多了```json```,或“以下为JSON字符串”等内容),这会影响到后续的解析步骤。开启结构化输出功能可以确保大模型输出标准格式的 JSON 字符串。

支持的模型

结构化输出功能支持以下模型:

  • qwen-max 系列

    qwen-max-2024-09-19及之后的模型

  • qwen-plus 系列(非思考模式)

    qwen-plus-2024-09-19及之后的模型

  • qwen-flash 系列(非思考模式)

    qwen-flash-2025-07-28及之后的模型

  • qwen-turbo 系列(非思考模式)

    qwen-turbo-2024-09-19及之后的模型

  • qwen-开源系列

    qwen3(非思考模式)、qwen2.5系列的文本模型(不含mathcoder模型)

  • qwen-vl系列

    仅 qwen-vl-max、qwen-vl-plus 模型支持(不含快照版及最新版模型)

开始使用

前提条件

您需要已获取API Key配置API Key到环境变量。如果通过 OpenAI SDK 或 DashScope SDK 进行调用,需要安装最新版 SDK

使用方法

您的请求需要满足两个条件:

  • 设置参数

    您需要设置请求参数response_format{"type": "json_object"}

  • 提示词指引

    您需要在提示词中指引模型输出 JSON 字符串,否则会报错:'messages' must contain the word 'json' in some form, to use 'response_format' of type 'json_object'.

文生文模型

建议您在提示词中说明每个属性的数据类型,并提供样例给大模型参考。

OpenAI兼容

Python

# 步骤 1:发出请求

from openai import OpenAI
import os
import json

# 预定义示例响应(用于few-shot提示)
example1_response = json.dumps(
    {
        "info": {"name": "张三", "age": "25岁", "email": "zhangsan@example.com"},
        "hobby": ["唱歌"]
    },
    ensure_ascii=False
)
example2_response = json.dumps(
    {
        "info": {"name": "李四", "age": "30岁", "email": "lisi@example.com"},
        "hobby": ["跳舞", "游泳"]
    },
    ensure_ascii=False
)
example3_response = json.dumps(
    {
        "info": {"name": "王五", "age": "40岁", "email": "wangwu@example.com"},
        "hobby": ["Rap", "篮球"]
    },
    ensure_ascii=False
)

client = OpenAI(
    api_key=os.getenv("DASHSCOPE_API_KEY"),
    base_url="https://dashscope.aliyuncs.com/compatible-mode/v1",
)

completion = client.chat.completions.create(
    model="qwen-plus",
    messages=[
        {
            "role": "system",
            "content": f"""提取name、age、email和hobby(数组类型),输出包含info层和hobby数组的JSON。
            示例:
            Q:我叫张三,今年25岁,邮箱是zhangsan@example.com,爱好是唱歌
            A:{example1_response}
            
            Q:我叫李四,今年30岁,邮箱是lisi@example.com,平时喜欢跳舞和游泳
            A:{example2_response}
            
            Q:我的邮箱是wangwu@example.com,今年40岁,名字是王五,会Rap和打篮球
            A:{example3_response}"""
        },
        {
            "role": "user",
            "content": "大家好,我叫刘五,今年34岁,邮箱是liuwu@example.com,平时喜欢打篮球和旅游", 
        },
    ],
    response_format={"type": "json_object"},
)

json_string = completion.choices[0].message.content
print(json_string)

返回结果

{
  "info": {
    "name": "刘五",
    "age": "34岁",
    "email": "liuwu@example.com"
  },
  "hobby": ["打篮球", "旅游"]
}

Node.js

// 步骤 1:发出请求

import OpenAI from "openai";

// 预定义示例响应
const example1Response = JSON.stringify({
    info: { name: "张三", age: "25岁", email: "zhangsan@example.com" },
    hobby: ["唱歌"]
});
const example2Response = JSON.stringify({
    info: { name: "李四", age: "30岁", email: "lisi@example.com" },
    hobby: ["跳舞", "游泳"]
});
const example3Response = JSON.stringify({
    info: { name: "王五", age: "40岁", email: "wangwu@example.com" },
    hobby: ["Rap", "篮球"]
});

const openai = new OpenAI({
    // 若没有配置环境变量,请用阿里云百炼API Key将下行替换为:apiKey: "sk-xxx",
    apiKey: process.env.DASHSCOPE_API_KEY,
    baseURL: "https://dashscope.aliyuncs.com/compatible-mode/v1"
});

const completion = await openai.chat.completions.create({
    model: "qwen-plus",
    messages: [
        {
            role: "system",
            content: `提取name、age、email和hobby(数组类型),输出包含info层和hobby数组的JSON。
            示例:
            Q:我叫张三,今年25岁,邮箱是zhangsan@example.com,爱好是唱歌
            A:${example1Response}
            
            Q:我叫李四,今年30岁,邮箱是lisi@example.com,平时喜欢跳舞和游泳
            A:${example2Response}
            
            Q:我的邮箱是wangwu@example.com,今年40岁,名字是王五,会Rap和打篮球
            A:${example3Response}`
        },
        {
            role: "user",
            content: "大家好,我叫刘五,今年34岁,邮箱是liuwu@example.com,平时喜欢打篮球和旅游"
        }
    ],
    response_format: {
        type: "json_object"
    }
});

const jsonString = completion.choices[0].message.content
console.log(jsonString);

返回结果

{
  "info": {
    "name": "刘五",
    "age": "34岁",
    "email": "liuwu@example.com"
  },
  "hobby": ["打篮球", "旅游"]
}

curl

curl -X POST https://dashscope.aliyuncs.com/compatible-mode/v1/chat/completions \
-H "Authorization: Bearer $DASHSCOPE_API_KEY" \
-H "Content-Type: application/json" \
-d '{
    "model": "qwen-plus",
    "messages": [
        {
            "role": "system",
            "content": "你需要提取出name(名字,为string类型)、age(年龄,为string类型)与email(邮箱,为string类型),请输出JSON 字符串,不要输出其它无关内容。\n示例:\nQ:我叫张三,今年25岁,邮箱是zhangsan@example.com\nA:{\"name\":\"张三\",\"age\":\"25岁\",\"email\":\"zhangsan@example.com\"}\nQ:我叫李四,今年30岁,我的邮箱是lisi@example.com\nA:{\"name\":\"李四\",\"age\":\"30岁\",\"email\":\"lisi@example.com\"}\nQ:我叫王五,我的邮箱是wangwu@example.com,今年40岁\nA:{\"name\":\"王五\",\"age\":\"40岁\",\"email\":\"wangwu@example.com\""
        },
        {
            "role": "user", 
            "content": "大家好,我叫刘五,今年34岁,邮箱是liuwu@example.com"
        }
    ],
    "response_format": {
        "type": "json_object"
    }
}'

返回结果

{
    "choices": [
        {
            "message": {
                "role": "assistant",
                "content": "{\n  \"name\": \"张三\",\n  \"age\": 25,\n  \"email\": \"zhangsan@example.com\"\n}"
            },
            "finish_reason": "stop",
            "index": 0,
            "logprobs": null
        }
    ],
    "object": "chat.completion",
    "usage": {
        "prompt_tokens": 65,
        "completion_tokens": 29,
        "total_tokens": 94,
        "prompt_tokens_details": {
            "cached_tokens": 0
        }
    },
    "created": 1736771145,
    "system_fingerprint": null,
    "model": "qwen-plus",
    "id": "chatcmpl-59b28c8b-6cb7-9e4d-9a78-3cbed664d3c0"
}

DashScope

Python

# 步骤 1:发出请求

import os
import json
import dashscope

# 预定义示例响应(用于few-shot提示)
example1_response = json.dumps(
    {
        "info": {"name": "张三", "age": "25岁", "email": "zhangsan@example.com"},
        "hobby": ["唱歌"]
    },
    ensure_ascii=False
)
example2_response = json.dumps(
    {
        "info": {"name": "李四", "age": "30岁", "email": "lisi@example.com"},
        "hobby": ["跳舞", "游泳"]
    },
    ensure_ascii=False
)
example3_response = json.dumps(
    {
        "info": {"name": "王五", "age": "40岁", "email": "wangwu@example.com"},
        "hobby": ["Rap", "篮球"]
    },
    ensure_ascii=False
)

messages=[
        {
            "role": "system",
            "content": f"""提取name、age、email和hobby(数组类型),输出包含info层和hobby数组的JSON。
            示例:
            Q:我叫张三,今年25岁,邮箱是zhangsan@example.com,爱好是唱歌
            A:{example1_response}
            
            Q:我叫李四,今年30岁,邮箱是lisi@example.com,平时喜欢跳舞和游泳
            A:{example2_response}
            
            Q:我的邮箱是wangwu@example.com,今年40岁,名字是王五,会Rap和打篮球
            A:{example3_response}"""
        },
        {
            "role": "user",
            "content": "大家好,我叫刘五,今年34岁,邮箱是liuwu@example.com,平时喜欢打篮球和旅游", 
        },
    ]
response = dashscope.Generation.call(
    # 若没有配置环境变量,请用阿里云百炼API Key将下行替换为:api_key="sk-xxx",
    api_key=os.getenv('DASHSCOPE_API_KEY'),
    model="qwen-plus", 
    messages=messages,
    result_format='message',
    response_format={'type': 'json_object'}
    )
    
json_string = response.output.choices[0].message.content
print(json_string)

返回结果

{
  "info": {
    "name": "刘五",
    "age": "34岁",
    "email": "liuwu@example.com"
  },
  "hobby": ["打篮球", "旅游"]
}

Java

// DashScope Java SDK 版本需要不低于 2.18.4

import java.util.Arrays;
import java.lang.System;
import com.alibaba.dashscope.aigc.generation.Generation;
import com.alibaba.dashscope.aigc.generation.GenerationParam;
import com.alibaba.dashscope.aigc.generation.GenerationResult;
import com.alibaba.dashscope.common.Message;
import com.alibaba.dashscope.common.Role;
import com.alibaba.dashscope.exception.ApiException;
import com.alibaba.dashscope.exception.InputRequiredException;
import com.alibaba.dashscope.exception.NoApiKeyException;
import com.alibaba.dashscope.utils.JsonUtils;
import com.alibaba.dashscope.common.ResponseFormat;
public class Main {
    public static GenerationResult callWithMessage() throws ApiException, NoApiKeyException, InputRequiredException {
        Generation gen = new Generation();
        Message systemMsg = Message.builder()
                .role(Role.SYSTEM.getValue())
                .content("""
                你需要提取出name(名字,为string类型)、age(年龄,为string类型)与email(邮箱,为string类型),请输出JSON 字符串,不要输出其它无关内容。
                示例:
                Q:我叫张三,今年25岁,邮箱是zhangsan@example.com
                A:{"name":"张三","age":"25岁","email":"zhangsan@example.com"}
                Q:我叫李四,今年30岁,我的邮箱是lisi@example.com
                A:{"name":"李四","age":"30岁","email":"lisi@example.com"}
                Q:我叫王五,我的邮箱是wangwu@example.com,今年40岁
                A:{"name":"王五","age":"40岁","email":"wangwu@example.com"}""")
                .build();
        Message userMsg = Message.builder()
                .role(Role.USER.getValue())
                .content("大家好,我叫刘五,今年34岁,邮箱是liuwu@example.com")
                .build();
        ResponseFormat jsonMode = ResponseFormat.builder().type("json_object").build();
        GenerationParam param = GenerationParam.builder()
                // 若没有配置环境变量,请用阿里云百炼API Key将下行替换为:.apiKey("sk-xxx")
                .apiKey(System.getenv("DASHSCOPE_API_KEY"))
                // 此处以qwen-plus为例,可按需更换模型名称。模型列表:https://help.aliyun.com/zh/model-studio/getting-started/models
                .model("qwen-plus")
                .messages(Arrays.asList(systemMsg, userMsg))
                .resultFormat(GenerationParam.ResultFormat.MESSAGE)
                .responseFormat(jsonMode)
                .build();
        return gen.call(param);
    }
    public static void main(String[] args) {
        try {
            GenerationResult result = callWithMessage();
            System.out.println(JsonUtils.toJson(result));
        } catch (ApiException | NoApiKeyException | InputRequiredException e) {
            // 使用日志框架记录异常信息
            System.err.println("An error occurred while calling the generation service: " + e.getMessage());
        }
        System.exit(0);
    }
}

curl

示例代码

curl -X POST https://dashscope.aliyuncs.com/api/v1/services/aigc/text-generation/generation \
-H "Authorization: Bearer $DASHSCOPE_API_KEY" \
-H "Content-Type: application/json" \
-d '{
    "model": "qwen-plus",
    "input": {
        "messages": [
            {
                "role": "system",
                "content": "你需要提取出name(名字,为string类型)、age(年龄,为string类型)与email(邮箱,为string类型),请输出JSON 字符串,不要输出其它无关内容。\n示例:\nQ:我叫张三,今年25岁,邮箱是zhangsan@example.com\nA:{\"name\":\"张三\",\"age\":\"25岁\",\"email\":\"zhangsan@example.com\"}\nQ:我叫李四,今年30岁,我的邮箱是lisi@example.com\nA:{\"name\":\"李四\",\"age\":\"30岁\",\"email\":\"lisi@example.com\"}\nQ:我叫王五,我的邮箱是wangwu@example.com,今年40岁\nA:{\"name\":\"王五\",\"age\":\"40岁\",\"email\":\"wangwu@example.com\""
            },
            {
                "role": "user", 
                "content": "大家好,我叫刘五,今年34岁,邮箱是liuwu@example.com"
            }
        ]
    },
    "parameters": {
        "result_format": "message",
        "response_format": {
            "type": "json_object"
        }
    }
}'

返回结果

{
    "output": {
        "choices": [
            {
                "finish_reason": "stop",
                "message": {
                    "role": "assistant",
                    "content": "{\"name\":\"刘五\",\"age\":\"34岁\",\"email\":\"liuwu@example.com\"}"
                }
            }
        ]
    },
    "usage": {
        "prompt_tokens_details": {
            "cached_tokens": 0
        },
        "total_tokens": 223,
        "output_tokens": 20,
        "input_tokens": 203
    },
    "request_id": "0c353885-ffdc-9b99-a273-ade7b14f3fab"
}

视觉理解模型

OpenAI兼容

Python

import os
from openai import OpenAI

client = OpenAI(
    api_key=os.getenv("DASHSCOPE_API_KEY"),
    base_url="https://dashscope.aliyuncs.com/compatible-mode/v1",
)

completion = client.chat.completions.create(
    model="qwen-vl-max",
    messages=[
        {
            "role": "system",
            "content": [{"type": "text", "text": "You are a helpful assistant."}],
        },
        {
            "role": "user",
            "content": [
                {
                    "type": "image_url",
                    "image_url": {
                        "url": "http://duguang-labelling.oss-cn-shanghai.aliyuncs.com/demo_ocr/receipt_zh_demo.jpg"
                    },
                },
                {"type": "text", "text": "提取图中ticket(包括 travel_date、trains、seat_num、arrival_site、price)和 invoice 的信息(包括 invoice_code 和 invoice_number ),请输出包含 ticket 和 invoice 数组的JSON"},
            ],
        },
    ],
    response_format={"type": "json_object"}
)
json_string = completion.choices[0].message.content
print(json_string)

返回结果

{
  "ticket": [
    {
      "travel_date": "2013-06-29",
      "trains": "流水",
      "seat_num": "371",
      "arrival_site": "开发区",
      "price": "8.00"
    }
  ],
  "invoice": [
    {
      "invoice_code": "221021325353",
      "invoice_number": "10283819"
    }
  ]
}

Node.js

import OpenAI from "openai";

const openai = new OpenAI({
  // 若没有配置环境变量,请用百炼API Key将下行替换为:apiKey: "sk-xxx"
  apiKey: process.env.DASHSCOPE_API_KEY,
  baseURL: "https://dashscope.aliyuncs.com/compatible-mode/v1"
});

async function main() {
  const response = await openai.chat.completions.create({
    model: "qwen-vl-max",
    messages: [{
        role: "system",
        content: [{
          type: "text",
          text: "You are a helpful assistant."
        }]
      },
      {
        role: "user",
        content: [{
            type: "image_url",
            image_url: {
              "url": "http://duguang-labelling.oss-cn-shanghai.aliyuncs.com/demo_ocr/receipt_zh_demo.jpg"
            }
          },
          {
            type: "text",
            text: "提取图中ticket(包括 travel_date、trains、seat_num、arrival_site、price)和 invoice 的信息(数组类型,包括 invoice_code 和 invoice_number ),请输出包含 ticket 和 invoice 数组的JSON"
          }
        ]
      }
    ],
    response_format: {type: "json_object"}
  });
  const jsonString = completion.choices[0].message.content
  console.log(response.choices[0].message.content);
}

main()

返回结果

{
  "ticket": [
    {
      "travel_date": "2013-06-29",
      "trains": "流水",
      "seat_num": "371",
      "arrival_site": "开发区",
      "price": "8.00"
    }
  ],
  "invoice": [
    {
      "invoice_code": "221021325353",
      "invoice_number": "10283819"
    }
  ]
}

curl

curl --location 'https://dashscope.aliyuncs.com/compatible-mode/v1/chat/completions' \
--header "Authorization: Bearer $DASHSCOPE_API_KEY" \
--header 'Content-Type: application/json' \
--data '{
  "model": "qwen-vl-max",
  "messages": [
  {"role":"system",
  "content":[
    {"type": "text", "text": "You are a helpful assistant."}]},
  {
    "role": "user",
    "content": [
      {"type": "image_url", "image_url": {"url": "http://duguang-labelling.oss-cn-shanghai.aliyuncs.com/demo_ocr/receipt_zh_demo.jpg"}},
      {"type": "text", "text": "提取图中ticket(包括 travel_date、trains、seat_num、arrival_site、price)和 invoice 的信息(数组类型,包括 invoice_code 和 invoice_number ),请输出包含 ticket 和 invoice 数组的JSON"}
    ]
  }],
  "response_format":{"type": "json_object"}
}'

返回结果

{
  "choices": [{
    "message": {
      "content": "{\n  \"ticket\": [\n    {\n      \"travel_date\": \"2013-06-29\",\n      \"trains\": \"流水\",\n      \"seat_num\": \"371\",\n      \"arrival_site\": \"开发区\",\n      \"price\": \"8.00\"\n    }\n  ],\n  \"invoice\": [\n    {\n      \"invoice_code\": \"221021325353\",\n      \"invoice_number\": \"10283819\"\n    }\n  ]\n}",
      "role": "assistant"
    },
    "finish_reason": "stop",
    "index": 0,
    "logprobs": null
  }],
  "object": "chat.completion",
  "usage": {
    "prompt_tokens": 486,
    "completion_tokens": 112,
    "total_tokens": 598,
    "prompt_tokens_details": {
      "cached_tokens": 0
    }
  },
  "created": 1755767481,
  "system_fingerprint": null,
  "model": "qwen-vl-max",
  "id": "chatcmpl-33249829-e9f3-9cbc-93e4-0536b3d7d713"
}

DashScope

Python

import os
import dashscope
messages = [
{
    "role": "system",
    "content": [
    {"text": "You are a helpful assistant."}]
},
{
    "role": "user",
    "content": [
    {"image": "http://duguang-labelling.oss-cn-shanghai.aliyuncs.com/demo_ocr/receipt_zh_demo.jpg"},
    {"text": "提取图中ticket(包括 travel_date、trains、seat_num、arrival_site、price)和 invoice 的信息(数组类型,包括 invoice_code 和 invoice_number ),请输出包含 ticket 和 invoice 数组的JSON"}]
}]
response = dashscope.MultiModalConversation.call(
    #若没有配置环境变量, 请用百炼API Key将下行替换为: api_key ="sk-xxx"
    api_key = os.getenv('DASHSCOPE_API_KEY'),
    model = 'qwen-vl-max',
    messages = messages,
    response_format={'type': 'json_object'}
)
json_string = response.output.choices[0].message.content[0]["text"]
print(json_string)

返回结果

{
  "ticket": [
    {
      "travel_date": "2013-06-29",
      "trains": "流水",
      "seat_num": "371",
      "arrival_site": "开发区",
      "price": "8.00"
    }
  ],
  "invoice": [
    {
      "invoice_code": "221021325353",
      "invoice_number": "10283819"
    }
  ]
}

Java

// DashScope Java SDK 版本需要不低于 2.21.4

import java.util.Arrays;
import java.util.Collections;
import com.alibaba.dashscope.aigc.multimodalconversation.MultiModalConversation;
import com.alibaba.dashscope.aigc.multimodalconversation.MultiModalConversationParam;
import com.alibaba.dashscope.aigc.multimodalconversation.MultiModalConversationResult;
import com.alibaba.dashscope.common.MultiModalMessage;
import com.alibaba.dashscope.common.Role;
import com.alibaba.dashscope.exception.ApiException;
import com.alibaba.dashscope.exception.NoApiKeyException;
import com.alibaba.dashscope.exception.UploadFileException;
import com.alibaba.dashscope.common.ResponseFormat;

public class Main {
    public static void simpleMultiModalConversationCall()
            throws ApiException, NoApiKeyException, UploadFileException {
        MultiModalConversation conv = new MultiModalConversation();
        MultiModalMessage systemMessage = MultiModalMessage.builder().role(Role.SYSTEM.getValue())
                .content(Arrays.asList(
                        Collections.singletonMap("text", "You are a helpful assistant."))).build();
        MultiModalMessage userMessage = MultiModalMessage.builder().role(Role.USER.getValue())
                .content(Arrays.asList(
                        Collections.singletonMap("image", "http://duguang-labelling.oss-cn-shanghai.aliyuncs.com/demo_ocr/receipt_zh_demo.jpg"),
                        Collections.singletonMap("text", "提取图中ticket(包括 travel_date、trains、seat_num、arrival_site、price)和 invoice 的信息(数组类型,包括 invoice_code 和 invoice_number ),请输出包含 ticket 和 invoice 数组的JSON"))).build();
        ResponseFormat jsonMode = ResponseFormat.builder().type("json_object").build();
        MultiModalConversationParam param = MultiModalConversationParam.builder()
                // 若没有配置环境变量,请用百炼API Key将下行替换为:.apiKey("sk-xxx")
                .apiKey(System.getenv("DASHSCOPE_API_KEY"))
                .model("qwen-vl-max")
                .messages(Arrays.asList(systemMessage, userMessage))
                .responseFormat(jsonMode)
                .build();
        MultiModalConversationResult result = conv.call(param);
        System.out.println(result.getOutput().getChoices().get(0).getMessage().getContent().get(0).get("text"));
    }
    public static void main(String[] args) {
        try {
            simpleMultiModalConversationCall();
        } catch (ApiException | NoApiKeyException | UploadFileException e) {
            System.out.println(e.getMessage());
        }
        System.exit(0);
    }
}

返回结果

{
  "ticket": [
    {
      "travel_date": "2013-06-29",
      "trains": "流水",
      "seat_num": "371",
      "arrival_site": "开发区",
      "price": "8.00"
    }
  ],
  "invoice": [
    {
      "invoice_code": "221021325353",
      "invoice_number": "10283819"
    }
  ]
}

curl

curl -X POST https://dashscope.aliyuncs.com/api/v1/services/aigc/multimodal-generation/generation \
-H "Authorization: Bearer $DASHSCOPE_API_KEY" \
-H 'Content-Type: application/json' \
-d '{
    "model": "qwen-vl-max",
    "input":{
        "messages":[
            {"role": "system",
         "content": [
           {"text": "You are a helpful assistant."}]},
            {
             "role": "user",
             "content": [
               {"image": "http://duguang-labelling.oss-cn-shanghai.aliyuncs.com/demo_ocr/receipt_zh_demo.jpg"},
               {"text": "提取图中ticket(包括 travel_date、trains、seat_num、arrival_site、price)和 invoice 的信息(数组类型,包括 invoice_code 和 invoice_number ),请输出包含 ticket 和 invoice 数组的JSON"}
                ]
            }
        ],
    "parameters": {
        "response_format": {"type": "json_object"}
    }
    }
}'

返回结果

{
  "output": {
    "choices": [{
      "message": {
        "content": [{
          "text": "```json\n{\n  \"ticket\": [\n    {\n      \"travel_date\": \"2013-06-29\",\n      \"trains\": \"流水\",\n      \"seat_num\": \"371\",\n      \"arrival_site\": \"开发区\",\n      \"price\": \"8.00\"\n    }\n  ],\n  \"invoice\": [\n    {\n      \"invoice_code\": \"221021325353\",\n      \"invoice_number\": \"10283819\"\n    }\n  ]\n}\n```"
        }],
        "role": "assistant"
      },
      "finish_reason": "stop"
    }]
  },
  "usage": {
    "input_tokens_details": {
      "image_tokens": 418,
      "text_tokens": 65
    },
    "prompt_tokens_details": {
      "cached_tokens": 0
    },
    "total_tokens": 599,
    "output_tokens": 116,
    "input_tokens": 483,
    "output_tokens_details": {
      "text_tokens": 116
    },
    "image_tokens": 418
  },
  "request_id": "9261e910-b810-91e9-88cf-9f7e0eb3750c"
}

解析 JSON 字符串

在获取大模型输出的 JSON 字符串后,您可以直接使用 JSON 工具进行解析。

# 步骤 2:解析 JSON 字符串。请将以下代码添加到步骤 1 之后
import json
json_object = json.loads(json_string)
print(json_object)
// 步骤 2:解析 JSON 字符串。请将以下代码添加到步骤 1 之后
const jsonObject = JSON.parse(jsonString);
console.log(jsonObject);
// 需要添加json依赖
import org.json.JSONObject;

...

String jsonString = result.getOutput().getChoices().get(0).getMessage().getContent();
JSONObject jsonObject = new JSONObject(jsonString);
System.out.println(jsonObject);

运行以上代码可以将 JSON 字符串转换为 JSON 对象。

常见问题

Q: 通义千问 API 是否支持根据我提供的 JSON Schema 生成数据?

A:当前通义千问 API 支持根据您输入的提示词生成合法的 JSON 字符串,无法根据您提供的 JSON Schema 来生成。

您可以在提示词中明确描述所需 JSON 的键值结构和数据类型,并提供标准数据样例,这会帮助大模型达到类似效果。

错误码

如果模型调用失败并返回报错信息,请参见错误信息进行解决。