Skip to content

Commit 49a8d4d

Browse files
authored
Extensions.Bedrock.MEAI: Fix reading/writing ReasoningContent for tool use (#4080)
1. The `RedactedContent` property of `TextReasoningContent` is optional and not required for Claude 4 models. Don't throw new it's not found in `AdditionalProperties`. 2. Use `ProtectedData` for the "Signature" of the reasoning content so that it round trips. See: dotnet/extensions#6784 3. In `GetStreamingResponseAsync`, the final `ReasoningContentBlockDelta` may not have text, but it will have `Signature` and possibly `RedactedContent` These fix a scenario where thinking + tool use fails because the reasoning content sent back with the tool response is invalid.
1 parent 6faf37c commit 49a8d4d

File tree

6 files changed

+24
-11
lines changed

6 files changed

+24
-11
lines changed

extensions/src/AWSSDK.Extensions.Bedrock.MEAI/AWSSDK.Extensions.Bedrock.MEAI.NetFramework.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
</Choose>
3838

3939
<ItemGroup>
40-
<PackageReference Include="Microsoft.Extensions.AI.Abstractions" Version="9.9.0" />
40+
<PackageReference Include="Microsoft.Extensions.AI.Abstractions" Version="9.9.1" />
4141
</ItemGroup>
4242

4343
<ItemGroup>

extensions/src/AWSSDK.Extensions.Bedrock.MEAI/AWSSDK.Extensions.Bedrock.MEAI.NetStandard.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
</Choose>
4242

4343
<ItemGroup>
44-
<PackageReference Include="Microsoft.Extensions.AI.Abstractions" Version="9.9.0" />
44+
<PackageReference Include="Microsoft.Extensions.AI.Abstractions" Version="9.9.1" />
4545
</ItemGroup>
4646

4747
<ItemGroup>

extensions/src/AWSSDK.Extensions.Bedrock.MEAI/AWSSDK.Extensions.Bedrock.MEAI.nuspec

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,17 @@
1515
<group targetFramework="net472">
1616
<dependency id="AWSSDK.Core" version="4.0.1.3" />
1717
<dependency id="AWSSDK.BedrockRuntime" version="4.0.7.7" />
18-
<dependency id="Microsoft.Extensions.AI.Abstractions" version="9.9.0" />
18+
<dependency id="Microsoft.Extensions.AI.Abstractions" version="9.9.1" />
1919
</group>
2020
<group targetFramework="netstandard2.0">
2121
<dependency id="AWSSDK.Core" version="4.0.1.3" />
2222
<dependency id="AWSSDK.BedrockRuntime" version="4.0.7.7" />
23-
<dependency id="Microsoft.Extensions.AI.Abstractions" version="9.9.0" />
23+
<dependency id="Microsoft.Extensions.AI.Abstractions" version="9.9.1" />
2424
</group>
2525
<group targetFramework="net8.0">
2626
<dependency id="AWSSDK.Core" version="4.0.1.3" />
2727
<dependency id="AWSSDK.BedrockRuntime" version="4.0.7.7" />
28-
<dependency id="Microsoft.Extensions.AI.Abstractions" version="9.9.0" />
28+
<dependency id="Microsoft.Extensions.AI.Abstractions" version="9.9.1" />
2929
</group>
3030
</dependencies>
3131
</metadata>

extensions/src/AWSSDK.Extensions.Bedrock.MEAI/BedrockChatClient.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ public async Task<ChatResponse> GetResponseAsync(
121121

122122
if (reasoningContent.ReasoningText.Signature is string signature)
123123
{
124-
(trc.AdditionalProperties ??= [])[nameof(reasoningContent.ReasoningText.Signature)] = signature;
124+
trc.ProtectedData = signature;
125125
}
126126

127127
if (reasoningContent.RedactedContent is { } redactedContent)
@@ -249,13 +249,13 @@ public async IAsyncEnumerable<ChatResponseUpdate> GetStreamingResponseAsync(
249249
yield return textUpdate;
250250
}
251251

252-
if (contentBlockDelta.Delta.ReasoningContent is { Text: not null } reasoningContent)
252+
if (contentBlockDelta.Delta.ReasoningContent is { } reasoningContent)
253253
{
254254
TextReasoningContent trc = new(reasoningContent.Text);
255255

256256
if (reasoningContent.Signature is not null)
257257
{
258-
(trc.AdditionalProperties ??= [])[nameof(reasoningContent.Signature)] = reasoningContent.Signature;
258+
trc.ProtectedData = reasoningContent.Signature;
259259
}
260260

261261
if (reasoningContent.RedactedContent is { } redactedContent)
@@ -516,16 +516,18 @@ private static List<ContentBlock> CreateContents(ChatMessage message)
516516
break;
517517

518518
case TextReasoningContent trc:
519+
object? redactedContent = null;
520+
trc.AdditionalProperties?.TryGetValue(nameof(ReasoningContentBlock.RedactedContent), out redactedContent);
519521
contents.Add(new()
520522
{
521523
ReasoningContent = new()
522524
{
523525
ReasoningText = new()
524526
{
525527
Text = trc.Text,
526-
Signature = trc.AdditionalProperties?[nameof(ReasoningContentBlock.ReasoningText.Signature)] as string,
528+
Signature = trc.ProtectedData,
527529
},
528-
RedactedContent = trc.AdditionalProperties?[nameof(ReasoningContentBlock.RedactedContent)] is byte[] array ? new(array) : null,
530+
RedactedContent = redactedContent is byte[] array ? new(array) : null,
529531
}
530532
});
531533
break;

extensions/test/BedrockMEAITests/BedrockMEAITests.NetFramework.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
</PropertyGroup>
1919

2020
<ItemGroup>
21-
<PackageReference Include="Microsoft.Extensions.AI.Abstractions" Version="9.9.0" />
21+
<PackageReference Include="Microsoft.Extensions.AI.Abstractions" Version="9.9.1" />
2222
<PackageReference Include="xunit" Version="2.9.2" />
2323
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2" />
2424
</ItemGroup>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"extensions": [
3+
{
4+
"extensionName": "Extensions.Bedrock.MEAI",
5+
"type": "patch",
6+
"changeLogMessages": [
7+
"Fix reading/writing ReasoningContent for tool use."
8+
]
9+
}
10+
]
11+
}

0 commit comments

Comments
 (0)