fix: invalidate cached OpenAI client when credentials change - #4505
fix: invalidate cached OpenAI client when credentials change#4505praisonai-triage-agent[bot] wants to merge 2 commits into
Conversation
β¦4488) Key the env-only getOpenAIClient() cache on API key + base URL instead of mere existence, so a rotated/updated key or a changed base URL is picked up without restarting the process. Add resetOpenAIClient() for tests and settings-driven rebuilds. The identity string is never logged. Co-authored-by: MervinPraison <MervinPraison@users.noreply.github.com>
|
@coderabbitai review |
|
/review |
Qodo reviews are paused for this user.Troubleshooting steps vary by plan Learn more β On a Teams plan? Using GitHub Enterprise Server, GitLab Self-Managed, or Bitbucket Data Center? |
Greptile SummaryThe PR updates the TypeScript OpenAI fallback client cache so API-key or base-URL changes rebuild the client, while explicit resets invalidate both cached fields.
Confidence Score: 5/5The PR appears safe to merge. No blocking failure remains.
|
| Filename | Overview |
|---|---|
| src/praisonai-ts/src/llm/openai.ts | Replaces the unkeyed singleton with an identity-keyed cache and safely returns the locally constructed client across asynchronous logging. |
| src/praisonai-ts/tests/unit/llm/openai-client-cache.test.ts | Adds unit coverage for credential and endpoint invalidation, stable reuse, reset behavior, missing keys, and the previously reported races. |
Reviews (2): Last reviewed commit: "fix: return locally-built client to clos..." | Re-trigger Greptile
|
@claude You are the FINAL architecture reviewer. If the branch is under MervinPraison/PraisonAI (not a fork), you are able to make modifications to this branch and push directly. SCOPE: Review changes in this PR. Python SDK: praisonaiagents, praisonai. TypeScript SDK: src/praisonai-ts/. Do NOT modify src/praisonai-rust. Read ALL comments above from Gemini, Qodo, CodeRabbit, and Copilot carefully before responding. Phase 1: Review per AGENTS.md
Phase 2: FIX Valid Issues Phase 3: Final Verdict |
|
Claude finished @MervinPraison's task ββ View job Final Architecture Review β PR #4505 β
Phase 1 β Architecture (per AGENTS.md)
Phase 2 β Valid issue fixedGreptile P1 β Shared cache return race ( Fix: build the client into a if (cachedClient !== null && cachedIdentity === identity) {
return cachedClient;
}
const client = new OpenAI(buildOpenAIClientOptions({ apiKey, ...(baseURL ? { baseURL } : {}) }));
cachedClient = client;
cachedIdentity = identity;
await Logger.debug('OpenAI client initialized');
return client;Added 2 regression tests: overlapping calls with different keys return distinct clients; Files modified
Skipped: nothing outstanding. ( VerdictApprove. The race flagged by Greptile is resolved and covered by tests; the change is minimal, backward-compatible, and secure (secret never logged). Note: I cannot submit a formal GitHub approval for security reasons β this comment is my review. |
Return the client captured in a local before the awaited Logger.debug so a concurrent credential change or resetOpenAIClient() during that yield cannot hand a caller the wrong client or null. Adds two race regression tests. Co-authored-by: Mervin Praison <MervinPraison@users.noreply.github.com>
Fixes #4488
Summary
getOpenAIClient()insrc/praisonai-ts/src/llm/openai.tscached a single module-level client keyed on null-vs-not-null, so a changedOPENAI_API_KEY(or base URL) was ignored for the process lifetime β every subsequent request kept using the stale client. This is the fallback env-only path used by the module-level convenience functions and anyAgentconstructed without explicit credentials.Changes
OPENAI_API_KEY+OPENAI_BASE_URL) instead of existence, rebuilding when either changes.resetOpenAIClient()to force a rebuild (tests / settings screens).Tests
New
tests/unit/llm/openai-client-cache.test.tscovers:resetOpenAIClient()forces a rebuildnpm run buildand the OpenAI unit suites pass (17/17).Scope is confined to
src/praisonai-ts/src/llm/openai.ts(+ its test), disjoint from #4487.Generated with Claude Code