LightResults.Extensions.OpenAI
10.0.1
Prefix Reserved
dotnet add package LightResults.Extensions.OpenAI --version 10.0.1
NuGet\Install-Package LightResults.Extensions.OpenAI -Version 10.0.1
<PackageReference Include="LightResults.Extensions.OpenAI" Version="10.0.1" />
<PackageVersion Include="LightResults.Extensions.OpenAI" Version="10.0.1" />
<PackageReference Include="LightResults.Extensions.OpenAI" />
paket add LightResults.Extensions.OpenAI --version 10.0.1
#r "nuget: LightResults.Extensions.OpenAI, 10.0.1"
#:package LightResults.Extensions.OpenAI@10.0.1
#addin nuget:?package=LightResults.Extensions.OpenAI&version=10.0.1
#tool nuget:?package=LightResults.Extensions.OpenAI&version=10.0.1
LightResults Extensions
Extensions for LightResults, an extremely light and modern Operation Result Pattern library for .NET.
OpenAI
Provides comprehensive an OpenAI (v2.7.0) integration with the Result pattern, offering extension methods that return Result<T> instead of throwing exceptions.
Documentation
Make sure to read the docs for the full API.
Try overloads
This package provides a Try extension method version of all public methods for all OpenAI clients, wrapping operations in a try { } catch { } block. If an exception occurs, a failed result will be returned and the Exception will be added to the result as metadata.
Supported Clients:
- Assistants -
AssistantClientextensions for AI assistants - Audio Processing -
AudioClientextensions for speech-to-text and text-to-speech - Batch Processing -
BatchClientextensions for batch operations - Chat Completions -
ChatClientextensions for chat completions and streaming - Conversations -
ConversationClientextensions for conversations - Embeddings -
EmbeddingClientextensions for text embeddings - Evaluations -
EvaluationClientextensions for model evaluations - File Operations -
OpenAIFileClientextensions for file uploads and management - Fine-tuning -
FineTuningClientextensions for model fine-tuning - Image Generation -
ImageClientextensions for image generation and editing - Responses -
ResponsesClientextensions for responses and streaming - Vector Stores -
VectorStoreClientextensions for vector storage and retrieval
Usage Examples
Chat Completions
using OpenAI.Chat;
using LightResults.Extensions.OpenAI;
var client = new ChatClient("gpt-4o", Environment.GetEnvironmentVariable("OPENAI_API_KEY"));
// Simple chat completion
var result = await client.TryCompleteChatAsync("What is the meaning of life?");
if (result.IsSuccess(out var completion))
{
Console.WriteLine($"Response: {completion.Content[0].Text}");
}
else if (result.IsFailure(out var error))
{
Console.WriteLine($"Error: {error.Message}");
}
// Chat with multiple messages
var messages = new List<ChatMessage>
{
new SystemChatMessage("You are a helpful assistant."),
new UserChatMessage("Explain quantum computing in simple terms.")
};
var chatResult = await client.TryCompleteChatAsync(messages);
if (chatResult.IsSuccess(out var chatCompletion))
{
Console.WriteLine($"Response: {chatCompletion.Content[0].Text}");
}
Embeddings
using OpenAI.Embeddings;
using LightResults.Extensions.OpenAI;
var embeddingClient = new EmbeddingClient("text-embedding-3-small", apiKey);
// Generate single embedding
var embeddingResult = await embeddingClient.TryGenerateEmbeddingAsync("Hello, world!");
if (embeddingResult.IsSuccess(out var embedding))
{
var vector = embedding.ToFloats();
Console.WriteLine($"Generated embedding with {vector.Length} dimensions");
}
// Generate multiple embeddings
var texts = new[] { "First text", "Second text", "Third text" };
var embeddingsResult = await embeddingClient.TryGenerateEmbeddingsAsync(texts);
if (embeddingsResult.IsSuccess(out var embeddings))
{
Console.WriteLine($"Generated {embeddings.Count} embeddings");
}
Image Generation
using OpenAI.Images;
using LightResults.Extensions.OpenAI;
var imageClient = new ImageClient("dall-e-3", apiKey);
// Generate image
var imageResult = await imageClient.TryGenerateImageAsync(
"A serene landscape with mountains and a lake at sunset");
if (imageResult.IsSuccess(out var image))
{
Console.WriteLine($"Generated image URL: {image.ImageUri}");
}
// Generate multiple images
var imagesResult = await imageClient.TryGenerateImagesAsync(
"A cute cat wearing a hat", imageCount: 2);
if (imagesResult.IsSuccess(out var images))
{
foreach (var generatedImage in images)
{
Console.WriteLine($"Image URL: {generatedImage.ImageUri}");
}
}
Audio Processing
using OpenAI.Audio;
using LightResults.Extensions.OpenAI;
var audioClient = new AudioClient("whisper-1", apiKey);
// Transcribe audio
var audioData = File.ReadAllBytes("audio.mp3");
var transcriptionResult = await audioClient.TryTranscribeAudioAsync(audioData, "audio.mp3");
if (transcriptionResult.IsSuccess(out var transcription))
{
Console.WriteLine($"Transcription: {transcription.Text}");
}
// Generate speech
var speechClient = new AudioClient("tts-1", apiKey);
var speechResult = await speechClient.TryGenerateSpeechAsync("Hello, world!", GeneratedSpeechVoice.Alloy);
if (speechResult.IsSuccess(out var speechData))
{
await File.WriteAllBytesAsync("output.mp3", speechData.ToArray());
}
File Operations
using OpenAI.Files;
using LightResults.Extensions.OpenAI;
var fileClient = new OpenAIFileClient(apiKey);
// Upload file
var fileData = File.ReadAllBytes("document.txt");
var uploadResult = await fileClient.TryUploadFileAsync(fileData, "document.txt", FileUploadPurpose.Assistants);
if (uploadResult.IsSuccess(out var uploadedFile))
{
Console.WriteLine($"Uploaded file ID: {uploadedFile.Id}");
}
// List files
var filesResult = await fileClient.TryGetFilesAsync();
if (filesResult.IsSuccess(out var files))
{
foreach (var file in files)
{
Console.WriteLine($"File: {file.Filename} (ID: {file.Id})");
}
}
Error Handling
All extension methods wrap operations in a try { } catch { } block and return Result<T> types for safe error handling:
var result = await client.TryCompleteChatAsync("Hello!");
if (result.IsFailure(out var error))
{
var ex = error.Exception;
// Handle specific exception types
if (ex is ClientResultException clientException)
{
Console.WriteLine($"API Error: {clientException.Message}");
}
else if (ex is TaskCanceledException)
{
Console.WriteLine("Request was cancelled");
}
else
{
Console.WriteLine($"Unexpected error: {ex?.Message ?? error.Message}");
}
}
Getting the Exception
var result = await client.TryCompleteChatAsync("Hello!");
if (result.IsFailure(out var error))
{
var ex = error.Exception;
// Do something with the base exception type or...
if (ex is ClientResultException clientResultException)
{
// Handle OpenAI-specific errors
Console.WriteLine($"OpenAI API Error: {clientResultException.Message}");
}
}
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 is compatible. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net10.0
- LightResults (>= 10.0.1)
- LightResults.Extensions.ExceptionHandling (>= 10.0.0)
- OpenAI (>= 2.8.0)
-
net8.0
- LightResults (>= 10.0.1)
- LightResults.Extensions.ExceptionHandling (>= 10.0.0)
- OpenAI (>= 2.8.0)
-
net9.0
- LightResults (>= 10.0.1)
- LightResults.Extensions.ExceptionHandling (>= 10.0.0)
- OpenAI (>= 2.8.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 10.0.1 | 438 | 12/14/2025 |
| 10.0.0 | 940 | 11/22/2025 |
| 10.0.0-preview.1 | 407 | 11/12/2025 |
| 9.0.0-preview.1 | 708 | 7/21/2025 |
