Repository navigation
fix(sse): keep streaming for forceStream providers when client requests JSON #5021
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
diegosouzapw
merged 1 commit into
release/v3.8.38
from
fix/port-pr-2081-force-stream-json
Jun 26, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -99,7 +99,7 @@ import { normalizeClaudeAdaptiveThinking } from "../services/claudeAdaptiveThink | |||||
| import { normalizeClaudeHaikuConstraints } from "../services/claudeHaikuConstraints.ts"; | ||||||
| import { echoModelInObject } from "../services/responseModelEcho.ts"; | ||||||
| import { stripGpt5SamplingWhenReasoning } from "../services/gpt5SamplingGuard.ts"; | ||||||
| import { getUnsupportedParams } from "../config/providerRegistry.ts"; | ||||||
| import { getUnsupportedParams, REGISTRY } from "../config/providerRegistry.ts"; | ||||||
| import { supportsMaxTokens } from "@/lib/modelCapabilities.ts"; | ||||||
| import { normalizeThinkingForModel } from "@/shared/constants/modelSpecs.ts"; | ||||||
| import { | ||||||
|
|
@@ -783,12 +783,17 @@ export async function handleChatCore({ | |||||
| // sourceFormat="claude" applies the Anthropic Messages spec default (stream=false | ||||||
| // when body omits stream), preventing STREAM_EARLY_EOF on /v1/messages when | ||||||
| // clients send Accept: */* without an explicit stream flag. | ||||||
| // providerRequiresStreaming: providers with forceStream:true reject stream:false | ||||||
| // upstream (HTTP 400); keep streaming so OmniRoute can convert the stream to JSON | ||||||
| // for the client via handleForcedSSEToJson. (#2081) | ||||||
| const providerRequiresStreaming = REGISTRY[provider]?.forceStream === true; | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use
Suggested change
|
||||||
| const stream = | ||||||
| nativeCodexPassthrough && isCompactResponsesEndpoint(endpointPath) | ||||||
| ? false | ||||||
| : resolveStreamFlag(body?.stream, acceptHeader, sourceFormat, { | ||||||
| userAgent: streamUserAgent, | ||||||
| streamDefaultMode: apiKeyInfo?.streamDefaultMode, | ||||||
| providerRequiresStreaming, | ||||||
| }); | ||||||
|
|
||||||
| // `settings` is already consolidated once near the top of handleChatCore | ||||||
|
|
||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| // Port of upstream #2081 — forceStream (stream-only) providers must keep streaming even | ||
| // when the client asks for a non-streaming/JSON response. OmniRoute then accumulates the | ||
| // provider stream and returns a normal JSON body to the client (handleForcedSSEToJson). | ||
| import { describe, it } from "node:test"; | ||
| import assert from "node:assert/strict"; | ||
| import { resolveStreamFlag } from "../../open-sse/utils/aiSdkCompat.ts"; | ||
|
|
||
| describe("resolveStreamFlag — forceStream / providerRequiresStreaming guard (#2081)", () => { | ||
| it("keeps streaming for a forceStream provider even when client prefers JSON and sets stream:false", () => { | ||
| // The bug: Accept: application/json + stream:false used to override providerRequiresStreaming, | ||
| // sending stream:false to a stream-only provider (e.g. CodeBuddy) → HTTP 400. | ||
| const result = resolveStreamFlag( | ||
| false, // body.stream = false | ||
| "application/json", // Accept header | ||
| undefined, // sourceFormat | ||
| { providerRequiresStreaming: true } | ||
| ); | ||
| assert.equal(result, true, "stream-only provider must stay streaming even when client prefers JSON"); | ||
| }); | ||
|
|
||
| it("non-forceStream provider: client prefers JSON + stream:false → non-streaming (unchanged behavior)", () => { | ||
| const result = resolveStreamFlag( | ||
| false, | ||
| "application/json", | ||
| undefined, | ||
| { providerRequiresStreaming: false } | ||
| ); | ||
| assert.equal(result, false, "normal provider should respect client JSON preference"); | ||
| }); | ||
|
|
||
| it("forceStream provider: no explicit stream flag → streams by default", () => { | ||
| const result = resolveStreamFlag( | ||
| undefined, | ||
| undefined, | ||
| undefined, | ||
| { providerRequiresStreaming: true } | ||
| ); | ||
| assert.equal(result, true); | ||
| }); | ||
|
|
||
| it("ordinary provider with no special flags streams by default (backward compat)", () => { | ||
| const result = resolveStreamFlag(undefined, undefined); | ||
| assert.equal(result, true); | ||
| }); | ||
|
|
||
| it("forceStream provider: client explicitly sends stream:true → stays true", () => { | ||
| const result = resolveStreamFlag( | ||
| true, | ||
| "application/json", | ||
| undefined, | ||
| { providerRequiresStreaming: true } | ||
| ); | ||
| assert.equal(result, true); | ||
| }); | ||
|
|
||
| it("forceStream provider: client sends Accept: text/event-stream + stream:false → stays true", () => { | ||
| // SSE Accept header alone shouldn't be needed for stream-only providers, | ||
| // but providerRequiresStreaming should still force true. | ||
| const result = resolveStreamFlag( | ||
| false, | ||
| "text/event-stream", | ||
| undefined, | ||
| { providerRequiresStreaming: true } | ||
| ); | ||
| assert.equal(result, true); | ||
| }); | ||
|
|
||
| it("without providerRequiresStreaming option, JSON client + stream:false still gets non-streaming", () => { | ||
| // Verify backward compatibility — no regression for callers that don't pass the option | ||
| const result = resolveStreamFlag(false, "application/json"); | ||
| assert.equal(result, false); | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of importing and querying the raw
REGISTRYobject directly, use thegetRegistryEntryhelper. This ensures that ifprovideris an alias, the registry entry is still correctly resolved. Direct lookup viaREGISTRY[provider]will returnundefinedfor aliased providers, which would bypass theforceStreamguard and cause HTTP 400 errors when clients request JSON.