Skip to content

Commit d76d4e4

Browse files
committed
fix(bedrock): request stop_details from the streaming message_delta
c688a19 asked ConverseStream for `/stop_details`, and no turn ever came back with it. ConverseStream resolves additionalModelResponseFieldPaths against Anthropic's native streaming `message_delta` event, where the field sits under `delta`, not against the non-streaming message. Verified live against Opus 5.5 on 2026-09-27: - `/stop_details` returned nothing. - `/delta/stop_details` returned `{"delta":{"stop_details":null}}` in messageStop.additionalModelResponseFields on an ordinary turn. The pointer and the reader now use that nested shape. - https://docs.aws.amazon.com/bedrock/latest/userguide/conversation-inference.html#converse-additional-model-response-field-paths - https://platform.claude.com/docs/en/build-with-claude/refusals-and-fallback#what-a-refusal-looks-like See also Zoo-Code-Org#1820.
1 parent aafc606 commit d76d4e4

2 files changed

Lines changed: 37 additions & 15 deletions

File tree

‎src/api/providers/__tests__/bedrock.stream-diagnostics.spec.ts‎

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -162,12 +162,12 @@ describe("AwsBedrockHandler stream diagnostics", () => {
162162
})
163163

164164
describe("stop_details forwarding", () => {
165-
it("asks Bedrock to forward Anthropic's stop_details for Claude models", async () => {
165+
it("asks Bedrock to forward Anthropic's stop_details from the streaming message_delta", async () => {
166166
const handler = createHandler([{ messageStop: { stopReason: "end_turn" } }])
167167
await collect(handler)
168168

169169
const payload = vi.mocked(ConverseStreamCommand).mock.calls[0][0]
170-
expect(payload.additionalModelResponseFieldPaths).toEqual(["/stop_details"])
170+
expect(payload.additionalModelResponseFieldPaths).toEqual(["/delta/stop_details"])
171171
})
172172

173173
it("appends the refusal category and explanation to the stop reason", async () => {
@@ -177,10 +177,12 @@ describe("AwsBedrockHandler stream diagnostics", () => {
177177
messageStop: {
178178
stopReason: "content_filtered",
179179
additionalModelResponseFields: {
180-
stop_details: {
181-
type: "refusal",
182-
category: "reasoning_extraction",
183-
explanation: "Declined to reproduce internal reasoning.",
180+
delta: {
181+
stop_details: {
182+
type: "refusal",
183+
category: "reasoning_extraction",
184+
explanation: "Declined to reproduce internal reasoning.",
185+
},
184186
},
185187
},
186188
},
@@ -201,7 +203,7 @@ describe("AwsBedrockHandler stream diagnostics", () => {
201203
messageStop: {
202204
stopReason: "content_filtered",
203205
additionalModelResponseFields: {
204-
stop_details: { type: "refusal", category: null, explanation: null },
206+
delta: { stop_details: { type: "refusal", category: null, explanation: null } },
205207
},
206208
},
207209
},
@@ -212,19 +214,35 @@ describe("AwsBedrockHandler stream diagnostics", () => {
212214
})
213215

214216
it("keeps the bare stop reason when stop_details is null", async () => {
217+
// Shape Bedrock returned live for an ordinary turn.
215218
const chunks = await collect(
216219
createHandler([
217220
{
218221
messageStop: {
219222
stopReason: "end_turn",
220-
additionalModelResponseFields: { stop_details: null },
223+
additionalModelResponseFields: { delta: { stop_details: null } },
221224
},
222225
},
223226
]),
224227
)
225228

226229
expect(chunks).toContainEqual({ type: "stop_reason", reason: "end_turn" })
227230
})
231+
232+
it("ignores stop_details at the top level, where ConverseStream never puts it", async () => {
233+
const chunks = await collect(
234+
createHandler([
235+
{
236+
messageStop: {
237+
stopReason: "content_filtered",
238+
additionalModelResponseFields: { stop_details: { type: "refusal", category: "cyber" } },
239+
},
240+
},
241+
]),
242+
)
243+
244+
expect(chunks).toContainEqual({ type: "stop_reason", reason: "content_filtered" })
245+
})
228246
})
229247
})
230248

‎src/api/providers/bedrock.ts‎

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -71,20 +71,24 @@ interface BedrockInferenceConfig {
7171
/**
7272
* Asks Bedrock to forward Anthropic's `stop_details`, which names the classifier category behind a refusal
7373
* (for example `reasoning_extraction`). Bedrock reports such refusals only as `content_filtered` otherwise.
74-
* A valid pointer the model doesn't return is ignored by Converse. See #1820.
74+
* See #1820.
7575
*
76-
* Not yet effective: as of 2026-09-26, Opus 5.5 on Bedrock accepted this request field but returned
77-
* `content_filtered` turns without `additionalModelResponseFields`, so no category was available.
78-
* Kept because it is harmless and starts working if Bedrock begins forwarding the field.
76+
* ConverseStream resolves these pointers against the native streaming `message_delta` event,
77+
* where the field sits under `delta`, not against the non-streaming message.
78+
* Verified live on 2026-09-27: `/stop_details` came back as nothing,
79+
* while `/delta/stop_details` returned `{"delta":{"stop_details":null}}` in
80+
* `messageStop.additionalModelResponseFields` on an ordinary turn.
81+
* A valid pointer the model doesn't return is ignored.
7982
*
8083
* - https://platform.claude.com/docs/en/build-with-claude/refusals-and-fallback#what-a-refusal-looks-like
81-
* - https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_ConverseStream.html#bedrock-runtime_ConverseStream-request-additionalModelResponseFieldPaths
84+
* - https://docs.aws.amazon.com/bedrock/latest/userguide/conversation-inference.html#converse-additional-model-response-field-paths
8285
*/
83-
const ANTHROPIC_RESPONSE_FIELD_PATHS = ["/stop_details"]
86+
const ANTHROPIC_RESPONSE_FIELD_PATHS = ["/delta/stop_details"]
8487

8588
/** Renders forwarded `stop_details` as `category: explanation`; the explanation text is unstable, so it's shown, not parsed. */
8689
export function formatStopDetails(fields: Record<string, unknown> | undefined): string | undefined {
87-
const details = fields?.stop_details
90+
const delta = fields?.delta
91+
const details = delta && typeof delta === "object" ? (delta as Record<string, unknown>).stop_details : undefined
8892
if (!details || typeof details !== "object") {
8993
return undefined
9094
}

0 commit comments

Comments
 (0)