Skip to content

fix(resume): defer OpenAI client construction#363

Open
Zekbot001 wants to merge 2 commits into
profullstack:masterfrom
Zekbot001:money/ugig-resume-parser-lazy-openai
Open

fix(resume): defer OpenAI client construction#363
Zekbot001 wants to merge 2 commits into
profullstack:masterfrom
Zekbot001:money/ugig-resume-parser-lazy-openai

Conversation

@Zekbot001
Copy link
Copy Markdown

Summary

  • defer OpenAI client construction until resume parsing is actually requested
  • keep module imports and Next page-data collection working when OPENAI_API_KEY is absent
  • return a clear configuration error only when the resume parser needs the missing key
  • add a regression test that imports the parser without an API key

Paid task: https://ugig.net/gigs/abd6b2a0-e728-48cf-a46f-f99e419ed94e

Testing

  • corepack pnpm exec vitest run src/lib/resume-parser.test.ts
  • corepack pnpm exec eslint src/lib/resume-parser.ts src/lib/resume-parser.test.ts
  • git diff --check
  • corepack pnpm build (now passes /api/profile/import page-data collection and proceeds into static generation; later blocked by pre-existing missing Supabase service-role configuration while prerendering /blog)

@greptile-apps
Copy link
Copy Markdown

greptile-apps Bot commented May 31, 2026

Greptile Summary

This PR fixes a module-load failure caused by constructing the OpenAI client eagerly at the top level, which threw when OPENAI_API_KEY was absent during Next.js page-data collection. The fix wraps client construction in a getOpenAIClient() factory that is called only inside parseWithOpenAI, and adds a regression test to lock in that behaviour.

  • resume-parser.ts: removes the module-level new OpenAI(...) call and replaces it with a getOpenAIClient() factory that validates the API key and throws a clear Error only at parse time.
  • resume-parser.test.ts: adds a single regression test that imports the module with OPENAI_API_KEY="" and asserts that the import resolves with parseResumeFile exported.

Confidence Score: 5/5

Safe to merge — the change is narrow and surgical, touching only how the OpenAI client is instantiated, with no impact on the parsing logic itself.

The refactor is straightforward: one factory function replaces a module-level constructor call, and a regression test covers the targeted scenario. The only minor concern is that the factory call sits inside the existing try/catch, so a missing-key error gets logged under the 'OpenAI parsing error' label before being re-thrown — confusing in logs but not functionally broken.

No files require special attention.

Important Files Changed

Filename Overview
src/lib/resume-parser.ts Replaces the module-level new OpenAI(...) with a getOpenAIClient() factory called at parse time, so the module loads safely without an API key; minor issue with the factory call sitting inside the catch-all try block.
src/lib/resume-parser.test.ts New regression test confirms the module can be imported with an empty OPENAI_API_KEY; correctly uses dynamic import + vi.resetModules() in afterEach to isolate the module cache.

Sequence Diagram

sequenceDiagram
    participant Caller
    participant parseResumeFile
    participant parseWithOpenAI
    participant getOpenAIClient
    participant OpenAI API

    Caller->>parseResumeFile: parseResumeFile(buffer, mimeType)
    parseResumeFile->>parseWithOpenAI: parseWithOpenAI(text)
    parseWithOpenAI->>getOpenAIClient: getOpenAIClient()
    alt OPENAI_API_KEY missing
        getOpenAIClient-->>parseWithOpenAI: throw Error("OPENAI_API_KEY is not configured")
        parseWithOpenAI-->>Caller: throw (propagates)
    else key present
        getOpenAIClient-->>parseWithOpenAI: OpenAI client instance
        parseWithOpenAI->>OpenAI API: chat.completions.create(...)
        OpenAI API-->>parseWithOpenAI: JSON response
        parseWithOpenAI-->>parseResumeFile: ParsedResumeProfile
        parseResumeFile-->>Caller: ParsedResumeProfile + _debug
    end
Loading

Reviews (2): Last reviewed commit: "fix(resume): preserve prompt whitespace" | Re-trigger Greptile

Comment thread src/lib/resume-parser.ts Outdated
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.

1 participant