Skip to content

fix: migrate inference client to httpx2 - #6826

Open
chenghao-mou wants to merge 1 commit into
mainfrom
chenghao/fix/AGT-3286-declare-httpx-dependency
Open

fix: migrate inference client to httpx2#6826
chenghao-mou wants to merge 1 commit into
mainfrom
chenghao/fix/AGT-3286-declare-httpx-dependency

Conversation

@chenghao-mou

@chenghao-mou chenghao-mou commented Aug 12, 2026

Copy link
Copy Markdown
Member

livekit-agents configures its inference HTTP transport directly. OpenAI 3
uses HTTPX2, so standalone installs should not depend on HTTPX arriving
transitively.

Use OpenAI's supported HTTPX2 factory and preserve the existing timeout,
retry, and connection limits. Require OpenAI 2.47, the first 2.x release that
accepts HTTPX2 clients.

Addresses AGT-3286

Initial prompt and agent context

Model: GPT-5.6

we need to add httpx because the new openai client dropped it and we did not declare it

change of plan, can we upgrade to httpx2 instead?

@chenghao-mou
chenghao-mou requested a review from a team as a code owner August 12, 2026 17:26

@devin-ai-integration devin-ai-integration Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

✅ Devin Review: No Issues Found

Devin Review analyzed this PR and found no potential bugs to report.

View in Devin Review to see 1 additional finding.

Open in Devin Review

@chenghao-mou
chenghao-mou force-pushed the chenghao/fix/AGT-3286-declare-httpx-dependency branch from 1e5741e to 07f9b59 Compare August 12, 2026 17:34
@chenghao-mou chenghao-mou changed the title fix: declare httpx dependency fix: migrate inference client to httpx2 Aug 12, 2026

@devin-ai-integration devin-ai-integration Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Devin Review found 1 new potential issue.

View 3 additional findings in Devin Review.

Open in Devin Review

Comment on lines +489 to 492
except httpx2.TimeoutException as e:
# Only the request call runs inside the openai client's error mapping, so a
# timeout waiting on the stream body arrives as the raw httpx exception.
# timeout waiting on the stream body arrives as the raw httpx2 exception.
raise APITimeoutError(retryable=retryable) from e

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Stalled responses from OpenAI-compatible providers are now reported as connection failures instead of timeouts

A stalled response body is only recognised as a timeout when it comes from the new HTTP library (except httpx2.TimeoutException at livekit-agents/livekit/agents/inference/llm.py:489), so providers still using the old HTTP library report a generic connection failure instead of a timeout.
Impact: Users and metrics of OpenAI-plugin-backed models (OpenAI, Groq, Cerebras, xAI, DeepSeek, Google AI Platform, …) see mislabelled connection errors when a provider goes quiet mid-stream.

Mechanism: shared LLMStream._run is reused by plugins that build httpx (v1) clients

livekit.agents.inference.llm.LLMStream is subclassed by livekit-plugins-openai (livekit-plugins/livekit-plugins-openai/livekit/plugins/openai/llm.py:1041) and used by livekit-plugins-google (livekit-plugins/livekit-plugins-google/livekit/plugins/google/aiplatform_llm.py:41); neither overrides _run. Those plugins construct their openai.AsyncClient with a plain httpx.AsyncClient (livekit-plugins/livekit-plugins-openai/livekit/plugins/openai/llm.py:165), so a read timeout while iterating the SSE body raises httpx.ReadTimeout, not httpx2.ReadTimeout. Since the request itself is the only part covered by the openai client's error mapping, that raw exception previously matched except httpx.TimeoutException and produced APITimeoutError; now it falls through to the trailing except Exception and produces APIConnectionError. Fix by catching both exception families (import httpx defensively, e.g. except (httpx2.TimeoutException, httpx.TimeoutException)).

Prompt for agents
In livekit-agents/livekit/agents/inference/llm.py, LLMStream._run now only catches httpx2.TimeoutException for stream-body stalls. This class is inherited by livekit-plugins-openai's LLMStream and used by livekit-plugins-google's aiplatform_llm, both of which pass openai clients built on the original httpx (v1) library. For those callers a mid-stream read timeout raises httpx.TimeoutException, which now falls into the generic `except Exception` branch and is surfaced as APIConnectionError instead of APITimeoutError. Make the handler tolerant of both HTTP client libraries, e.g. by importing httpx lazily/optionally and catching a tuple of both timeout exception types (falling back gracefully if httpx is not installed, since it is no longer a declared dependency of livekit-agents).
Open in Devin Review

Was this helpful? React with 👍 or 👎 to provide feedback.

@longcw

longcw commented Aug 13, 2026

Copy link
Copy Markdown
Contributor

the plugins still depend on the httpx, should we add httpx to the agent instead?

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.

2 participants