Migrating from Legacy OpenAI .NET SDK: What Replaces CompletionRequest in Version 2.1.0?

I’m having trouble after upgrading my OpenAI .NET SDK from version 1.11.0 to 2.1.0. The classes I was using before don’t seem to exist anymore.

In my old implementation, I was using these components that are now missing:

  • CompletionRequest class
  • Completions.CreateCompletionAsync method

Here’s my original working code from the older SDK version:

using OpenAI_API;
using OpenAI_API.Completions;

public class AiContentGenerator : IContentGenerator
{
    private readonly OpenAIAPI _apiClient;
    private readonly IUserValidator _validator;

    public AiContentGenerator(string key, IUserValidator validator)
    {
        _apiClient = new OpenAIAPI(key);
        _validator = validator;
    }

    public async Task<ContentResult> GenerateContentAsync(ContentRequest input)
    {
        var promptRequest = new CompletionRequest
        {
            Prompt = input.UserPrompt,
            Model = input.SelectedModel,
            MaxTokens = input.TokenLimit,
            Temperature = input.Creativity
        };

        var result = await _apiClient.Completions.CreateCompletionAsync(promptRequest);
        var cleanedOutput = result.ToString().Replace("\n", "").Replace("\r", "").Trim('"', ' ');

        return new ContentResult(cleanedOutput, 0, true);
    }
}

After updating to 2.1.0, I tried modifying the code like this:

using System.Threading.Tasks;
using OpenAI;

public class AiContentGenerator : IContentGenerator
{
    private readonly OpenAIClient _openAiClient;
    private readonly IUserValidator _validator;

    public AiContentGenerator(string key, IUserValidator validator)
    {
        _openAiClient = new OpenAIClient(key);
        _validator = validator;
    }

    public async Task<ContentResult> GenerateContentAsync(ContentRequest input)
    {
        var promptRequest = new CompletionRequest
        {
            Prompt = input.UserPrompt,
            Model = input.SelectedModel,
            MaxTokens = input.TokenLimit,
            Temperature = input.Creativity
        };

        var result = await _openAiClient.Completions.CreateCompletionAsync(promptRequest);
        var cleanedOutput = result.ToString().Replace("\n", "").Replace("\r", "").Trim('"', ' ');

        return new ContentResult(cleanedOutput, 0, true);
    }
}

But this doesn’t work because:

  1. CompletionRequest class doesn’t exist
  2. Completions property is not available on OpenAIClient

My Setup:

  • Framework: .NET 9.0
  • Package: OpenAI version 2.1.0
  • Also using: Newtonsoft.Json version 13.0.3 and Rystem.OpenAi version 4.0.5

What I need help with:

  1. How do I make completion requests in OpenAI .NET SDK version 2.1.0?
  2. What classes or methods should I use instead of the old CompletionRequest and Completions.CreateCompletionAsync?
  3. Is there a different way to handle text completions in this newer version?

I’d really appreciate any code examples or guidance on how to properly migrate this functionality. Thanks!

Hey! Hit this same issue last month. The completion API’s deprecated now - you need ChatCompletions instead. Try await _openAiClient.GetChatClient("gpt-3.5-turbo").CompleteChatAsync() and pass your prompt as a user message. Different structure but works great once you’ve got it set up.

This topic was automatically closed 4 days after the last reply. New replies are no longer allowed.