Repository navigation
fix(openai): keep Azure connections alive between turns in with_azure() - #8793
Merged
Merged
Conversation
Contributor
Author
|
/devin review |
davidzhao
approved these changes
Nov 26, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #8792
Problem.
LLM.with_azure(),TTS.with_azure()andSTT.with_azure()createAsyncAzureOpenAIwithout anhttp_client, so the OpenAI SDK's default pool applies and closes idle connections after 5 seconds. The default constructors pass anhttpx.AsyncClientwithkeepalive_expiry=120. In a voice agent the pause between two requests is usually longer than 5 seconds, so almost every Azure request opens a new TCP and TLS connection.Fix. Each
with_azure()passes the samehttpx.AsyncClientits class's constructor builds: 50 connections,keepalive_expiry=120,follow_redirects=True, and the giventimeoutor the existing default. The SDK client keeps the sametimeoutargument as before, so request timeouts don't change. The Azure pool limit drops from the SDK's 1000 connections to 50, the same as the default constructors.STT.aclose()now also closes the OpenAI client the STT created, asLLMandTTSalready do. It never did, soSTT.with_azure()relied on the SDK's garbage-collection cleanup, which a plainhttpx.AsyncClientdoesn't have. A client the caller passes in stays open.Tests.
test_with_azure_keeps_its_connection_between_turnsruns for LLM, TTS and STT. It sends two requests to a local HTTP/1.1 server and moves the connection pool's clock 10 seconds forward in between. With the fix the client reuses one connection. Onmainit opens two, and all three cases fail. A control case runs a plainAsyncAzureOpenAIand expects two connections, so the test fails if the clock patch ever stops reaching the pool.test_stt_aclose_closes_only_the_client_it_ownscovers the STT change.ruff check,ruff format --checkandmypy -p livekit.plugins.openaipass.We found this in production and measured it against two real Azure OpenAI resources, in Sweden Central and East US 2, with three requests 10 seconds apart.
LLM.with_azure()opened a new connection for each request. An Azure client with the same 120-second pool as this PR opened one for the first request and reused it for the other two.