fix(core): surface real http_context error from STT streams#5709
Merged
Conversation
Move `_ensure_session()` out of `SpeechStream.__init__` for inference and
cartesia STT. Previously, the base `RecognizeStream.__init__` scheduled
`_main_task` before the subclass finished init, so when `_ensure_session()`
raised `RuntimeError` (no http context), the orphan task ran and surfaced
`AttributeError: '_session'` — masking the real error.
Add `utils.http_context.open()` async context manager for running plugins
outside a job (tests, scripts):
async with utils.http_context.open():
async with AgentSession() as session:
await session.start(MyAgent())
Update the `http_session()` RuntimeError message to point users to the helper.
theomonnom
approved these changes
May 12, 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.
Summary
When
AgentSessionruns outside a job process (tests, scripts, ad-hoc usage), there's no http session bound to the event loop. The intent was forutils.http_context.http_session()to raise a clearRuntimeError, but two bugs combined to hide it:Init race in inference & cartesia
SpeechStream. The baseRecognizeStream.__init__schedules_main_taskviaasyncio.create_task(...)before the subclass finishes init. The subclass then ranself._session = stt._ensure_session(), which can raiseRuntimeErrorfromhttp_session(). When it did,self._sessionwas never assigned — but the already-scheduled task kept running, eventually hittingself._session.ws_connect(...)→AttributeError: 'SpeechStream' object has no attribute '_session'. The originalRuntimeErrorgot swallowed and users saw a crypticAttributeErrorwithTask exception was never retrieved.No ergonomic way to set up the context outside a worker. Users running plugins in tests had to hand-roll an aiohttp.ClientSession and pass it to every plugin constructor.
Changes
livekit-agents/livekit/agents/inference/stt.pyandlivekit-plugins/livekit-plugins-cartesia/livekit/plugins/cartesia/stt.py: move_ensure_session()out ofSpeechStream.__init__and into_run(). The realRuntimeErrornow propagates through the task naturally.livekit-agents/livekit/agents/utils/http_context.py: add a publicopen()async context manager for running plugins outside a job:It's a no-op pass-through when an http context is already set up (e.g. inside a worker, or nested calls).
Update the
http_session()RuntimeErrormessage to point users at the new helper.Notes
Supersedes #5608's approach of having
AgentSessionown the http context.