From 37c07e468d6d6dce495983c6a498745825ecb5fe Mon Sep 17 00:00:00 2001 From: Christopher Mayor Date: Fri, 24 Apr 2026 14:37:28 -0700 Subject: [PATCH] fix: lazy-init OpenAI client to avoid build failure without API key --- src/lib/llm/providers/openai.ts | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/lib/llm/providers/openai.ts b/src/lib/llm/providers/openai.ts index f3116af..dd10e4e 100644 --- a/src/lib/llm/providers/openai.ts +++ b/src/lib/llm/providers/openai.ts @@ -6,9 +6,14 @@ import type { ItemResearch, } from "../types"; -const client = new OpenAI({ - apiKey: process.env.OPENAI_API_KEY, -}); +let _client: OpenAI | null = null; + +function getClient(): OpenAI { + if (!_client) { + _client = new OpenAI({ apiKey: process.env.OPENAI_API_KEY }); + } + return _client; +} const SYSTEM_PROMPT = `You are an expert research analyst. Your job is to compare items across multiple dimensions and produce structured, insightful comparison data. @@ -105,7 +110,7 @@ Provide a comprehensive comparison with scores, pros/cons, and a recommendation. for (let attempt = 1; attempt <= MAX_RETRIES; attempt++) { try { - const response = await client.chat.completions.create({ + const response = await getClient().chat.completions.create({ model: "gpt-4o-mini", messages: [ { role: "system", content: SYSTEM_PROMPT },