feat(bedrock-runtime): stream ConverseStream incrementally (#3043)

ConverseStream previously buffered the whole proxied response, translated it in
one shot, and returned it as a single byte[]. Both backends now write frames
through a JAX-RS StreamingOutput as they're known, and the proxy backend reads
the upstream SSE response via HttpResponse.BodyHandlers.ofLines() instead of
ofString() - text deltas reach SDK clients as the model generates them instead
of landing all at once when the response finishes.

Once the StreamingOutput starts writing, JAX-RS has already committed the 200
response, so a failure discovered only after streaming begins - a stream that
never reaches a finish_reason or "[DONE]" (upstream connection dropped
mid-generation) - can no longer become an HTTP error status. It's now reported
as a modelStreamErrorException event, the same wire shape (:message-type:
exception, :exception-type: <name>) and modeled exception real Bedrock uses
for a mid-stream failure, verified to decode correctly through
BedrockRuntimeAsyncClient's generated ConverseStreamResponseHandler. A stream
that completes normally (finish_reason or "[DONE]" seen) with no content is
now treated as a legitimate empty completion rather than an error, matching
real Bedrock.

Added a compatibility-tests/sdk-test-java case exercising
BedrockRuntimeAsyncClient.converseStream end to end, proving the emitted
event-stream frames decode correctly through the real AWS SDK rather than
only Floci's own byte-level assertions.
This commit is contained in:
Scott Steen authored and GitHub committed 2026-09-05 15:17:39 -04:00
1 parent 724be0a6a7
commit 0471d6f488
13 files changed
+475 -257

No files matched your search

@@ -775,14 +775,16 @@ class BedrockProxyIntegrationTest {
}
@Test
void converseStream_noUsableSseChunks_returns424InsteadOfFakeSuccess() {
void converseStream_noUsableSseChunks_emitsInBandStreamError() {
// A 2xx response with a body that isn't SSE-shaped at all (e.g. a plain JSON error
// object) must not be silently translated into a "successful" empty completion.
// object) never reaches a finish_reason or "[DONE]". The HTTP status is already 200 by
// the time messageStart is written, so the failure surfaces as an in-band
// modelStreamErrorException event rather than an HTTP error status.
nextResponseBody.set("""
{"error": {"message": "model not found"}}
""");
given()
String body = given()
.contentType("application/json")
.header("Authorization", AUTH_HEADER)
.body("""
@@ -791,16 +793,20 @@ class BedrockProxyIntegrationTest {
.when()
.post("/model/" + MAPPED_MODEL_ID + "/converse-stream")
.then()
.statusCode(424)
.body("__type", equalTo("ModelErrorException"));
.statusCode(200)
.header("Content-Type", containsString("application/vnd.amazon.eventstream"))
.extract().body().asString();
assertThat(body, containsString("messageStart"));
assertThat(body, containsString("modelStreamErrorException"));
assertThat(body, org.hamcrest.Matchers.not(containsString("messageStop")));
}
@Test
void converseStream_parsableChunksWithNoContent_returns424InsteadOfFakeSuccess() {
// Every line here parses as valid JSON (parsedChunks > 0), but none of them carry
// actual completion content - a role-only chunk, then a bare finish_reason with no
// text or tool_calls. A finish_reason alone must not be treated as proof of a real
// completion; this is indistinguishable from a truncated/failed generation.
void converseStream_completedWithNoContent_isValidEmptyCompletion() {
// A role-only chunk, then a bare finish_reason with no text or tool_calls, then [DONE].
// The stream properly completed (finish_reason and "[DONE]" were both seen) - real
// Bedrock delivers this as a legitimate empty message, not an error.
nextResponseBody.set("""
data: {"choices":[{"delta":{"role":"assistant"}}]}
@@ -809,7 +815,7 @@ class BedrockProxyIntegrationTest {
data: [DONE]
""");
given()
String body = given()
.contentType("application/json")
.header("Authorization", AUTH_HEADER)
.body("""
@@ -818,22 +824,30 @@ class BedrockProxyIntegrationTest {
.when()
.post("/model/" + MAPPED_MODEL_ID + "/converse-stream")
.then()
.statusCode(424)
.body("__type", equalTo("ModelErrorException"));
.statusCode(200)
.extract().body().asString();
assertThat(body, containsString("messageStart"));
assertThat(body, containsString("messageStop"));
assertThat(body, containsString("end_turn"));
assertThat(body, containsString("metadata"));
assertThat(body, org.hamcrest.Matchers.not(containsString("modelStreamErrorException")));
assertThat(body, org.hamcrest.Matchers.not(containsString("contentBlockDelta")));
}
@Test
void converseStream_truncatedTextWithNoFinishReason_returns424() {
void converseStream_truncatedTextWithNoFinishReason_emitsInBandStreamErrorAfterPartialText() {
// A real text delta arrives, but the stream ends (EOF, no [DONE], no finish_reason) -
// e.g. the upstream connection dropped mid-generation. Accumulated text alone must not
// be treated as a completed answer.
// e.g. the upstream connection dropped mid-generation. The already-streamed text and the
// 200 status can't be undone, so the truncation is reported as a trailing in-band
// modelStreamErrorException event instead of a clean messageStop.
nextResponseBody.set("""
data: {"choices":[{"delta":{"role":"assistant"}}]}
data: {"choices":[{"delta":{"content":"Hello"}}]}
""");
given()
String body = given()
.contentType("application/json")
.header("Authorization", AUTH_HEADER)
.body("""
@@ -842,15 +856,21 @@ class BedrockProxyIntegrationTest {
.when()
.post("/model/" + MAPPED_MODEL_ID + "/converse-stream")
.then()
.statusCode(424)
.body("__type", equalTo("ModelErrorException"));
.statusCode(200)
.extract().body().asString();
assertThat(body, containsString("messageStart"));
assertThat(body, containsString("Hello"));
assertThat(body, containsString("modelStreamErrorException"));
assertThat(body, org.hamcrest.Matchers.not(containsString("messageStop")));
}
@Test
void converseStream_incompleteToolCallFragment_returns424() {
// The tool_calls delta never carries a "name" and its arguments never close into
// valid JSON - a partial/corrupted fragment, not a usable tool_use block. It must be
// dropped rather than surfacing as a 200 with an empty-name toolUse block.
void converseStream_incompleteToolCallFragment_dropsFragmentAndCompletesNormally() {
// The tool_calls delta never carries a "name" and its arguments never close into valid
// JSON - a partial/corrupted fragment, not a usable tool_use block. It's dropped rather
// than surfacing as a toolUse block with empty identity fields; since the stream did
// reach "[DONE]"/finish_reason, it still completes normally (as an empty message).
nextResponseBody.set("""
data: {"choices":[{"delta":{"role":"assistant"}}]}
@@ -861,7 +881,7 @@ class BedrockProxyIntegrationTest {
data: [DONE]
""");
given()
String body = given()
.contentType("application/json")
.header("Authorization", AUTH_HEADER)
.body("""
@@ -870,8 +890,14 @@ class BedrockProxyIntegrationTest {
.when()
.post("/model/" + MAPPED_MODEL_ID + "/converse-stream")
.then()
.statusCode(424)
.body("__type", equalTo("ModelErrorException"));
.statusCode(200)
.extract().body().asString();
assertThat(body, containsString("messageStop"));
assertThat(body, containsString("end_turn"));
assertThat(body, org.hamcrest.Matchers.not(containsString("call_1")));
assertThat(body, org.hamcrest.Matchers.not(containsString("toolUse")));
assertThat(body, org.hamcrest.Matchers.not(containsString("modelStreamErrorException")));
}
@Test