If you are new to Semantic Kernel, I would point you to one of my earlier posts. In this post I want to show how you can change the timeout values when using Semantic Kernel.
The power of Semantic Kernel is that it gives you the ability to interact with multiple (large language) models in an uniform way. You interact using C#, Java or Python with the Semantic Kernel SDK and behind the scenes it will do the necessary API calls to OpenAI, Azure OpenAI, Hugging Face or a local OpenAI compatible tool like Ollama.
Of course as we are interacting with an API behind the scenes, it can happen that the API doesn’t return any results in time and that we get a timeout exception.
The operation was cancelled because it exceeded the configured timeout.
Let me share how I fixed it…
Use a custom HttpClient
One option you have is to explicitly pass an HttpClient
instance when creating the Semantic Kernel instance:
Retry when a timeout happens
If the timeout typically happens because the AI platform that you are targeting reacts slow sometimes, it is maybe a better idea to configure a retry policy instead of changing the timeout value. This can be easily done in .NET Core by adding a ResilienceHandler
:
More information
Build resilient HTTP apps: Key development patterns - .NET | Microsoft Learn