Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions server/src/llm/structuredInvoke.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,17 @@ function tryFixTruncatedJson(raw: string): string {
return fixed;
}

function unwrapSingletonArray(val: unknown): unknown {
if (Array.isArray(val) && val.length === 1) {
return val[0];
}
return val;
}

function tryParseStructuredJsonValue(source: string): { parsed: unknown } | { error: string } {
try {
return {
parsed: JSON.parse(extractJSONValue(source)) as unknown,
parsed: unwrapSingletonArray(JSON.parse(extractJSONValue(source))),
};
} catch (error) {
const fixed = tryFixTruncatedJson(source);
Expand All @@ -126,7 +133,7 @@ function tryParseStructuredJsonValue(source: string): { parsed: unknown } | { er

try {
return {
parsed: JSON.parse(extractJSONValue(fixed)) as unknown,
parsed: unwrapSingletonArray(JSON.parse(extractJSONValue(fixed))),
};
} catch (fixedError) {
return {
Expand Down
18 changes: 18 additions & 0 deletions server/src/llm/structuredOutput.ts
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,24 @@ export function classifyStructuredOutputFailure(input: {
if (haystack.includes("zod") || haystack.includes("schema") || haystack.includes("校验错误")) {
return "schema_mismatch";
}
// Relay/proxy returned HTML instead of JSON (e.g. rate limit page, auth error page)
const lowerHaystack = haystack.toLowerCase();
if (
lowerHaystack.includes("<!doctype") ||
lowerHaystack.includes("<html") ||
lowerHaystack.includes("<body") ||
lowerHaystack.includes("</div>") ||
lowerHaystack.includes("</script>") ||
lowerHaystack.includes("rate limit") ||
lowerHaystack.includes("429") ||
lowerHaystack.includes("<title>error") ||
lowerHaystack.includes("<title>403") ||
lowerHaystack.includes("<title>404") ||
lowerHaystack.includes("<title>502") ||
lowerHaystack.includes("<title>503")
) {
return "transport_error";
}
return "transport_error";
}

Expand Down