Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

🐛 fix(model-runtime): keep the provider body in a failed generateObject trace (#19756)

Arvin Xu578764fda4fc
diff --git a/packages/model-runtime/src/core/ModelRuntime.test.ts b/packages/model-runtime/src/core/ModelRuntime.test.ts
index affaa06f43a..0fcfef997f4 100644
--- a/packages/model-runtime/src/core/ModelRuntime.test.ts
+++ b/packages/model-runtime/src/core/ModelRuntime.test.ts
@@ -870,6 +870,28 @@ describe('ModelRuntime', () => {
expect(data.error?.message).toBe('invalid key');
});
+ it('onGenerateObjectComplete keeps the provider body when the payload has no message', async () => {
+ const onGenerateObjectComplete = vi.fn();
+ const { runtime, mockRuntimeAI } = createMockRuntime({ onGenerateObjectComplete });
+ // Production shape of a refined upstream rejection: the body is the only description,
+ // and it is the part that names which field the provider refused.
+ const cause = {
+ endpoint: 'https://api.example.com',
+ error: {
+ error: { code: 'invalid_value', param: 'input[1].content[0].type' },
+ status: 400,
+ },
+ errorType: 'UpstreamHttpError',
+ provider: 'azure',
+ };
+ mockRuntimeAI.generateObject.mockRejectedValue(cause);
+
+ await expect(runtime.generateObject(genObjPayload)).rejects.toBe(cause);
+ const [data] = onGenerateObjectComplete.mock.calls[0];
+ expect(data.error?.code).toBe('UpstreamHttpError');
+ expect(data.error?.message).toContain('input[1].content[0].type');
+ });
+
it('onGenerateObjectComplete falls back to error.name for AI SDK errors', async () => {
const onGenerateObjectComplete = vi.fn();
const { runtime, mockRuntimeAI } = createMockRuntime({ onGenerateObjectComplete });
diff --git a/packages/model-runtime/src/core/ModelRuntime.ts b/packages/model-runtime/src/core/ModelRuntime.ts
index 593496e5f10..905d2a6f2a9 100644
--- a/packages/model-runtime/src/core/ModelRuntime.ts
+++ b/packages/model-runtime/src/core/ModelRuntime.ts
@@ -40,6 +40,35 @@ import type { LobeRuntimeAI } from './BaseAI';
const { logger: timing } = createTimingHelpers('lobe-server:chat:lobehub:timing');
+/** Keeps one provider body out of the tracing row's way while staying diagnosable. */
+const MAX_TRACED_ERROR_DETAIL = 4000;
+
+/**
+ * Describes a failed generation for the tracing row.
+ *
+ * A provider either rethrows its own error (`.message` carries the body) or throws the
+ * normalized `ChatCompletionErrorPayload`, which has no `.message` at all — the body sits under
+ * `.error`. Reading only `.message` therefore drops exactly the cases the error refinement
+ * normalized, leaving a tracing row that names a bucket (`UpstreamHttpError`) and nothing else.
+ */
+export const describeGenerateObjectError = (error: {
+ error?: unknown;
+ message?: string;
+}): string | undefined => {
+ if (typeof error?.message === 'string' && error.message.length > 0) return error.message;
+
+ const body = error?.error;
+ if (body === undefined || body === null) return undefined;
+ if (typeof body === 'string') return body.slice(0, MAX_TRACED_ERROR_DETAIL) || undefined;
+
+ try {
+ return JSON.stringify(body).slice(0, MAX_TRACED_ERROR_DETAIL);
+ } catch {
+ // Circular or otherwise unserializable payloads still beat recording nothing.
+ return String(body).slice(0, MAX_TRACED_ERROR_DETAIL);
+ }
+};
+
const getLobeHubTimingMetadata = (options?: {
metadata?: Record<string, unknown>;
}): Record<string, unknown> | undefined =>
@@ -430,10 +459,10 @@ export class ModelRuntime {
// `AI_*Error` subclasses, Node Errors with `.code`, etc. Try the most
// descriptive identifier first so the tracing row gets a usable code
// instead of falling through to `unknown`.
- const err = error as Error & { code?: string; errorType?: string };
+ const err = error as Error & { code?: string; error?: unknown; errorType?: string };
const code = err?.errorType ?? err?.code ?? err?.name ?? err?.constructor?.name;
await fireComplete({
- error: { code, message: err?.message, stack: err?.stack },
+ error: { code, message: describeGenerateObjectError(err), stack: err?.stack },
success: false,
});
throw error;