Lazily initialize OpenAI client so local routers import without an API key#91
Open
zl190 wants to merge 1 commit into
Open
Lazily initialize OpenAI client so local routers import without an API key#91zl190 wants to merge 1 commit into
zl190 wants to merge 1 commit into
Conversation
…I key `similarity_weighted/utils.py` created the OpenAI client at module import time (`OPENAI_CLIENT = OpenAI()`). Because this module is imported transitively by `routers.py` and `matrix_factorization/model.py`, importing *any* router forced an OpenAI client to be constructed, which requires `OPENAI_API_KEY`. As a result `from routellm.controller import Controller` raised `openai.OpenAIError: Missing credentials` even when the user only wanted a fully-local router (e.g. `bert` or `causal_llm`) that never calls OpenAI. Replace the module-level instantiation with a cached `get_openai_client()` accessor that constructs the client on first use. Behavior is identical when a key is present (the sw_ranking / mf embedding path still gets a client), but local routers now import without any credentials. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
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.
Problem
routellm/routers/similarity_weighted/utils.pyinstantiated the OpenAI client at module import time:Constructing
OpenAI()requiresOPENAI_API_KEY. Because this module is imported transitively byrouters.pyandmatrix_factorization/model.py, importing any router forced a client to be built — even fully-local routers (bert,causal_llm) that never call OpenAI. Sofrom routellm.controller import Controllercrashed withopenai.OpenAIError: Missing credentialsfor users who only wanted a local router.This is the symptom behind issues like #21 and #38.
Reproduce (before this PR)
In a fresh env with no
OPENAI_API_KEY:Fix
Make the client lazy — instantiate on first use, not at import. Replace the module-level
OPENAI_CLIENT = OpenAI()with a cached accessor:and update the two call sites (
SWRankingRouterinrouters.py,MFModelinmatrix_factorization/model.py) to callget_openai_client(). Behavior is identical when a key is present — the sw_ranking / mf embedding path still gets a (now cached) client. No routing-logic or signature changes.Scope note:
openai_server.py(AsyncOpenAI()) andevals/find_contaminated.py(OpenAI()) also instantiate at import, but neither is on the router/Controllerimport path — they're a standalone server entrypoint and an eval script — so they don't affect importing routers. Left untouched to keep this diff minimal.Result
bert&causal_llmnow import and construct keyless.🤖 Generated with Claude Code