|
21 | 21 | using System.Buffers; |
22 | 22 | using System.Collections.Generic; |
23 | 23 | using System.Diagnostics; |
| 24 | +using System.Globalization; |
24 | 25 | using System.IO; |
25 | 26 | using System.Linq; |
26 | 27 | using System.Runtime.CompilerServices; |
@@ -97,11 +98,14 @@ public async Task<ChatResponse> GetResponseAsync( |
97 | 98 | { |
98 | 99 | response = await _runtime.ConverseAsync(request, cancellationToken).ConfigureAwait(false); |
99 | 100 | } |
| 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. |
100 | 103 | catch (AmazonBedrockRuntimeException ex) when (options?.ResponseFormat is ChatResponseFormatJson) |
101 | 104 | { |
102 | | - // Detect unsupported model: ValidationException mentioning "toolChoice" |
| 105 | + // Detect unsupported model: ValidationException with specific tool support error messages |
103 | 106 | 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)) |
105 | 109 | { |
106 | 110 | throw new NotSupportedException( |
107 | 111 | $"The model '{request.ModelId}' does not support ResponseFormat. " + |
@@ -148,18 +152,11 @@ public async Task<ChatResponse> GetResponseAsync( |
148 | 152 | else |
149 | 153 | { |
150 | 154 | // 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 | | - |
158 | 155 | throw new InvalidOperationException( |
159 | 156 | $"Model '{request.ModelId}' did not return structured output as requested. " + |
160 | 157 | $"This may indicate the model refused to follow the tool use instruction, " + |
161 | 158 | $"the schema was too complex, or the prompt conflicted with the requirement. " + |
162 | | - $"StopReason: {response.StopReason?.Value ?? "unknown"}"); |
| 159 | + $"StopReason: {response.StopReason?.Value ?? "unknown"}."); |
163 | 160 | } |
164 | 161 | } |
165 | 162 |
|
|
0 commit comments