Skip to content

Commit c6f77bd

Browse files
giulio-leoneCopilot
andcommitted
fix(amazon-bedrock): return request body from doGenerate and doStream
Add `request: { body: args }` to the return objects of both `doGenerate()` and `doStream()` in BedrockChatLanguageModel, matching the pattern used by `@ai-sdk/anthropic` and other providers. Previously the Bedrock provider omitted the request field (with a `// TODO request?` comment in doStream), causing observability frameworks like Mastra to receive `undefined` when reading `request.body` for tracing span inputs. Fixes #13927 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 1e5f83c commit c6f77bd

File tree

4 files changed

+78
-1
lines changed

4 files changed

+78
-1
lines changed

.changeset/bedrock-request-body.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@ai-sdk/amazon-bedrock': patch
3+
---
4+
5+
Return `request: { body }` from `doGenerate()` and `doStream()` in the Bedrock chat language model, matching the pattern used by other providers (e.g. `@ai-sdk/anthropic`). This enables observability frameworks to access the request body for tracing and debugging.

packages/amazon-bedrock/src/__snapshots__/bedrock-chat-language-model.test.ts.snap

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,29 @@ There are 3 r's.",
3535
},
3636
},
3737
},
38+
"request": {
39+
"body": {
40+
"additionalModelRequestFields": undefined,
41+
"additionalModelResponseFieldPaths": [
42+
"/delta/stop_sequence",
43+
],
44+
"messages": [
45+
{
46+
"content": [
47+
{
48+
"text": "Hello",
49+
},
50+
],
51+
"role": "user",
52+
},
53+
],
54+
"system": [
55+
{
56+
"text": "System Prompt",
57+
},
58+
],
59+
},
60+
},
3861
"response": {
3962
"headers": {
4063
"content-length": "865",
@@ -92,6 +115,29 @@ There are **3** "r"s in "strawberry."",
92115
},
93116
},
94117
},
118+
"request": {
119+
"body": {
120+
"additionalModelRequestFields": undefined,
121+
"additionalModelResponseFieldPaths": [
122+
"/delta/stop_sequence",
123+
],
124+
"messages": [
125+
{
126+
"content": [
127+
{
128+
"text": "Hello",
129+
},
130+
],
131+
"role": "user",
132+
},
133+
],
134+
"system": [
135+
{
136+
"text": "System Prompt",
137+
},
138+
],
139+
},
140+
},
95141
"response": {
96142
"headers": {
97143
"content-length": "435",

packages/amazon-bedrock/src/bedrock-chat-language-model.test.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,19 @@ describe('doStream', () => {
270270
}
271271
`);
272272
});
273+
274+
it('should include request body', async () => {
275+
const result = await model.doStream({
276+
prompt: TEST_PROMPT,
277+
includeRawChunks: false,
278+
});
279+
280+
expect(result.request).toBeDefined();
281+
expect(result.request?.body).toBeDefined();
282+
expect(result.request?.body).toMatchObject({
283+
messages: expect.any(Array),
284+
});
285+
});
273286
});
274287

275288
describe('reasoning', () => {
@@ -3133,6 +3146,18 @@ describe('doGenerate', () => {
31333146
}
31343147
`);
31353148
});
3149+
3150+
it('should include request body', async () => {
3151+
const { request } = await model.doGenerate({
3152+
prompt: TEST_PROMPT,
3153+
});
3154+
3155+
expect(request).toBeDefined();
3156+
expect(request?.body).toBeDefined();
3157+
expect(request?.body).toMatchObject({
3158+
messages: expect.any(Array),
3159+
});
3160+
});
31363161
});
31373162

31383163
describe('reasoning', () => {

packages/amazon-bedrock/src/bedrock-chat-language-model.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -575,6 +575,7 @@ export class BedrockChatLanguageModel implements LanguageModelV4 {
575575
raw: response.stopReason ?? undefined,
576576
},
577577
usage: convertBedrockUsage(response.usage),
578+
request: { body: args },
578579
response: {
579580
id: responseHeaders?.['x-amzn-requestid'] ?? undefined,
580581
timestamp:
@@ -979,7 +980,7 @@ export class BedrockChatLanguageModel implements LanguageModelV4 {
979980
},
980981
}),
981982
),
982-
// TODO request?
983+
request: { body: args },
983984
response: { headers: responseHeaders },
984985
};
985986
}

0 commit comments

Comments
 (0)