Connect Existing OpenAPIs to LLMs with Spring AI
Function calling allows LLMs to access external data to perform certain tasks. Spring AI already supports function calling. Function in Spring AI are simple Java Function s.
There are already many public or private APIs exposed using OpenAPI spec. When developing AI apps, we may want to connect these OpenAPIs to LLMs.
I created a small tool to generate OpenAPI clients with Spring AI configurations, which turns operations in OpenAPI into functions in Spring AI. This tool is based on OpenAPI Generator.
Let’s see an example how to use this tool. I use this universities search service and create an OpenAPI spec for this service.
Run OpenAPI generator with my custom generator. See the pom.xml in the example. Put the custom generator as a dependency of openapi-generator-maven-plugin .
<plugin>
<groupId>org.openapitools</groupId>
<artifactId>openapi-generator-maven-plugin</artifactId>
<version>${openapi-generator-version}</version>
<executions>
<execution>
<id>generate-client-code</id>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<generatorName>Java</generatorName>
<library>native</library>
<inputSpec>${project.basedir}/universities.yaml</inputSpec>
</configuration>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>io.github.alexcheng1982</groupId>
<artifactId>openapi-function-spring-ai-generator</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
</plugin>After running the generation, a Spring Configuration will be generated for each API. See the UniversitiesApiSpringAiFunctionConfiguration below.
For each operation, the generator generates:
- A record for request type.
- A record for response type.
- A bean of type Function to invoke the corresponding operation.
@Configuration
public class UniversitiesApiSpringAiFunctionConfiguration {
public record Request_searchUniversities(String country, Integer limit) {}
public record Response_searchUniversities(List<University> value) {}
@Bean
@Description("Search universities")
public Function<Request_searchUniversities, Response_searchUniversities> searchUniversities(UniversitiesApi apiInvoker) {
return (request) -> {
try {
return new Response_searchUniversities(apiInvoker.searchUniversities(request.country(), request.limit()));
} catch (ApiException e) {
throw new RuntimeException(e);
}
};
}
}The generated OpenAPI client should then be published for later usage.
To use the generated OpenAPI client in Spring AI:
1. Add the generated client into your project.
2. Import the Configurationor use component scanning.
@Import({
UniversitiesApiSpringAiFunctionConfiguration.class
})3. Declare an API bean.
@Bean
public UniversitiesApi universitiesApi() {
return new UniversitiesApi();
}4. Use the function in Spring AI.
See here for a guide of how to use functions with OpenAI.
searchUniversities is the name of an operation defined in the OpenAPI spec, which is also the name of function to call.
OpenAiChatClient chatClient = …
UserMessage userMessage = new UserMessage(
"Find 10 universities in United Kingdom, output the name and website in CSV format.");
ChatResponse response = chatClient.call(new Prompt(List.of(userMessage),
OpenAiChatOptions.builder().withFunction("searchUniversities")
.build())); // (1) Enable the functionMore info on the GitHub.






