如下面的代码 ``` Toolkit toolkit = new Toolkit(); toolkit.registerTool(new Tool1()); toolkit.registerTool(new Tool2()); ObjectMapper om = new ObjectMapper(); JsonNode schema = om.readTree(""" { "type": "object", "properties": { "sentiment": { "type": "string", "enum": ["positive", "negative", "neutral"] }, "confidence": { "type": "number" } }, "required": ["sentiment", "confidence"] } """); ReActAgent agent = ReActAgent.builder() .name("GuardedAgent") .sysPrompt("你是一个助理") .model("dashscope:qwen-plus") .toolkit(toolkit) .build(); Msg result = agent.call(List.of(new UserMessage("美丽")), schema).block(); System.out.println(result.getTextContent()); ``` 当不设定tookkit时返回正确 ``` {"confidence":0.95,"sentiment":"positive"} ``` 当设定工具后返回 ``` generate_response({"response": {"confidence": 0.95, "sentiment": "positive"}}) ``` 甚至是纯文本
Activity
oss-maintainer commentedon Oct 3, 2026
jujn commentedon Oct 3, 2026
DashScope 只在一部分模型支持严格的 JSON Schema 结构化输出:
https://help.aliyun.com/zh/model-studio/qwen-structured-output#支持的模型框架这边是用了一个工具(generate_response)来强制模型结构化输出。
可以用
ToolChoice.Specific("generate_response")强制模型调用这个工具,或者换个好点的模型(qwen-plus 老古董了)zt18661205847 commentedon Oct 4, 2026
为什么不通过 模型支持schema适配+system prompt做schema兜底来做呢?纯用工具的话它有可能会不掉,特别是工具配备比较多的情况
feat(core): Enhance reliability of structured output degradation path (…