Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -270,33 +270,36 @@ public Flux<DashScopeResponse> stream(
.build();

return transport.stream(httpRequest)
.map(
data -> {
.<ParsedStreamResponse>handle(
(data, sink) -> {
try {
// Decrypt response if encryption is enabled
if (finalEncryptionContext != null) {
data = decryptResponse(data, finalEncryptionContext);
}
return JsonUtils.getJsonCodec()
.fromJson(data, DashScopeResponse.class);
sink.next(
new ParsedStreamResponse(
data,
JsonUtils.getJsonCodec()
.fromJson(
data,
DashScopeResponse.class)));
} catch (JsonException e) {
log.warn(
"Failed to parse SSE data: {}. Error: {}",
data,
e.getMessage());
// Return null and filter out later
return null;
}
})
.filter(response -> response != null)
.handle(
(response, sink) -> {
(streamResponse, sink) -> {
DashScopeResponse response = streamResponse.response();
if (response.isError()) {
sink.error(
new DashScopeHttpException(
"DashScope API error: " + response.getMessage(),
response.getCode(),
null));
streamResponse.responseBody()));
} else {
sink.next(response);
}
Expand Down Expand Up @@ -834,6 +837,8 @@ public DashScopeHttpClient build() {
}
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[praise] Clean use of a private record to thread the raw response body alongside the parsed response through the reactive pipeline. Minimal change surface, no side channels, and makes the streaming error path symmetric with the sync execute() path at line 215. Well done.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[praise] Clean use of a private record to thread the raw response body alongside the parsed response through the reactive pipeline. Minimal change surface, no side channels, and makes the streaming error path symmetric with the sync execute() path at line 215. Well done.


private record ParsedStreamResponse(String responseBody, DashScopeResponse response) {}

/**
* Exception thrown when DashScope HTTP operations fail.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -647,10 +647,26 @@ void testStreamErrorHandling() {
&& dashScopeHttpException.getErrorCode().equals(errorCode)
&& dashScopeHttpException
.getMessage()
.equals("DashScope API error: " + errorMessage))
.equals("DashScope API error: " + errorMessage)
&& dashScopeHttpException
.getResponseBody()

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[praise] Good regression test — asserting getResponseBody().contains("request_id") directly validates the fix for #2197 and guards against future regressions.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[praise] Good regression test — asserting getResponseBody().contains("request_id") directly validates the fix for #2197 and guards against future regressions.

.contains("\"request_id\":\"request_id_123\""))
.verify();
}

@Test
void testStreamIgnoresMalformedSseData() {
mockServer.enqueue(
new MockResponse()
.setResponseCode(200)
.setBody("data: malformed-json\\n\\n")
.setHeader("Content-Type", "text/event-stream"));

DashScopeRequest request = createTestRequest("qwen-plus", "test");

StepVerifier.create(client.stream(request, null, null, null)).verifyComplete();
}

@Test
void testHeaderOverride() throws Exception {
mockServer.enqueue(
Expand Down
Loading