fix(llm-parse): stop discarding an extraction Anthropic sent as a string

With the Anthropic provider, an import sometimes reported that no
reservations were found even though the document held one and the model had
extracted it. Nothing was logged, because from the code's point of view
nothing failed: the value simply was not an array, and the check that only
accepted arrays returned an empty list. What reached the user was
indistinguishable from a document with no booking in it.

The tool declares `reservations` as an array and the call forces the tool, so
it usually is one. Sometimes the model serialises the same content as a
JSON-encoded string instead — either a stringified array, or stringified and
wrapped in `{ reservations: ... }` again. Because that is a serialisation
choice made per call, the same file imported fine on one attempt and came
back empty on the next.

Normalising is now one function next to the lenient JSON parser, and the
OpenAI-compatible client uses it too — it had grown its own version of the
object-unwrap half, and two copies of this rule would drift.

It unwraps a string once and no further. A value that is still not a list
after that is not a list, and reaching deeper would start turning prose into
bookings.

Reported with the exact file and lines, and with both observed shapes.

Closes #1968
This commit is contained in:
Maurice
2026-08-20 17:29:00 +02:00
committed by Maurice
parent 9522e4ce4a
commit a5e468829c
5 changed files with 130 additions and 10 deletions
@@ -1,5 +1,6 @@
import type { LlmExtractionClient, LlmExtractionInput } from '../llm-provider.interface';
import { safeFetchLlm } from '../../../utils/ssrfGuard';
import { toReservationList } from '../lenient-json';
const TIMEOUT_MS = 120_000;
const MAX_TOKENS = 8192;
@@ -80,8 +81,7 @@ export class AnthropicClient implements LlmExtractionClient {
}
const toolUse = data.content?.find(b => b.type === 'tool_use' && b.name === TOOL_NAME);
const reservations = toolUse?.input?.reservations;
return Array.isArray(reservations) ? (reservations as Record<string, unknown>[]) : [];
return toReservationList(toolUse?.input?.reservations);
}
}
@@ -1,6 +1,6 @@
import type { LlmExtractionClient, LlmExtractionInput } from '../llm-provider.interface';
import { isNuExtractModel, buildNuExtractUserText, nuExtractToKiReservations } from './nuextract';
import { parseLenientJson } from '../lenient-json';
import { parseLenientJson, toReservationList } from '../lenient-json';
import { safeFetchLlm } from '../../../utils/ssrfGuard';
// Generous: a local CPU model (Ollama, no GPU) may cold-load several GB and then
@@ -136,10 +136,5 @@ const USER_TEXT = 'Extract every travel reservation from the following document
/** Tolerant parse: strip code fences, JSON(5).parse, pull `reservations`. `[]` on failure. */
function parseReservations(content: string | undefined | null): Record<string, unknown>[] {
const parsed = parseLenientJson(content);
if (Array.isArray(parsed)) return parsed as Record<string, unknown>[];
if (parsed && typeof parsed === 'object' && Array.isArray((parsed as { reservations?: unknown }).reservations)) {
return (parsed as { reservations: Record<string, unknown>[] }).reservations;
}
return [];
return toReservationList(parseLenientJson(content));
}
+36
View File
@@ -29,3 +29,39 @@ export function parseLenientJson(content: string | undefined | null): unknown {
}
}
}
/**
* Whatever a provider handed back, as a list of reservation nodes.
*
* The extraction tool declares `reservations` as an array and the call forces
* that tool, so the answer should already be one. It is not always. Anthropic
* sometimes serialises its tool input as a JSON-encoded string instead (#1968),
* in either of two shapes seen in the wild:
*
* "[{\"@type\":\"LodgingReservation\", …}]" a stringified array
* "{\"reservations\":[{…}]}" stringified and re-wrapped
*
* Whether it happens is up to how the model serialises that particular call, so
* the same document imported fine one minute and came back empty the next —
* indistinguishable, to the person waiting, from a document with no booking in
* it. Nothing was logged, because nothing had failed: the value simply was not
* an array, and the check that only accepted arrays dropped a good extraction.
*
* One unwrap of a string, not a loop: a value that is still not a list after
* that is genuinely not one, and guessing further would start inventing
* bookings out of prose.
*/
export function toReservationList(value: unknown): Record<string, unknown>[] {
const list = (v: unknown): Record<string, unknown>[] | null => {
if (Array.isArray(v)) return v as Record<string, unknown>[];
if (v && typeof v === 'object' && Array.isArray((v as { reservations?: unknown }).reservations)) {
return (v as { reservations: Record<string, unknown>[] }).reservations;
}
return null;
};
const direct = list(value);
if (direct) return direct;
if (typeof value === 'string') return list(parseLenientJson(value)) ?? [];
return [];
}