Skip to content

Build your personal knowledge base with Trilium Notes

feat(llm): display actual error message instead of a generic one

Elian Doranfb58b96e2a14
diff --git a/apps/server/src/routes/api/llm_chat.ts b/apps/server/src/routes/api/llm_chat.ts
index dd5bf149c8..1564dd2ad2 100644
--- a/apps/server/src/routes/api/llm_chat.ts
+++ b/apps/server/src/routes/api/llm_chat.ts
@@ -63,6 +63,9 @@ async function streamChat(req: Request, res: Response) {
const pricing = provider.getModelPricing(modelId);
const modelDisplayName = provider.getAvailableModels().find(m => m.id === modelId)?.name || modelId;
for await (const chunk of streamToChunks(result, { model: modelDisplayName, pricing })) {
+ if (chunk.type === "error") {
+ log.error(`LLM chat stream error (model ${modelDisplayName}): ${chunk.error}`);
+ }
res.write(`data: ${JSON.stringify(chunk)}\n\n`);
// Flush immediately to ensure real-time streaming
if (typeof flushableRes.flush === "function") {
@@ -81,6 +84,7 @@ async function streamChat(req: Request, res: Response) {
}
} catch (error) {
const errorMessage = error instanceof Error ? error.message : "Unknown error";
+ log.error(`LLM chat stream failed: ${safeExtractMessageAndStackFromError(error)}`);
res.write(`data: ${JSON.stringify({ type: "error", error: errorMessage })}\n\n`);
} finally {
res.end();
diff --git a/apps/server/src/services/llm/stream.ts b/apps/server/src/services/llm/stream.ts
index 91a0056b67..871f34ab5a 100644
--- a/apps/server/src/services/llm/stream.ts
+++ b/apps/server/src/services/llm/stream.ts
@@ -49,6 +49,7 @@ export interface StreamOptions {
* This is provider-agnostic - works with any AI SDK provider.
*/
export async function* streamToChunks(result: StreamResult, options: StreamOptions = {}): AsyncIterable<LlmStreamChunk> {
+ let errorEmitted = false;
try {
for await (const part of result.fullStream) {
switch (part.type) {
@@ -96,13 +97,27 @@ export async function* streamToChunks(result: StreamResult, options: StreamOptio
break;
case "error":
+ errorEmitted = true;
yield { type: "error", error: String(part.error) };
break;
}
}
- // Get usage information after stream completes
- const usage = await result.usage;
+ // Get usage information after the stream completes. When the stream produced
+ // no steps, the AI SDK rejects `usage` with a generic NoOutputGeneratedError
+ // ("No output generated. Check the stream for errors."). If a real error was
+ // already emitted above, that earlier error is the actual cause — don't mask
+ // it with the generic one.
+ let usage: LanguageModelUsage;
+ try {
+ usage = await result.usage;
+ } catch (error) {
+ if (!errorEmitted) {
+ yield { type: "error", error: error instanceof Error ? error.message : "Unknown error" };
+ }
+ return;
+ }
+
if (usage && typeof usage.inputTokens === "number" && typeof usage.outputTokens === "number") {
const cost = calculateCost(usage, options.pricing);
yield {
@@ -119,7 +134,9 @@ export async function* streamToChunks(result: StreamResult, options: StreamOptio
yield { type: "done" };
} catch (error) {
- const message = error instanceof Error ? error.message : "Unknown error";
- yield { type: "error", error: message };
+ if (!errorEmitted) {
+ const message = error instanceof Error ? error.message : "Unknown error";
+ yield { type: "error", error: message };
+ }
}
}