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.
Invest now in Acorns!!! 🚀
Join Acorns and get your $5 bonus!
Acorns is a micro-investing app that automatically invests your "spare change" from daily purchases into diversified, expert-built portfolios of ETFs. It is designed for beginners, allowing you to start investing with as little as $5. The service automates saving and investing. Disclosure: I may receive a referral bonus.
Invest now!!! Get Free equity stock (US, UK only)!
Use Robinhood app to invest in stocks. It is safe and secure. Use the Referral link to claim your free stock when you sign up!.
The Robinhood app makes it easy to trade stocks, crypto and more.
Webull! Receive free stock by signing up using the link: Webull signup.
More Related questions...
