@@ -155,21 +155,22 @@ void IDisposable.Dispose()
155
155
156
156
foreach ( var content in input . Contents )
157
157
{
158
- if ( content is FunctionCallContent callRequest )
158
+ switch ( content )
159
159
{
160
- message . ToolCalls . Add (
161
- ChatToolCall . CreateFunctionToolCall (
162
- callRequest . CallId ,
163
- callRequest . Name ,
164
- new ( JsonSerializer . SerializeToUtf8Bytes (
165
- callRequest . Arguments ,
166
- options . GetTypeInfo ( typeof ( IDictionary < string , object ? > ) ) ) ) ) ) ;
167
- }
168
- }
160
+ case ErrorContent errorContent when errorContent . ErrorCode is nameof ( message . Refusal ) :
161
+ message . Refusal = errorContent . Message ;
162
+ break ;
169
163
170
- if ( input . AdditionalProperties ? . TryGetValue ( nameof ( message . Refusal ) , out string ? refusal ) is true )
171
- {
172
- message . Refusal = refusal ;
164
+ case FunctionCallContent callRequest :
165
+ message . ToolCalls . Add (
166
+ ChatToolCall . CreateFunctionToolCall (
167
+ callRequest . CallId ,
168
+ callRequest . Name ,
169
+ new ( JsonSerializer . SerializeToUtf8Bytes (
170
+ callRequest . Arguments ,
171
+ options . GetTypeInfo ( typeof ( IDictionary < string , object ? > ) ) ) ) ) ) ;
172
+ break ;
173
+ }
173
174
}
174
175
175
176
yield return message ;
@@ -370,7 +371,7 @@ private static async IAsyncEnumerable<ChatResponseUpdate> FromOpenAIStreamingCha
370
371
// add it to this function calling item.
371
372
if ( refusal is not null )
372
373
{
373
- ( responseUpdate . AdditionalProperties ??= [ ] ) [ nameof ( ChatMessageContentPart . Refusal ) ] = refusal . ToString ( ) ;
374
+ responseUpdate . Contents . Add ( new ErrorContent ( refusal . ToString ( ) ) { ErrorCode = "Refusal" } ) ;
374
375
}
375
376
376
377
// Propagate additional relevant metadata.
@@ -450,6 +451,12 @@ private static ChatResponse FromOpenAIChatCompletion(ChatCompletion openAIComple
450
451
}
451
452
}
452
453
454
+ // And add error content for any refusals, which represent errors in generating output that conforms to a provided schema.
455
+ if ( openAICompletion . Refusal is string refusal )
456
+ {
457
+ returnMessage . Contents . Add ( new ErrorContent ( refusal ) { ErrorCode = nameof ( openAICompletion . Refusal ) } ) ;
458
+ }
459
+
453
460
// Wrap the content in a ChatResponse to return.
454
461
var response = new ChatResponse ( returnMessage )
455
462
{
@@ -470,11 +477,6 @@ private static ChatResponse FromOpenAIChatCompletion(ChatCompletion openAIComple
470
477
( response . AdditionalProperties ??= [ ] ) [ nameof ( openAICompletion . ContentTokenLogProbabilities ) ] = contentTokenLogProbs ;
471
478
}
472
479
473
- if ( openAICompletion . Refusal is string refusal )
474
- {
475
- ( response . AdditionalProperties ??= [ ] ) [ nameof ( openAICompletion . Refusal ) ] = refusal ;
476
- }
477
-
478
480
if ( openAICompletion . RefusalTokenLogProbabilities is { Count : > 0 } refusalTokenLogProbs )
479
481
{
480
482
( response . AdditionalProperties ??= [ ] ) [ nameof ( openAICompletion . RefusalTokenLogProbabilities ) ] = refusalTokenLogProbs ;
0 commit comments