From 30dbc175aecf894029d42923e077c543e6097f72 Mon Sep 17 00:00:00 2001 From: sgoel2be24-cyber <223222024+sgoel2be24-cyber@users.noreply.github.com> Date: Sat, 10 Oct 2026 13:29:19 +0530 Subject: [PATCH] Improve ContextVar BYOK provider typing MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit set_context_provider/reset_context_provider now use the typed Token[Optional[LLMProvider]] — a Token from a different ContextVar is a type error instead of a silent cross-context reset. Lifecycle docs clarified (pair set/reset in a finally; the provider never outlives its request). --- core/llm_context.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/core/llm_context.py b/core/llm_context.py index 978a1bf..99332c6 100644 --- a/core/llm_context.py +++ b/core/llm_context.py @@ -35,15 +35,19 @@ ) -def set_context_provider(provider: Optional["LLMProvider"]) -> Token: +def set_context_provider( + provider: Optional["LLMProvider"], +) -> Token[Optional["LLMProvider"]]: """Set the provider for the current context (request). Returns a Token that MUST be passed to reset_context_provider when the request ends - see api/dependencies for the yield-dependency that does this.""" return _current_llm_provider.set(provider) -def reset_context_provider(token: Token) -> None: - """Undo a prior set_context_provider call. Always call this in a +def reset_context_provider(token: Token[Optional["LLMProvider"]]) -> None: + """Undo a prior set_context_provider call. The Token is typed to this + ContextVar's value type, so a Token from a different ContextVar is a + type error, not a silent cross-context reset. Always call this in a `finally` block paired with the `set_context_provider` that produced `token`, so a provider never outlives the request that set it.""" _current_llm_provider.reset(token)