Skip to content

Never stop coding. Free MIT AI gateway: one endpoint, 375 providers (150+ free), 1,400+ models Kimi, Claude, GPT, Gemini, GLM, DeepSeek, MiniMax. Works with Claude Code, Codex, Cursor, OpenCode, Cline & Copilot. Quota-aware auto-fallback, RTK+Caveman compression saves 15-95% tokens, MCP/A2A, Desktop/PWA. Built by 900+ contributors

fix(stream): default Nextcloud integration to JSON

Jan Leon57aff1278192
diff --git a/open-sse/handlers/chatCore.ts b/open-sse/handlers/chatCore.ts
index 5c9c571148..2aff929deb 100644
--- a/open-sse/handlers/chatCore.ts
+++ b/open-sse/handlers/chatCore.ts
@@ -2105,6 +2105,12 @@ export async function handleChatCore({
clientRawRequest?.headers && typeof clientRawRequest.headers.get === "function"
? clientRawRequest.headers.get("accept") || clientRawRequest.headers.get("Accept")
: clientRawRequest?.headers?.["accept"] || clientRawRequest?.headers?.["Accept"];
+ const streamUserAgent = [
+ typeof userAgent === "string" ? userAgent : "",
+ getHeaderValueCaseInsensitive(clientRawRequest?.headers ?? null, "user-agent") || "",
+ ]
+ .filter(Boolean)
+ .join(" ");
const explicitStreamAlias = resolveExplicitStreamAlias(body);
@@ -2130,7 +2136,7 @@ export async function handleChatCore({
const stream =
nativeCodexPassthrough && isCompactResponsesEndpoint(endpointPath)
? false
- : resolveStreamFlag(body?.stream, acceptHeader, sourceFormat);
+ : resolveStreamFlag(body?.stream, acceptHeader, sourceFormat, streamUserAgent);
const settings = cachedSettings ?? (await getCachedSettings());
credentials = applyCodexGlobalFastServiceTier(provider, credentials, settings, {
model: requestedModel,
diff --git a/open-sse/utils/aiSdkCompat.ts b/open-sse/utils/aiSdkCompat.ts
index 2faaa6c5f1..60b468cd67 100644
--- a/open-sse/utils/aiSdkCompat.ts
+++ b/open-sse/utils/aiSdkCompat.ts
@@ -29,20 +29,30 @@ export function clientWantsJsonResponse(acceptHeader: unknown): boolean {
export function resolveStreamFlag(
bodyStream: unknown,
acceptHeader: unknown,
- sourceFormat?: string
+ sourceFormat?: string,
+ userAgent?: unknown
): boolean {
// Explicit body value always wins
if (bodyStream === true) return true;
if (bodyStream === false) return false;
+ const acceptsEventStream =
+ typeof acceptHeader === "string" && /text\/event-stream/i.test(acceptHeader);
+
// Anthropic Messages API spec: stream defaults to false when body omits it.
// Only honor an explicit text/event-stream Accept header as a streaming opt-in
// for /v1/messages — otherwise default to non-stream so upstream JSON responses
// are surfaced correctly instead of triggering stream_early_eof.
if (sourceFormat === "claude") {
- if (typeof acceptHeader === "string" && /text\/event-stream/i.test(acceptHeader)) {
- return true;
- }
+ if (acceptsEventStream) return true;
+ return false;
+ }
+
+ // Nextcloud's OpenAI/LocalAI integration sends synchronous JSON requests and
+ // does not set `stream: false`. With a wildcard/empty Accept header, the legacy
+ // OmniRoute fallback would force SSE upstream and fail JSON-only providers as
+ // STREAM_EARLY_EOF before Nextcloud could receive a response.
+ if (isKnownJsonOnlyClient(userAgent) && !acceptsEventStream) {
return false;
}
@@ -51,6 +61,11 @@ export function resolveStreamFlag(
return !clientWantsJsonResponse(acceptHeader);
}
+export function isKnownJsonOnlyClient(userAgent: unknown): boolean {
+ if (typeof userAgent !== "string") return false;
+ return /nextcloud\s+openai\/localai\s+integration/i.test(userAgent);
+}
+
/**
* Resolves explicit stream aliases used by non-standard clients.
* Returns:
diff --git a/tests/unit/chatcore-sanitization.test.ts b/tests/unit/chatcore-sanitization.test.ts
index be5cc01da2..b62211f789 100644
--- a/tests/unit/chatcore-sanitization.test.ts
+++ b/tests/unit/chatcore-sanitization.test.ts
@@ -102,9 +102,11 @@ async function invokeChatCore({
} = {}) {
const originalFetch = globalThis.fetch;
const calls = [];
+ const nextcloudJsonOnlyClient = /nextcloud\s+openai\/localai\s+integration/i.test(userAgent);
const resolvedStream =
body?.stream === true ||
- (body?.stream === undefined && String(accept).toLowerCase().includes("text/event-stream"));
+ (body?.stream === undefined && String(accept).toLowerCase().includes("text/event-stream")) ||
+ (body?.stream === undefined && !nextcloudJsonOnlyClient && !String(accept).includes("json"));
globalThis.fetch = async (url, init = {}) => {
const parsedBody = init.body ? JSON.parse(String(init.body)) : null;
@@ -128,7 +130,7 @@ async function invokeChatCore({
clientRawRequest: {
endpoint,
body: structuredClone(body),
- headers: new Headers({ accept }),
+ headers: new Headers({ accept, "user-agent": userAgent }),
},
apiKeyInfo,
userAgent,
@@ -508,6 +510,23 @@ test("chatCore resolves stream mode from body.stream and Accept header", async (
assert.equal(jsonDefault.call.headers.Accept, "application/json");
});
+test("chatCore treats Nextcloud OpenAI integration requests as non-streaming by default", async () => {
+ const nextcloudDefault = await invokeChatCore({
+ accept: "*/*",
+ userAgent: "Nextcloud OpenAI/LocalAI integration",
+ body: { model: "gpt-4o-mini", messages: [{ role: "user", content: "hello" }] },
+ });
+ const nextcloudExplicitStream = await invokeChatCore({
+ accept: "application/json",
+ userAgent: "Nextcloud OpenAI/LocalAI integration",
+ body: { model: "gpt-4o-mini", stream: true, messages: [{ role: "user", content: "hello" }] },
+ });
+
+ assert.equal(nextcloudDefault.call.headers.Accept, "application/json");
+ assert.equal(nextcloudDefault.result.response.headers.get("content-type"), "application/json");
+ assert.equal(nextcloudExplicitStream.call.headers.Accept, "text/event-stream");
+});
+
test("chatCore injects memories when enabled and memories are found", async () => {
await settingsDb.updateSettings({
memoryEnabled: true,
diff --git a/tests/unit/t26-ai-sdk-accept-header-compat.test.ts b/tests/unit/t26-ai-sdk-accept-header-compat.test.ts
index 81c91d8282..983379c6f9 100644
--- a/tests/unit/t26-ai-sdk-accept-header-compat.test.ts
+++ b/tests/unit/t26-ai-sdk-accept-header-compat.test.ts
@@ -3,6 +3,7 @@ import assert from "node:assert/strict";
const {
clientWantsJsonResponse,
+ isKnownJsonOnlyClient,
resolveStreamFlag,
resolveExplicitStreamAlias,
hasExplicitNoStreamParam,
@@ -81,6 +82,17 @@ test("T26: non-claude sourceFormat preserves pre-#2325 streaming default", () =>
assert.equal(resolveStreamFlag(undefined, "application/json"), false);
});
+test("T26: Nextcloud OpenAI integration defaults to non-streaming JSON", () => {
+ const ua = "Nextcloud OpenAI/LocalAI integration";
+
+ assert.equal(isKnownJsonOnlyClient(ua), true);
+ assert.equal(resolveStreamFlag(undefined, undefined, "openai", ua), false);
+ assert.equal(resolveStreamFlag(undefined, "*/*", "openai", ua), false);
+ assert.equal(resolveStreamFlag(undefined, "application/json", "openai", ua), false);
+ assert.equal(resolveStreamFlag(undefined, "text/event-stream", "openai", ua), true);
+ assert.equal(resolveStreamFlag(true, "application/json", "openai", ua), true);
+});
+
test("T26: explicit non-stream aliases are detected", () => {
assert.equal(hasExplicitNoStreamParam({ non_stream: true }), true);
assert.equal(hasExplicitNoStreamParam({ disable_stream: true }), true);