Spring / Spring AI interview questions
What is function calling (tool use) in Spring AI and how do you register a function?
Function calling — also called tool use — is a model capability where, instead of fabricating an answer, the LLM decides to invoke a named function that your application provides, waits for the result, and uses it to compose its final response. This gives the model access to real-time data, private systems, and external APIs without those capabilities needing to be baked into the model's weights.
In Spring AI you register tools as plain Spring beans whose type is Function<Input, Output>. The @Description annotation provides the natural language hint the model uses to decide when to call it. Parameter schema is inferred from the input record's fields.
@Configuration public class WeatherTools { @Bean @Description("Returns current weather conditions for a city") public Function<WeatherRequest, WeatherResponse> getWeather( WeatherService svc) { return req -> svc.fetchWeather(req.city()); } } record WeatherRequest(String city) {} record WeatherResponse(String city, double tempC, String conditions) {} // Call site String answer = chatClient.prompt() .user("What is the weather in Berlin right now?") .tools("getWeather") // pass the @Bean name .call().content();
Spring AI handles the entire tool loop transparently: it sends the tool definitions to the model, detects when the model wants to invoke one, calls the registered bean with the model's arguments, wraps the result in a ToolResponseMessage, and re-calls the model. The caller just receives the final natural language answer.
More Related questions...