Skip to content
Merged
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
18 changes: 10 additions & 8 deletions packages/ai-sdk-provider/e2e/gateway-matrix.e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -366,25 +366,27 @@ describe("e2e — Neon AI Gateway capability matrix", () => {
}, 240_000);
});

describe("gpt-oss harmony normalization (#308)", () => {
// The gateway returns gpt-oss `message.content` as a harmony parts
// array; the provider normalizes it to the OpenAI Chat Completions
// contract. Verify text comes through for both generate and stream.
it("generateText works and does not throw on the harmony shape", async () => {
describe("array content normalization on the chat route", () => {
// The gateway returns gemini-4-argon `message.content` as an array of
// text parts on every non-streaming response; the provider normalizes
// it to the OpenAI Chat Completions contract. Its streamed deltas are
// strings, so the streamText case is a sanity check on the same model.
// gpt-oss, which this block covered first (#308), returns a string now.
it("generateText works and does not throw on the array shape", async () => {
const result = await withRateLimitRetry(() =>
generateText({
model: neon("gpt-oss-120b"),
model: neon("gemini-4-argon"),
prompt: "Reply with exactly three words.",
maxOutputTokens: 512,
}),
);
expect(result.text.trim().length).toBeGreaterThan(0);
}, 180_000);

it("streamText works without an invalid-JSON error flood", async () => {
it("streamText returns text", async () => {
const text = await withRateLimitRetry(async () => {
const result = streamText({
model: neon("gpt-oss-120b"),
model: neon("gemini-4-argon"),
prompt: "Reply with exactly three words.",
maxOutputTokens: 512,
});
Expand Down
40 changes: 21 additions & 19 deletions packages/ai-sdk-provider/src/lib/neon-harmony-normalize.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,29 @@
import type { FetchFunction } from "@ai-sdk/provider-utils";

// TODO(#308): Temporary workaround. Remove this whole module (and its wiring in
// provider.ts) once the Neon AI Gateway returns Chat Completions-compliant
// gpt-oss responses (string `content` + `reasoning_content`). The wrapper is a
// transparent pass-through for any already-compliant response, so it is safe to
// leave in place until the gateway fix ships — but it should be deleted then.
// Keep this module until the Neon AI Gateway returns a string `content` for
// every model on the unified Chat Completions route. gpt-oss, which it was
// written for (#308), is compliant now, but gemini-3-7-flash, gemini-3-8-flash
// and gemini-4-argon still return arrays there, and the AI SDK rejects them
// without this rewrite.

/**
* Normalize the Neon AI Gateway's non-OpenAI-compliant `gpt-oss` ("harmony")
* response shape into the OpenAI Chat Completions contract before it reaches
* the AI SDK's schema validation.
* Normalize array `content` from the Neon AI Gateway's unified Chat
* Completions route into the OpenAI Chat Completions contract before it
* reaches the AI SDK's schema validation.
*
* For `gpt-oss-*` the gateway returns `message.content` (and streaming
* `delta.content`) as an array of Responses-style parts:
* For some models the gateway returns `message.content` (and streaming
* `delta.content`) as an array of Responses-style parts. gpt-oss ("harmony")
* sent reasoning and text:
*
* "content": [
* { "type": "reasoning", "summary": [{ "type": "summary_text", "text": "…" }] },
* { "type": "text", "text": "…" }
* ]
*
* and Gemini models send text parts on non-streaming responses:
*
* "content": [{ "type": "text", "text": "…", "thoughtSignature": "…" }]
*
* The Chat Completions spec requires `content` to be a string, so any strict
* OpenAI-compatible client (incl. the Vercel AI SDK) rejects it. We flatten the
* `text` parts into the string `content` and hoist reasoning into
Expand All @@ -27,11 +32,8 @@ import type { FetchFunction } from "@ai-sdk/provider-utils";
*
* The transform only rewrites when `content` is an array, and the fetch wrapper
* returns the response untouched when nothing was rewritten. So it is a true
* no-op / pass-through for every spec-compliant model (Llama, Gemini, Qwen, …)
* and self-retires the moment the gateway starts returning a compliant shape —
* it will not double-transform or otherwise interfere with the downstream fix.
*
* See neondatabase/neon-pkgs#308.
* no-op / pass-through for every spec-compliant model and will not
* double-transform once the gateway returns a compliant shape.
*/

function isRecord(value: unknown): value is Record<string, unknown> {
Expand Down Expand Up @@ -233,13 +235,13 @@ function headersWithout(source: Headers, ...names: string[]): Headers {
}

/**
* Wrap a fetch implementation so gpt-oss harmony responses are normalized to
* the OpenAI Chat Completions shape before the caller parses them. Composes
* Wrap a fetch implementation so responses with array `content` (Gemini text
* parts, gpt-oss harmony parts) are normalized to the OpenAI Chat Completions
* shape before the caller parses them. Composes
* with a user-supplied `fetch` (falls back to the global `fetch`).
*
* When the response is already compliant, the original response is returned
* untouched — a transparent pass-through that will not interfere once the
* gateway is fixed.
* untouched.
*/
export function wrapFetchWithHarmonyNormalization(
baseFetch?: FetchFunction,
Expand Down
17 changes: 7 additions & 10 deletions packages/ai-sdk-provider/src/lib/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,17 +215,14 @@ export function createNeon(options: NeonProviderSettings = {}): NeonProvider {
url: ({ path }) => `${getHost()}/v1${path}`,
headers: getHeaders,
// Two wrappers, on disjoint halves of the response: the harmony one
// rewrites successful gpt-oss bodies and returns early on any error,
// the error one only touches non-2xx bodies. This route emits the
// same flat `{ error_code, message }` Databricks envelope as the
// Responses route (an out-of-range `temperature`, say), which
// `errorStructure` below cannot read on its own.
// rewrites successful bodies whose `content` is an array and returns
// early on any error, the error one only touches non-2xx bodies. This
// route emits the same flat `{ error_code, message }` Databricks
// envelope as the Responses route (an out-of-range `temperature`,
// say), which `errorStructure` below cannot read on its own.
//
// TODO(#308): Temporary workaround for gpt-oss. The gateway returns a
// non-compliant "harmony" content-array shape; this wrapper normalizes it
// to the Chat Completions contract and is a transparent pass-through for
// every compliant response. Remove it (and neon-harmony-normalize.ts) once
// the gateway returns spec-compliant gpt-oss responses.
// The gateway still returns array `content` for some Gemini models on
// this route, so the harmony wrapper stays until it no longer does.
fetch: wrapFetchWithHarmonyNormalization(
wrapFetchWithGatewayErrorNormalization(options.fetch, "openai"),
),
Expand Down
52 changes: 50 additions & 2 deletions packages/ai-sdk-provider/src/lib/sdk-compatibility.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,42 @@ const responseBody = {
},
};

function createTestProvider() {
// What the gateway returns for gemini-3-7-flash, gemini-3-8-flash and
// gemini-4-argon on a non-streaming /v1/chat/completions call.
const arrayContentBody = {
id: null,
object: "chat.completion",
created: 0,
model: "system.ai.gemini-4-argon",
choices: [
{
index: 0,
message: {
role: "assistant",
content: [
{
type: "text",
text: "pong",
thoughtSignature: "c2lnbmF0dXJl",
},
],
},
finish_reason: "stop",
},
],
usage: {
prompt_tokens: 1,
completion_tokens: 1,
total_tokens: 2,
},
};

function createTestProvider(body: object = responseBody) {
return createNeon({
baseURL: "https://example.com",
apiKey: "test-token",
fetch: async () =>
new Response(JSON.stringify(responseBody), {
new Response(JSON.stringify(body), {
status: 200,
headers: { "content-type": "application/json" },
}),
Expand Down Expand Up @@ -97,6 +127,24 @@ describe("AI SDK compatibility", () => {
expect(result.text).toBe("pong");
});

it("generates text from array content with AI SDK 6", async () => {
const result = await generateTextV6({
model: createTestProvider(arrayContentBody)("gemini-4-argon"),
prompt: "Reply with pong.",
});

expect(result.text).toBe("pong");
});

it("generates text from array content with AI SDK 7", async () => {
const result = await generateTextV7({
model: createTestProvider(arrayContentBody)("gemini-4-argon"),
prompt: "Reply with pong.",
});

expect(result.text).toBe("pong");
});

it("streams text with AI SDK 6", async () => {
const result = streamTextV6({
model: createStreamingTestProvider()("gpt-oss-20b"),
Expand Down
Loading