Skip to content

fix(openai): keep Azure connections alive between turns in with_azure() - #8793

Merged
davidzhao merged 4 commits into
livekit:mainfrom
furkan-v:fix/openai-azure-keepalive
Nov 26, 2026
Merged

davidzhao merged 4 commits into
livekit:mainfrom
furkan-v:fix/openai-azure-keepalive

Conversation

@furkan-v

@furkan-v furkan-v commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #8792

Problem. LLM.with_azure(), TTS.with_azure() and STT.with_azure() create AsyncAzureOpenAI without an http_client, so the OpenAI SDK's default pool applies and closes idle connections after 5 seconds. The default constructors pass an httpx.AsyncClient with keepalive_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 same httpx.AsyncClient its class's constructor builds: 50 connections, keepalive_expiry=120, follow_redirects=True, and the given timeout or the existing default. The SDK client keeps the same timeout argument 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, as LLM and TTS already do. It never did, so STT.with_azure() relied on the SDK's garbage-collection cleanup, which a plain httpx.AsyncClient doesn't have. A client the caller passes in stays open.

Tests. test_with_azure_keeps_its_connection_between_turns runs 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. On main it opens two, and all three cases fail. A control case runs a plain AsyncAzureOpenAI and expects two connections, so the test fails if the clock patch ever stops reaching the pool. test_stt_aclose_closes_only_the_client_it_owns covers the STT change.

$ pytest --unit --plugin openai tests/test_plugin_openai.py tests/test_plugin_openai_stt.py tests/test_plugin_openai_responses.py tests/test_plugin_openai_decisions.py
181 passed

ruff check, ruff format --check and mypy -p livekit.plugins.openai pass.

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.

@furkan-v
furkan-v requested a review from a team as a code owner November 26, 2026 09:34
@CLAassistant

CLAassistant commented Nov 26, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

devin-ai-integration[bot]

This comment was marked as resolved.

@furkan-v

Copy link
Copy Markdown
Contributor Author

/devin review

@davidzhao davidzhao left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lg

@davidzhao
davidzhao merged commit 0677f49 into livekit:main Nov 26, 2026
16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

openai: with_azure() closes idle connections after 5s, so most Azure requests open a new connection

3 participants