|
| 1 | +// Regression coverage for the locally patched OpenAI delta schema (see |
| 2 | +// vendor/intx-inference/PATCHES.md#providers-ts-openai-null-delta-tolerance). |
| 3 | +// vLLM serializers behind OpenAI-compatible relays emit `"role": null` on |
| 4 | +// streamed deltas and `"tool_calls": null` on non-tool chunks; the patch |
| 5 | +// tolerates them (null ≡ absent) while wrong types stay rejected. |
| 6 | +import { describe, expect, test } from "bun:test"; |
| 7 | +import { ProtocolMismatchError } from "@intx/inference"; |
| 8 | +import { createOpenAIAdapter } from "@intx/inference/providers"; |
| 9 | +import type { LastCycleSource } from "@intx/types/runtime"; |
| 10 | +import { createOpenAICompatibleAdapter } from "./openai-compatible-adapter.js"; |
| 11 | +import { defined } from "../../testkit/defined.js"; |
| 12 | + |
| 13 | +const source: LastCycleSource = { |
| 14 | + sourceId: "null-delta-test", |
| 15 | + provider: "openai-compatible", |
| 16 | + model: "null-delta-fixture", |
| 17 | +}; |
| 18 | + |
| 19 | +const builtin = createOpenAIAdapter(source); |
| 20 | + |
| 21 | +describe("chat completions null delta tolerance", () => { |
| 22 | + test("(a) null role is equivalent to an absent role", () => { |
| 23 | + const withNull = builtin.parseResponse( |
| 24 | + JSON.stringify({ |
| 25 | + choices: [{ index: 0, delta: { role: null, content: "hi" } }], |
| 26 | + }), |
| 27 | + ); |
| 28 | + const absent = builtin.parseResponse( |
| 29 | + JSON.stringify({ choices: [{ index: 0, delta: { content: "hi" } }] }), |
| 30 | + ); |
| 31 | + expect(withNull).toEqual(absent); |
| 32 | + const textDelta = withNull.find( |
| 33 | + (event) => event.type === "inference.text.delta", |
| 34 | + ); |
| 35 | + expect( |
| 36 | + (textDelta as unknown as { data: Record<string, unknown> } | undefined) |
| 37 | + ?.data, |
| 38 | + ).toEqual({ |
| 39 | + token: "hi", |
| 40 | + partial: { text: "" }, |
| 41 | + index: 0, |
| 42 | + }); |
| 43 | + }); |
| 44 | + |
| 45 | + test("(b) tool_calls null and empty array are equivalent to absent", () => { |
| 46 | + const frames = [{ tool_calls: null }, { tool_calls: [] }, {}] as const; |
| 47 | + const shapes = frames.map((delta) => |
| 48 | + builtin.parseResponse(JSON.stringify({ choices: [{ index: 0, delta }] })), |
| 49 | + ); |
| 50 | + expect(shapes[0]).toEqual(shapes[2]); |
| 51 | + expect(shapes[1]).toEqual(shapes[2]); |
| 52 | + expect(shapes[2]).toEqual([]); |
| 53 | + }); |
| 54 | + |
| 55 | + test("(c) wrong-type role still throws ProtocolMismatchError", () => { |
| 56 | + expect(() => |
| 57 | + builtin.parseResponse( |
| 58 | + JSON.stringify({ choices: [{ index: 0, delta: { role: 42 } }] }), |
| 59 | + ), |
| 60 | + ).toThrow(ProtocolMismatchError); |
| 61 | + }); |
| 62 | + |
| 63 | + test("(c) wrong-type tool_calls still throws ProtocolMismatchError", () => { |
| 64 | + for (const tool_calls of [42, "nope"]) { |
| 65 | + expect(() => |
| 66 | + builtin.parseResponse( |
| 67 | + JSON.stringify({ choices: [{ index: 0, delta: { tool_calls } }] }), |
| 68 | + ), |
| 69 | + ).toThrow(ProtocolMismatchError); |
| 70 | + } |
| 71 | + }); |
| 72 | + |
| 73 | + test("(d) incident-shape first chunk with null role and tool_calls parses", () => { |
| 74 | + const events = builtin |
| 75 | + .parseResponse( |
| 76 | + JSON.stringify({ |
| 77 | + choices: [ |
| 78 | + { |
| 79 | + index: 0, |
| 80 | + delta: { role: null, tool_calls: null, content: "hello" }, |
| 81 | + }, |
| 82 | + ], |
| 83 | + }), |
| 84 | + ) |
| 85 | + .concat( |
| 86 | + builtin.parseResponse( |
| 87 | + JSON.stringify({ |
| 88 | + choices: [ |
| 89 | + { |
| 90 | + index: 0, |
| 91 | + delta: { role: null, tool_calls: null, content: " world" }, |
| 92 | + }, |
| 93 | + ], |
| 94 | + }), |
| 95 | + ), |
| 96 | + ); |
| 97 | + const textDeltas = events.filter( |
| 98 | + (event) => event.type === "inference.text.delta", |
| 99 | + ); |
| 100 | + expect( |
| 101 | + textDeltas.map((event) => { |
| 102 | + const { token, index, partial } = event.data as unknown as { |
| 103 | + token: string; |
| 104 | + index: number; |
| 105 | + partial: { text: string }; |
| 106 | + }; |
| 107 | + return { token, index, partial }; |
| 108 | + }), |
| 109 | + ).toEqual([ |
| 110 | + { token: "hello", partial: { text: "" }, index: 0 }, |
| 111 | + { token: " world", partial: { text: "" }, index: 0 }, |
| 112 | + ]); |
| 113 | + }); |
| 114 | + |
| 115 | + test("(e1) non-streaming null role is equivalent to absent role", () => { |
| 116 | + const wireUsage = { |
| 117 | + prompt_tokens: 10, |
| 118 | + completion_tokens: 5, |
| 119 | + }; |
| 120 | + const withNull = defined(builtin.parseJSONResponse)( |
| 121 | + JSON.stringify({ |
| 122 | + object: "chat.completion", |
| 123 | + choices: [{ message: { role: null, content: "done" } }], |
| 124 | + usage: wireUsage, |
| 125 | + }), |
| 126 | + ); |
| 127 | + const absent = defined(builtin.parseJSONResponse)( |
| 128 | + JSON.stringify({ |
| 129 | + object: "chat.completion", |
| 130 | + choices: [{ message: { content: "done" } }], |
| 131 | + usage: wireUsage, |
| 132 | + }), |
| 133 | + ); |
| 134 | + expect(withNull).toEqual(absent); |
| 135 | + expect( |
| 136 | + withNull.some((event) => event.type === "inference.text.delta"), |
| 137 | + ).toBe(true); |
| 138 | + expect(withNull.some((event) => event.type === "inference.usage")).toBe( |
| 139 | + true, |
| 140 | + ); |
| 141 | + }); |
| 142 | + |
| 143 | + test("(e2) non-streaming null tool_calls is equivalent to absent tool_calls", () => { |
| 144 | + const wireUsage = { |
| 145 | + prompt_tokens: 10, |
| 146 | + completion_tokens: 5, |
| 147 | + }; |
| 148 | + const withNull = defined(builtin.parseJSONResponse)( |
| 149 | + JSON.stringify({ |
| 150 | + object: "chat.completion", |
| 151 | + choices: [ |
| 152 | + { message: { role: null, tool_calls: null, content: "done" } }, |
| 153 | + ], |
| 154 | + usage: wireUsage, |
| 155 | + }), |
| 156 | + ); |
| 157 | + const absent = defined(builtin.parseJSONResponse)( |
| 158 | + JSON.stringify({ |
| 159 | + object: "chat.completion", |
| 160 | + choices: [{ message: { content: "done" } }], |
| 161 | + usage: wireUsage, |
| 162 | + }), |
| 163 | + ); |
| 164 | + expect(withNull).toEqual(absent); |
| 165 | + }); |
| 166 | + |
| 167 | + test("(e3) non-streaming wrong-type role still throws ProtocolMismatchError", () => { |
| 168 | + expect(() => |
| 169 | + defined(builtin.parseJSONResponse)( |
| 170 | + JSON.stringify({ |
| 171 | + object: "chat.completion", |
| 172 | + choices: [{ message: { role: 42, content: "done" } }], |
| 173 | + usage: { prompt_tokens: 10, completion_tokens: 5 }, |
| 174 | + }), |
| 175 | + ), |
| 176 | + ).toThrow(ProtocolMismatchError); |
| 177 | + }); |
| 178 | +}); |
| 179 | + |
| 180 | +describe("openai-compatible adapter null delta wiring parity", () => { |
| 181 | + const wrapperSource = { |
| 182 | + id: "test", |
| 183 | + provider: "openai-compatible", |
| 184 | + baseURL: "https://example.test", |
| 185 | + apiKey: "sk-test", |
| 186 | + model: "gpt-5.1", |
| 187 | + } as unknown as Parameters<typeof createOpenAICompatibleAdapter>[0]; |
| 188 | + |
| 189 | + test("(a) null role is equivalent to an absent role", () => { |
| 190 | + const adapter = createOpenAICompatibleAdapter(wrapperSource); |
| 191 | + expect( |
| 192 | + adapter.parseResponse( |
| 193 | + JSON.stringify({ |
| 194 | + choices: [{ index: 0, delta: { role: null, content: "hi" } }], |
| 195 | + }), |
| 196 | + ), |
| 197 | + ).toEqual( |
| 198 | + adapter.parseResponse( |
| 199 | + JSON.stringify({ choices: [{ index: 0, delta: { content: "hi" } }] }), |
| 200 | + ), |
| 201 | + ); |
| 202 | + }); |
| 203 | + |
| 204 | + test("(b) tool_calls null is equivalent to absent", () => { |
| 205 | + const adapter = createOpenAICompatibleAdapter(wrapperSource); |
| 206 | + expect( |
| 207 | + adapter.parseResponse( |
| 208 | + JSON.stringify({ |
| 209 | + choices: [{ index: 0, delta: { tool_calls: null } }], |
| 210 | + }), |
| 211 | + ), |
| 212 | + ).toEqual( |
| 213 | + adapter.parseResponse( |
| 214 | + JSON.stringify({ choices: [{ index: 0, delta: {} }] }), |
| 215 | + ), |
| 216 | + ); |
| 217 | + }); |
| 218 | + |
| 219 | + test("(c) wrong-type role still throws ProtocolMismatchError", () => { |
| 220 | + const adapter = createOpenAICompatibleAdapter(wrapperSource); |
| 221 | + expect(() => |
| 222 | + adapter.parseResponse( |
| 223 | + JSON.stringify({ choices: [{ index: 0, delta: { role: 42 } }] }), |
| 224 | + ), |
| 225 | + ).toThrow(ProtocolMismatchError); |
| 226 | + }); |
| 227 | +}); |
0 commit comments