Skip to content

Commit a06bd63

Browse files
authored
Use ErrorContent in OpenAIResponseChatClient (#6231)
* Use ErrorContent in OpenAIResponseChatClient * Address feedback
1 parent 444e5cb commit a06bd63

File tree

3 files changed

+50
-15
lines changed

3 files changed

+50
-15
lines changed

src/Libraries/Microsoft.Extensions.AI.Abstractions/Contents/ErrorContent.cs

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using System.Diagnostics;
5-
using System.Text.Json.Serialization;
6-
using Microsoft.Shared.Diagnostics;
5+
using System.Diagnostics.CodeAnalysis;
76

87
namespace Microsoft.Extensions.AI;
98

@@ -16,33 +15,33 @@ namespace Microsoft.Extensions.AI;
1615
public class ErrorContent : AIContent
1716
{
1817
/// <summary>The error message.</summary>
19-
private string _message;
18+
private string? _message;
2019

21-
/// <summary>Initializes a new instance of the <see cref="ErrorContent"/> class with the specified message.</summary>
22-
/// <param name="message">The message to store in this content.</param>
23-
[JsonConstructor]
24-
public ErrorContent(string message)
20+
/// <summary>Initializes a new instance of the <see cref="ErrorContent"/> class with the specified error message.</summary>
21+
/// <param name="message">The error message to store in this content.</param>
22+
public ErrorContent(string? message)
2523
{
26-
_message = Throw.IfNull(message);
24+
_message = message;
2725
}
2826

2927
/// <summary>Gets or sets the error message.</summary>
28+
[AllowNull]
3029
public string Message
3130
{
32-
get => _message;
33-
set => _message = Throw.IfNull(value);
31+
get => _message ?? string.Empty;
32+
set => _message = value;
3433
}
3534

36-
/// <summary>Gets or sets the error code.</summary>
35+
/// <summary>Gets or sets an error code associated with the error.</summary>
3736
public string? ErrorCode { get; set; }
3837

39-
/// <summary>Gets or sets the error details.</summary>
38+
/// <summary>Gets or sets additional details about the error.</summary>
4039
public string? Details { get; set; }
4140

4241
/// <summary>Gets a string representing this instance to display in the debugger.</summary>
4342
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
4443
private string DebuggerDisplay =>
45-
$"Error = {Message}" +
46-
(ErrorCode is not null ? $" ({ErrorCode})" : string.Empty) +
47-
(Details is not null ? $" - {Details}" : string.Empty);
44+
$"Error = \"{Message}\"" +
45+
(!string.IsNullOrWhiteSpace(ErrorCode) ? $" ({ErrorCode})" : string.Empty) +
46+
(!string.IsNullOrWhiteSpace(Details) ? $" - \"{Details}\"" : string.Empty);
4847
}

src/Libraries/Microsoft.Extensions.AI.OpenAI/OpenAIResponseChatClient.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,11 @@ public async Task<ChatResponse> GetResponseAsync(
132132
break;
133133
}
134134
}
135+
136+
if (openAIResponse.Error is { } error)
137+
{
138+
message.Contents.Add(new ErrorContent(error.Message) { ErrorCode = error.Code });
139+
}
135140
}
136141

137142
return response;
@@ -246,6 +251,24 @@ public async IAsyncEnumerable<ChatResponseUpdate> GetStreamingResponseAsync(
246251

247252
break;
248253
}
254+
255+
case StreamingResponseErrorUpdate errorUpdate:
256+
yield return new ChatResponseUpdate
257+
{
258+
CreatedAt = createdAt,
259+
MessageId = lastMessageId,
260+
ModelId = modelId,
261+
ResponseId = responseId,
262+
Contents =
263+
[
264+
new ErrorContent(errorUpdate.Message)
265+
{
266+
ErrorCode = errorUpdate.Code,
267+
Details = errorUpdate.Param,
268+
}
269+
],
270+
};
271+
break;
249272
}
250273
}
251274
}

test/Libraries/Microsoft.Extensions.AI.Abstractions.Tests/Contents/ErrorContentTests.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,19 @@ namespace Microsoft.Extensions.AI;
88

99
public class ErrorContentTests
1010
{
11+
[Fact]
12+
public void Constructor_NormalizesNullToEmpty()
13+
{
14+
ErrorContent content = new(null!);
15+
Assert.Empty(content.Message);
16+
17+
content.Message = "test";
18+
Assert.Equal("test", content.Message);
19+
20+
content.Message = null!;
21+
Assert.Empty(content.Message);
22+
}
23+
1124
[Fact]
1225
public void Constructor_ShouldInitializeProperties()
1326
{

0 commit comments

Comments
 (0)