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): stop provider diagnostics from crashing on a malformed choices field (#19007)

Arvin Xuc3a69d528a35
diff --git a/packages/model-runtime/src/core/openaiCompatibleFactory/providerDiagnostics.test.ts b/packages/model-runtime/src/core/openaiCompatibleFactory/providerDiagnostics.test.ts
new file mode 100644
index 00000000000..ce5c82250da
--- /dev/null
+++ b/packages/model-runtime/src/core/openaiCompatibleFactory/providerDiagnostics.test.ts
@@ -0,0 +1,56 @@
+import { describe, expect, it } from 'vitest';
+
+import {
+ initializeOpenAIDiagnostics,
+ recordOpenAIChatCompletionChunk,
+ recordOpenAIChatCompletionResponse,
+} from './providerDiagnostics';
+
+const createDiagnostics = () =>
+ initializeOpenAIDiagnostics({
+ apiMode: 'chatCompletion',
+ diagnostics: {} as any,
+ endpoint: 'https://example.test/v1/chat/completions',
+ payload: {},
+ sentAt: Date.now(),
+ })!;
+
+describe('providerDiagnostics choices guard', () => {
+ // OpenAI-compatible proxies send keep-alive / usage-only / error frames whose
+ // `choices` is absent or null. Diagnostics is observability β€” it must never be
+ // what fails the request.
+ const malformed = [
+ ['absent', {}],
+ ['null', { choices: null }],
+ ['an object', { choices: { 0: {} } }],
+ ['a string', { choices: '' }],
+ ] as const;
+
+ it.each(malformed)('survives a stream chunk whose choices is %s', (_label, shape) => {
+ const diagnostics = createDiagnostics();
+
+ expect(() =>
+ recordOpenAIChatCompletionChunk(diagnostics, { id: 'c1', ...shape } as any),
+ ).not.toThrow();
+ });
+
+ it.each(malformed)('survives a completion whose choices is %s', async (_label, shape) => {
+ const diagnostics = createDiagnostics();
+
+ await expect(
+ recordOpenAIChatCompletionResponse(diagnostics, { id: 'c1', ...shape } as any),
+ ).resolves.not.toThrow();
+ });
+
+ it('still records content from a well-formed chunk', () => {
+ const diagnostics = createDiagnostics();
+
+ recordOpenAIChatCompletionChunk(diagnostics, {
+ choices: [{ delta: { content: 'hello' }, index: 0 }],
+ id: 'c1',
+ } as any);
+
+ expect(diagnostics.textChars).toBe(5);
+ expect(diagnostics.hasNonWhitespaceText).toBe(true);
+ });
+});
diff --git a/packages/model-runtime/src/core/openaiCompatibleFactory/providerDiagnostics.ts b/packages/model-runtime/src/core/openaiCompatibleFactory/providerDiagnostics.ts
index 882c7d95bf2..1ff3a186860 100644
--- a/packages/model-runtime/src/core/openaiCompatibleFactory/providerDiagnostics.ts
+++ b/packages/model-runtime/src/core/openaiCompatibleFactory/providerDiagnostics.ts
@@ -206,7 +206,11 @@ export const recordOpenAIChatCompletionChunk = (
type: chunk.object || 'chat.completion.chunk',
};
- for (const choice of chunk.choices) {
+ // OpenAI-compatible upstreams routinely omit `choices` or send `null` on
+ // keep-alive / usage-only / error frames, even though the SDK type says
+ // otherwise. This is diagnostics β€” it must never be the thing that fails the
+ // request. (`tool_calls` below is already guarded the same way.)
+ for (const choice of Array.isArray(chunk.choices) ? chunk.choices : []) {
const delta = choice.delta as typeof choice.delta & {
reasoning?: string;
reasoning_content?: string;
@@ -387,7 +391,7 @@ export const recordOpenAIChatCompletionResponse = async (
const event: Omit<ProviderResponseEventDiagnostics, 'index'> = {
type: completion.object,
};
- for (const choice of completion.choices) {
+ for (const choice of Array.isArray(completion.choices) ? completion.choices : []) {
const message = choice.message as typeof choice.message & {
reasoning?: string;
reasoning_content?: string;