fix(llm): lazy OpenAI client init fix regression tests and Run full tests in PR CI#1291
Merged
muddlebee merged 4 commits intoTracer-Cloud:mainfrom May 5, 2026
Merged
Conversation
- Construct OpenAI SDK client in _ensure_client with _build_client helper. - Avoid OpenAI() during __init__ when key is absent (OpenAI 2.34+ rejects empty key). - Add tests for lazy init, missing-key invoke, and key rotation rebuild.
Contributor
Greptile Summary
Confidence Score: 4/5Safe to merge — the lazy-init fix is correct and well-tested; only minor P2 style findings. All findings are P2: one unreachable guard (dead code, not a runtime hazard) and one mutable class-variable hygiene issue in tests. No logic errors or security concerns introduced. No files require special attention; the dead-code guard in Important Files Changed
Sequence DiagramsequenceDiagram
participant Caller
participant OpenAILLMClient
participant _ensure_client
participant _build_client
participant OpenAI_SDK
Caller->>OpenAILLMClient: __init__(model=...)
note over OpenAILLMClient: self._client = None (deferred)
Caller->>OpenAILLMClient: invoke(prompt)
OpenAILLMClient->>_ensure_client: _ensure_client()
_ensure_client->>_ensure_client: resolve_llm_api_key()
alt api_key is empty
_ensure_client-->>Caller: raise RuntimeError("Missing KEY")
else client is None or key rotated
_ensure_client->>_build_client: _build_client(api_key)
_build_client->>OpenAI_SDK: OpenAI(api_key, base_url, ...)
OpenAI_SDK-->>_build_client: client instance
_build_client-->>_ensure_client: client
_ensure_client->>OpenAILLMClient: self._client = client
end
OpenAILLMClient->>OpenAI_SDK: client.chat.completions.create(...)
OpenAI_SDK-->>Caller: LLMResponse
Reviews (1): Last reviewed commit: "fix(llm): lazy OpenAI client init and re..." | Re-trigger Greptile |
- Drop fork-only safe subset (previously skipped most tests/e2e). - Run the same coverage pytest command as main pushes. - Upload coverage artifacts for fork PRs too.
- Return OpenAI client from _ensure_client and use it in invoke. - Remove unreachable none-check after successful _ensure_client. - Add autouse fixture to reset _FakeOpenAI shared state per test.
- update OpenAI guardrail fixtures to return client._client from patched _ensure_client. - keep lazy-client production behavior while preserving test stubs. - fix CI failures in OpenAI guardrail redaction tests.
Contributor
|
🏄 Some PRs rot in review for six weeks. @muddlebee's said "not today" and merged like it owned the place. 🌊 👋 Join us on Discord - OpenSRE : hang out, contribute, or hunt for features and issues. Everyone's welcome. |
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
Fixes CI failures when the OpenAI Python SDK rejects an empty
api_keyatOpenAI()construction (e.g. 2.34+). Construction is deferred until_ensure_clientwhen a key is available.Changes
OpenAILLMClient: lazy SDK init via_build_client; explicit guard ininvokeif client is unset.tests/services/test_llm_client.py: defer-init, missing-key invoke, key rotation, and construction tracking.Related
Deps / lockfile / CI: #1292. Prefer merging this PR first.