Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

System.ClientModel.ClientResultException: 'HTTP 400 (invalid_request_error: ) #30

Closed
cjkarande opened this issue Jun 9, 2024 · 7 comments · Fixed by #58
Closed

System.ClientModel.ClientResultException: 'HTTP 400 (invalid_request_error: ) #30

cjkarande opened this issue Jun 9, 2024 · 7 comments · Fixed by #58
Labels
bug Something isn't working

Comments

@cjkarande
Copy link

I am using the following code in a .NET Standard 2.0 project within a Xamarin.Forms Solution
string _model = "gpt-3.5-turbo";
_openAI_Client = new ChatClient(model: _model, _apiKey);
ChatCompletion chatCompletion = await _openAI_Client.CompleteChatAsync(new ChatMessage[] {
new UserChatMessage("Deep Reinforcement Learning for Finance")
});

however I am receiving the following exception on the execution of CompleteChatAsync()

System.ClientModel.ClientResultException: 'HTTP 400 (invalid_request_error: )
Parameter: messages.[0].content
Invalid value for 'content': expected a string, got null.'

@cjkarande
Copy link
Author

cjkarande commented Jun 11, 2024

@joseharriaga
@hed-openai @mjr-openai @ram-openai @kimo-openai @srw-openai

Anyone from the OpenAI team who could have a look at this issue pls? Its a show stopper

@joseharriaga
Copy link
Collaborator

@trrwilson: Is this related to the issue that Chris and you were seeing with Mono?

@trrwilson
Copy link
Collaborator

@joseharriaga

@trrwilson: Is this related to the issue that Chris and you were seeing with Mono?

Almost certainly the same issue, yes. There's an oddity with how serialization covariance is handled on Mono with System.ClientModel's use of ModelReaderWriter on IJsonModel<T> when there's an abstract base type; in this case, the message is being written as IJsonModel<ChatMessage> (which has no "content" to speak of) instead of the intended derived instance type of UserChatMessage.

The problem is well understood, but we should consider expediting the workaround (of using virtual methods for these abstract serialization paths) in the interim.

@rudetrue
Copy link

Experiencing this as well. Let me know if I can try/test anything to work towards a fix.

@MoienTajik
Copy link

MoienTajik commented Jun 12, 2024

Same issue here on Blazor WASM. As a temporary workaround, you can use:

var json = BinaryData.FromObjectAsJson(new
{
  model = "gpt-3.5-turbo",
  messages = new[]
  {
    new
    {
      role = "user",
      content ="YOUR_PROMPT"
    }
  }
});

var clientResult = await chatClient.CompleteChatAsync(BinaryContent.Create(json));
var chatCompletion = ModelReaderWriter.Read<ChatCompletion>(clientResult.GetRawResponse().Content, new("W"));

@trrwilson trrwilson added the bug Something isn't working label Jun 12, 2024
@trrwilson
Copy link
Collaborator

To update; we've filed an issue with the .NET runtime team (thanks to @chschrae for investigating) to track the full fix for the underlying issue causing this problem:

dotnet/runtime#103365

We're evaluating a mitigation option to unblock mono/wasm/etc. in the interim.

@cjkarande
Copy link
Author

@trrwilson, thanks a ton for the fix, it indeed was an issue with the generic interface across different runtimes.
The issue is now resolved in the latest (2.0.0-beta.5) Nuget update.

Appreciate the team's prompt intervention

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants