Skip to content

Thread-safe singleton for OpenAI client in Q&A agent#8

Draft
joestein with Copilot wants to merge 3 commits into
graph-qafrom
copilot/sub-pr-5-another-one
Draft

Thread-safe singleton for OpenAI client in Q&A agent#8
joestein with Copilot wants to merge 3 commits into
graph-qafrom
copilot/sub-pr-5-another-one

Conversation

Copilot AI commented Dec 2, 2025

Copy link
Copy Markdown

Addresses review feedback: the OpenAI client was being instantiated on every function call, causing potential resource exhaustion under load.

Changes

  • Add _get_openai_client() singleton helper with double-checked locking
  • Replace direct OpenAI() instantiation in classify_with_instructor() and _generate_question_text()
_openai_client: OpenAI | None = None
_openai_client_lock = threading.Lock()

def _get_openai_client() -> OpenAI:
    """Return a thread-safe singleton OpenAI client instance."""
    global _openai_client
    if _openai_client is None:
        with _openai_client_lock:
            if _openai_client is None:
                _openai_client = OpenAI(api_key=settings.OPENAI_API_KEY)
    return _openai_client

✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI and others added 2 commits December 2, 2025 00:33
Co-authored-by: joestein <115151+joestein@users.noreply.github.com>
Co-authored-by: joestein <115151+joestein@users.noreply.github.com>
Copilot AI changed the title [WIP] Address feedback on Q&A graph chat fixes Thread-safe singleton for OpenAI client in Q&A agent Dec 2, 2025
Copilot AI requested a review from joestein December 2, 2025 00:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants