Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System.Diagnostics;
using System.Text.Json.Serialization;
using Microsoft.Shared.Diagnostics;
using System.Diagnostics.CodeAnalysis;

namespace Microsoft.Extensions.AI;

Expand All @@ -16,33 +15,33 @@ namespace Microsoft.Extensions.AI;
public class ErrorContent : AIContent
{
/// <summary>The error message.</summary>
private string _message;
private string? _message;

/// <summary>Initializes a new instance of the <see cref="ErrorContent"/> class with the specified message.</summary>
/// <param name="message">The message to store in this content.</param>
[JsonConstructor]
public ErrorContent(string message)
/// <summary>Initializes a new instance of the <see cref="ErrorContent"/> class with the specified error message.</summary>
/// <param name="message">The error message to store in this content.</param>
public ErrorContent(string? message)
{
_message = Throw.IfNull(message);
_message = message;
}

/// <summary>Gets or sets the error message.</summary>
[AllowNull]
public string Message
{
get => _message;
set => _message = Throw.IfNull(value);
get => _message ?? string.Empty;
set => _message = value;
}

/// <summary>Gets or sets the error code.</summary>
/// <summary>Gets or sets an error code associated with the error.</summary>
public string? ErrorCode { get; set; }

/// <summary>Gets or sets the error details.</summary>
/// <summary>Gets or sets additional details about the error.</summary>
public string? Details { get; set; }

/// <summary>Gets a string representing this instance to display in the debugger.</summary>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private string DebuggerDisplay =>
$"Error = {Message}" +
(ErrorCode is not null ? $" ({ErrorCode})" : string.Empty) +
(Details is not null ? $" - {Details}" : string.Empty);
$"Error = \"{Message}\"" +
(!string.IsNullOrWhiteSpace(ErrorCode) ? $" ({ErrorCode})" : string.Empty) +
(!string.IsNullOrWhiteSpace(Details) ? $" - \"{Details}\"" : string.Empty);
}
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,11 @@ public async Task<ChatResponse> GetResponseAsync(
break;
}
}

if (openAIResponse.Error is { } error)
{
message.Contents.Add(new ErrorContent(error.Message) { ErrorCode = error.Code });
}
}

return response;
Expand Down Expand Up @@ -246,6 +251,24 @@ public async IAsyncEnumerable<ChatResponseUpdate> GetStreamingResponseAsync(

break;
}

case StreamingResponseErrorUpdate errorUpdate:
yield return new ChatResponseUpdate
{
CreatedAt = createdAt,
MessageId = lastMessageId,
ModelId = modelId,
ResponseId = responseId,
Contents =
[
new ErrorContent(errorUpdate.Message)
{
ErrorCode = errorUpdate.Code,
Details = errorUpdate.Param,
}
],
};
break;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,19 @@ namespace Microsoft.Extensions.AI;

public class ErrorContentTests
{
[Fact]
public void Constructor_NormalizesNullToEmpty()
{
ErrorContent content = new(null!);
Assert.Empty(content.Message);

content.Message = "test";
Assert.Equal("test", content.Message);

content.Message = null!;
Assert.Empty(content.Message);
}

[Fact]
public void Constructor_ShouldInitializeProperties()
{
Expand Down
Loading