Skip to content

Commit 062070c

Browse files
committed
Fix error handling issues and add critical error scenario test coverage per PR feedback
1 parent cf26be9 commit 062070c

File tree

2 files changed

+1033
-126
lines changed

2 files changed

+1033
-126
lines changed

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

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
using System.Buffers;
2222
using System.Collections.Generic;
2323
using System.Diagnostics;
24+
using System.Globalization;
2425
using System.IO;
2526
using System.Linq;
2627
using System.Runtime.CompilerServices;
@@ -97,11 +98,14 @@ public async Task<ChatResponse> GetResponseAsync(
9798
{
9899
response = await _runtime.ConverseAsync(request, cancellationToken).ConfigureAwait(false);
99100
}
101+
// Transforms ValidationException to NotSupportedException when error message indicates model lacks tool use support (required for ResponseFormat).
102+
// This detection relies on error message text which may change in future Bedrock API versions.
100103
catch (AmazonBedrockRuntimeException ex) when (options?.ResponseFormat is ChatResponseFormatJson)
101104
{
102-
// Detect unsupported model: ValidationException mentioning "toolChoice"
105+
// Detect unsupported model: ValidationException with specific tool support error messages
103106
if (ex.ErrorCode == "ValidationException" &&
104-
ex.Message.IndexOf("toolchoice", StringComparison.OrdinalIgnoreCase) >= 0)
107+
(ex.Message.IndexOf("toolChoice is not supported by this model", StringComparison.OrdinalIgnoreCase) >= 0 ||
108+
ex.Message.IndexOf("This model doesn't support tool use", StringComparison.OrdinalIgnoreCase) >= 0))
105109
{
106110
throw new NotSupportedException(
107111
$"The model '{request.ModelId}' does not support ResponseFormat. " +
@@ -148,18 +152,11 @@ public async Task<ChatResponse> GetResponseAsync(
148152
else
149153
{
150154
// Model succeeded but did not return expected structured output
151-
var errorMessage = string.Format(
152-
"ResponseFormat was specified but model did not return expected tool use. ModelId: {0}, StopReason: {1}",
153-
request.ModelId,
154-
response.StopReason?.Value ?? "unknown");
155-
156-
DefaultLogger.Error(new InvalidOperationException(errorMessage), errorMessage);
157-
158155
throw new InvalidOperationException(
159156
$"Model '{request.ModelId}' did not return structured output as requested. " +
160157
$"This may indicate the model refused to follow the tool use instruction, " +
161158
$"the schema was too complex, or the prompt conflicted with the requirement. " +
162-
$"StopReason: {response.StopReason?.Value ?? "unknown"}");
159+
$"StopReason: {response.StopReason?.Value ?? "unknown"}.");
163160
}
164161
}
165162

0 commit comments

Comments
 (0)