using System.Threading.Tasks;
using System.Collections.Generic;
using Newtonsoft.Json.Schema;
using Newtonsoft.Json.Schema.Generation;
public class CompletionsResult<T>
public List<object> Content { get; set; }
public T Result { get; set; }
public class AnthropicProvider
public async Task<CompletionsResult<T>> GetCompletionsAsync<T>(string modelId, List<string> promptMessages)
JSchemaGenerator generator = new JSchemaGenerator();
JSchema schema = generator.Generate(typeof(T));
var results = await GetCompletionsAsync(modelId, promptMessages, schema.ToString());
return new CompletionsResult<T>()
Content = results.Content,
Result = JsonConvert.DeserializeObject<T>(results.Result)
public async Task<CompletionsResult<string>> GetCompletionsAsync(string modelId, List<string> promptMessages, string jsonOutputSchema = null)
return new CompletionsResult<string>() { Result = JsonConvert.SerializeObject(new MyOutputType { Id = 1, Name = "test-user" }) };
public class MyOutputType
public int Id { get; set; }
public string Name { get; set; }
static async Task Main(string[] args)
var s = new AnthropicProvider();
var output1 = await s.GetCompletionsAsync("model1", new List<string> { "msg1", "msg2" });
Console.WriteLine($"output1: return type: {output1.GetType()} \nreturn type.Result: {output1.Result.GetType()}\n");
var output2 = await s.GetCompletionsAsync<MyOutputType>("model1", new List<string> { "msg1", "msg2" });
Console.WriteLine($"output2: return type: {output2.GetType()} \nreturn type.Result: {output2.Result.GetType()}");