You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
but the .NET OpenAI library doesn't work with it. Gemini is sending back a role of "model" as part of the response, but the OpenAI library is trying to validate that any role is one of only five hardcoded values, and anything else causes the request to fail:
Run the .NET equivalent to the code in that blog post:
using OpenAI;using OpenAI.Chat;varclient=new OpenAIClient(new("gemini_api_key"),new(){Endpoint=new("https://generativelanguage.googleapis.com/v1beta/"),}).GetChatClient("gemini-1.5-flash");varresponse=await client.CompleteChatAsync([ChatMessage.CreateSystemMessage("You are a helpful AI assistant."),ChatMessage.CreateUserMessage("Explain to me how AI works"),]);
Console.WriteLine(response.Value.Content[0].Text);
It fails with:
Unhandled exception. System.ArgumentOutOfRangeException: Unknown ChatMessageRole value. (Parameter 'value')
Actual value was model.
at OpenAI.Chat.ChatMessageRoleExtensions.ToChatMessageRole(String value)
at OpenAI.Chat.InternalChatCompletionResponseMessage.DeserializeInternalChatCompletionResponseMessage(JsonElement element, ModelReaderWriterOptions options)
at OpenAI.Chat.InternalCreateChatCompletionResponseChoice.DeserializeInternalCreateChatCompletionResponseChoice(JsonElement element, ModelReaderWriterOptions options)
at OpenAI.Chat.ChatCompletion.DeserializeChatCompletion(JsonElement element, ModelReaderWriterOptions options)
at OpenAI.Chat.ChatCompletion.FromResponse(PipelineResponse response)
at OpenAI.Chat.ChatClient.CompleteChatAsync(IEnumerable`1 messages, ChatCompletionOptions options, CancellationToken cancellationToken)
at OpenAI.Chat.ChatClient.CompleteChatAsync(ChatMessage[] messages)
at Program.<Main>$(String[] args)
Code snippets
OS
any
.NET version
.NET 8
Library version
2.1.0-beta.2
The text was updated successfully, but these errors were encountered:
after adding the new role to the Serializer seems to be working fine. i assume this library is generated automatically. but i couldnt find instructions on how to fix the generation code. havent tested anything complex, but this patch seems to work:
--- a/src/Generated/Models/ChatMessageRole.Serialization.cs+++ b/src/Generated/Models/ChatMessageRole.Serialization.cs@@ -15,6 +15,7 @@ namespace OpenAI.Chat
ChatMessageRole.Assistant => "assistant",
ChatMessageRole.Tool => "tool",
ChatMessageRole.Function => "function",
+ ChatMessageRole.Model => "model",
_ => throw new ArgumentOutOfRangeException(nameof(value), value, "Unknown ChatMessageRole value.")
};
@@ -23,6 +24,7 @@ namespace OpenAI.Chat
if (StringComparer.OrdinalIgnoreCase.Equals(value, "system")) return ChatMessageRole.System;
if (StringComparer.OrdinalIgnoreCase.Equals(value, "user")) return ChatMessageRole.User;
if (StringComparer.OrdinalIgnoreCase.Equals(value, "assistant")) return ChatMessageRole.Assistant;
+ if (StringComparer.OrdinalIgnoreCase.Equals(value, "model")) return ChatMessageRole.Model;
if (StringComparer.OrdinalIgnoreCase.Equals(value, "tool")) return ChatMessageRole.Tool;
if (StringComparer.OrdinalIgnoreCase.Equals(value, "function")) return ChatMessageRole.Function;
throw new ArgumentOutOfRangeException(nameof(value), value, "Unknown ChatMessageRole value.");
Service
OpenAI
Describe the bug
Google blogged about how they now expose an OpenAI-compatible endpoint for gemini:
https://developers.googleblog.com/en/gemini-is-now-accessible-from-the-openai-library/
but the .NET OpenAI library doesn't work with it. Gemini is sending back a role of "model" as part of the response, but the OpenAI library is trying to validate that any role is one of only five hardcoded values, and anything else causes the request to fail:
openai-dotnet/src/Generated/Models/ChatMessageRole.Serialization.cs
Lines 21 to 29 in 539172f
Steps to reproduce
Run the .NET equivalent to the code in that blog post:
It fails with:
Code snippets
OS
any
.NET version
.NET 8
Library version
2.1.0-beta.2
The text was updated successfully, but these errors were encountered: