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

Spring AI中如何定义无输入参数的函数调用?

Spring AI无输入参数函数调用的正确定义方式

在Spring AI中直接使用Void作为函数输入类型时,会导致生成的OpenAPI schema不符合大模型(如Azure OpenAI)的要求,进而触发400错误。以下是几种正确的无参数函数定义方式:

方法一:使用Map<String, Object>作为输入类型

@Bean
@Description("return list of Vets, include their specialist")
public Function<Map<String, Object>, Collection<Vet>> queryVets() {
    return request -> vetRepository.findAll();
}

函数内部无需使用输入的Map参数,Spring AI会生成允许空属性的对象schema,满足大模型的校验要求。

方法二:自定义空DTO作为输入类型

先定义一个空的请求DTO类:

public class EmptyVetQueryRequest {}

再基于该类定义函数Bean:

@Bean
@Description("return list of Vets, include their specialist")
public Function<EmptyVetQueryRequest, Collection<Vet>> queryVets() {
    return request -> vetRepository.findAll();
}

自定义空类会让Spring AI生成无属性的合法对象schema,符合大模型对无参数函数的格式要求。

方法三:使用Spring AI内置的VoidInput类型(版本兼容时)

如果你的Spring AI版本支持VoidInput类,可直接使用:

import org.springframework.ai.function.VoidInput;

@Bean
@Description("return list of Vets, include their specialist")
public Function<VoidInput, Collection<Vet>> queryVets() {
    return request -> vetRepository.findAll();
}

VoidInput是Spring AI专门为无输入参数场景提供的类型,能自动生成符合规范的schema。

错误原因说明

Java的Void类型是特殊的标记类型,Spring AI无法将其解析为大模型要求的“空对象schema”,而是生成了缺失属性的无效结构,因此触发大模型的参数校验错误。

内容的提问来源于stack exchange,提问作者Li Truman

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.22 14:18:13