Skip to content

Job application tracker and AI-powered job search assistant. Helps job seekers manage their search journey with AI resume review, job matching, task logging, and application analytics, all while keeping your data private.

fix: OpenAI models return schema error Issue #99, enhance error handling in resume import

Khuram Niaz704e1aa1a5bb
diff --git a/__tests__/resumeImportStream.spec.ts b/__tests__/resumeImportStream.spec.ts
index 8a5dfc3..296b8ed 100644
--- a/__tests__/resumeImportStream.spec.ts
+++ b/__tests__/resumeImportStream.spec.ts
@@ -98,6 +98,37 @@ describe("streamResumeImport", () => {
expect(data.experience[0].company).toBe("Acme");
});
+ it("throws the provider message carried on the error line", async () => {
+ const message =
+ "Invalid parameter: 'text.format' of type 'json_schema' is not supported with model version `gpt-3.5-turbo`.";
+ vi.stubGlobal(
+ "fetch",
+ vi.fn().mockResolvedValue(
+ streamingResponse([JSON.stringify({ __error: message }) + "\n"]),
+ ),
+ );
+
+ await expect(
+ streamResumeImport({ resumeId: "r1", selectedModel }),
+ ).rejects.toThrow(message);
+ });
+
+ it("prefers salvaged data over a trailing error line", async () => {
+ vi.stubGlobal(
+ "fetch",
+ vi.fn().mockResolvedValue(
+ streamingResponse([
+ '{"summary":"Partial."}\n',
+ '{"__error":"stream died"}\n',
+ ]),
+ ),
+ );
+
+ const { data } = await streamResumeImport({ resumeId: "r1", selectedModel });
+
+ expect(data.summary).toBe("Partial.");
+ });
+
it("throws the server error message on a non-ok response", async () => {
vi.stubGlobal(
"fetch",
diff --git a/src/app/api/ai/resume/import/route.ts b/src/app/api/ai/resume/import/route.ts
index 45b1c73..71cdb29 100644
--- a/src/app/api/ai/resume/import/route.ts
+++ b/src/app/api/ai/resume/import/route.ts
@@ -126,6 +126,10 @@ export const POST = async (req: NextRequest) => {
// background until it finishes or hits the timeout.
req.signal.addEventListener("abort", () => controller.abort());
+ // Only onError carries the provider's message — the error thrown from
+ // partialOutputStream is a generic "No output generated".
+ let streamErrorMessage: string | undefined;
+
const result = streamText({
model,
output: Output.object({ schema: ResumeImportSchema }),
@@ -135,6 +139,10 @@ export const POST = async (req: NextRequest) => {
abortSignal: controller.signal,
providerOptions: {
ollama: { options: { num_ctx: APP_CONSTANTS.AI_OLLAMA_NUM_CTX } },
+ // OpenAI (and OpenRouter, same factory) default to strict json_schema,
+ // which rejects any object whose `required` omits a key — this schema
+ // is optional-by-design throughout. Non-strict passes it as guidance.
+ openai: { strictJsonSchema: false },
},
onFinish: () => {
clearTimeout(timer);
@@ -142,6 +150,7 @@ export const POST = async (req: NextRequest) => {
onError: ({ error }) => {
clearTimeout(timer);
console.error("Resume import stream error:", error);
+ streamErrorMessage = error instanceof Error ? error.message : undefined;
},
});
@@ -162,6 +171,15 @@ export const POST = async (req: NextRequest) => {
// last complete snapshot. onError already logged it.
console.error("Resume import stream interrupted:", err);
} finally {
+ // The 200 and headers are committed before the provider is called,
+ // so a failure can only reach the client in-band as a final line.
+ if (streamErrorMessage) {
+ controller.enqueue(
+ encoder.encode(
+ JSON.stringify({ __error: streamErrorMessage }) + "\n",
+ ),
+ );
+ }
controller.close();
}
},
diff --git a/src/utils/resumeImportStream.utils.ts b/src/utils/resumeImportStream.utils.ts
index 862d30c..ea90601 100644
--- a/src/utils/resumeImportStream.utils.ts
+++ b/src/utils/resumeImportStream.utils.ts
@@ -53,6 +53,7 @@ export async function streamResumeImport({
const decoder = new TextDecoder();
let buffer = "";
let latest: DeepPartial<ResumeImportData> | undefined;
+ let streamError: string | undefined;
const handleLine = (line: string) => {
const trimmed = line.trim();
@@ -60,6 +61,12 @@ export async function streamResumeImport({
try {
const obj = JSON.parse(trimmed);
if (obj && typeof obj === "object") {
+ // Sentinel line: the provider's message, which can only travel in-band
+ // because the route has already committed a 200.
+ if (typeof obj.__error === "string") {
+ streamError = obj.__error;
+ return;
+ }
latest = obj as DeepPartial<ResumeImportData>;
onPartial?.(latest);
}
@@ -87,7 +94,8 @@ export async function streamResumeImport({
if (!latest || typeof latest !== "object") {
throw new Error(
- "The AI service returned no data. Please ensure it is running and try again.",
+ streamError ??
+ "The AI service returned no data. Please ensure it is running and try again.",
);
}