Skip to content

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
muddlebee:fix/openai-llm-client-lazy-init
May 5, 2026
Merged

fix(llm): lazy OpenAI client init fix regression tests and Run full tests in PR CI#1291
muddlebee merged 4 commits intoTracer-Cloud:mainfrom
muddlebee:fix/openai-llm-client-lazy-init

Conversation

@muddlebee
Copy link
Copy Markdown
Collaborator

@muddlebee muddlebee commented May 5, 2026

Summary

Fixes CI failures when the OpenAI Python SDK rejects an empty api_key at OpenAI() construction (e.g. 2.34+). Construction is deferred until _ensure_client when a key is available.

Changes

  • OpenAILLMClient: lazy SDK init via _build_client; explicit guard in invoke if 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.

- 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.
@greptile-apps
Copy link
Copy Markdown
Contributor

greptile-apps Bot commented May 5, 2026

Greptile Summary

  • Defers OpenAI SDK construction from __init__ to _ensure_client via a new _build_client helper, fixing CI failures when OpenAI SDK 2.34+ rejects an empty api_key at construction time.
  • Adds four regression tests covering deferred init, missing-key invoke failure, key rotation triggering a rebuild, and construction-call tracking via _FakeOpenAI.init_api_keys.
  • The if client is None guard added in invoke (lines 315–316) is unreachable since _ensure_client either raises or guarantees a non-None client before returning.

Confidence Score: 4/5

Safe 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 app/services/llm_client.py lines 314-316 is worth cleaning up but does not affect correctness.

Important Files Changed

Filename Overview
app/services/llm_client.py Lazy OpenAI client init deferred to _ensure_client; guard added in invoke is unreachable dead code after the refactor.
tests/services/test_llm_client.py New regression tests cover deferred init, missing-key guard, key rotation, and construction tracking; init_api_keys class variable not reset in all test functions.

Sequence Diagram

sequenceDiagram
    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
Loading

Reviews (1): Last reviewed commit: "fix(llm): lazy OpenAI client init and re..." | Re-trigger Greptile

Comment thread app/services/llm_client.py Outdated
Comment thread tests/services/test_llm_client.py
muddlebee added 3 commits May 5, 2026 10:53
- 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.
@muddlebee muddlebee changed the title fix(llm): lazy OpenAI client init and regression tests fix(llm): lazy OpenAI client init fix regression tests and Run full tests in PR CI May 5, 2026
@muddlebee muddlebee merged commit 948068a into Tracer-Cloud:main May 5, 2026
10 checks passed
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented May 5, 2026

🏄 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant