LmStudioSharp 1.2.0

dotnet add package LmStudioSharp --version 1.2.0
                    
NuGet\Install-Package LmStudioSharp -Version 1.2.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="LmStudioSharp" Version="1.2.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="LmStudioSharp" Version="1.2.0" />
                    
Directory.Packages.props
<PackageReference Include="LmStudioSharp" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add LmStudioSharp --version 1.2.0
                    
#r "nuget: LmStudioSharp, 1.2.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package LmStudioSharp@1.2.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=LmStudioSharp&version=1.2.0
                    
Install as a Cake Addin
#tool nuget:?package=LmStudioSharp&version=1.2.0
                    
Install as a Cake Tool

LmStudioSharp

Lightweight .NET client for LM Studio's chat API. This library is transport-only: it manages HTTP, SSE streaming, and response parsing. Prompts, tool routing, and app-specific behavior should live in the calling application.

Portions of this project were developed with LLMs such as Codex 5.2, GLM 4.7, and Qwen3.

LM Studio REST API docs: https://lmstudio.ai/docs/developer/rest

Installation

dotnet add package LmStudioSharp

For the latest prerelease:

dotnet add package LmStudioSharp --prerelease

Quick Start

using LmStudioSharp;

var options = new LmStudioClientOptions
{
    Host = "http://localhost:1234",
    BearerToken = "",
};

using var client = new LmStudioClient(options);

var request = new LmStudioRequest
{
    Model = "zai-org/glm-4.7-flash",
    SystemPrompt = "You are helpful.",
    Input = "Hello!",
    Stream = true,
};

await foreach (var chunk in client.StreamAsync(request))
{
    switch (chunk.Kind)
    {
        case LmStudioStreamChunkKind.Reasoning:
        case LmStudioStreamChunkKind.Output:
            Console.Write(chunk.Content);
            break;
        case LmStudioStreamChunkKind.ResponseId:
            Console.WriteLine($"\nResponse ID: {chunk.ResponseId}");
            break;
        case LmStudioStreamChunkKind.Warning:
            Console.WriteLine($"\n[warning] {chunk.Content}");
            break;
    }
}

Streaming

  • Use StreamAsync to receive LmStudioStreamChunk values as the model responds.
  • Chunks are yielded in the order received from LM Studio.
  • Reasoning and Output chunks may interleave depending on model behavior.
  • A ResponseId chunk is emitted when the server includes an ID.
  • A Warning chunk is emitted for unusual stream conditions (for example repeating output chunks or no payload).

Non-Streaming Usage

request.Stream = false;
var result = await client.RunAsync(request);
if (result is not null)
{
    Console.WriteLine(result.Output);
}

If you need access to the raw response (including stats), use RunForResponseAsync:

request.Stream = false;
var response = await client.RunForResponseAsync(request);
if (response is not null)
{
    Console.WriteLine(response.Stats?.TotalOutputTokens);
}

Listing Models

var models = await client.GetModelsAsync();
foreach (var model in models?.Models ?? [])
{
    Console.WriteLine($"{model.DisplayName} ({model.Key})");
}

Image Input (VLM)

Vision-Language Models (VLMs) can accept images as input. Set Input to a list of LmStudioInputItem objects:

var request = new LmStudioRequest
{
    Model = "qwen/qwen3-vl-4b",
    Input = new List<LmStudioInputItem>
    {
        new LmStudioTextInput("Describe this image"),
        LmStudioImageHelper.LoadImage("path/to/image.png")
    },
    Stream = true,
};

Load images asynchronously:

var image = await LmStudioImageHelper.LoadImageAsync("photo.jpg");

Or from a base64 string:

var image = LmStudioImageHelper.FromBase64(base64String, "image/png");

Supported formats: PNG, JPEG, BMP, WebP, GIF.

Security

Authentication

Bearer tokens are only required if your LM Studio server requires authentication. If enabled, set BearerToken in LmStudioClientOptions and handle LmStudioAuthenticationException for missing/invalid tokens.

Transport

  • Prefer https:// hosts.
  • http:// is allowed by default only for loopback hosts (localhost, 127.0.0.1, ::1).
  • For non-loopback HTTP hosts (for example http://192.168.0.123), set AllowInsecureHttp = true in LmStudioClientOptions.

Error Handling

  • StreamAsync throws LmStudioRequestException on HTTP failures.
  • RunAsync returns null when the response cannot be parsed or the request fails.
  • RunAsync, RunForResponseAsync, and GetModelsAsync throw LmStudioAuthenticationException for missing/invalid bearer tokens.
  • GetModelsAsync throws LmStudioRequestException on HTTP failures.

Notes

  • Use the app layer to implement prompts, tool routing, and any persistence.
  • Inject your own HttpClient if you need custom handlers or lifetime management.

Releases

Publishing a GitHub Release triggers the Release Build workflow in .github/workflows/release.yml. That workflow builds the library in Release mode and attaches a zip bundle (DLL + symbols/docs when present) to the release.

Contributing

Pull requests and issues are open and accepted on this project.

Product 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 was computed.  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 was computed.  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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net8.0

    • No dependencies.

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
1.2.0 178 7/13/2026
1.1.1 122 7/12/2026
1.1.0 124 7/11/2026
1.0.1 689 2/18/2026
1.0.0 125 2/17/2026
0.2.1-pre 120 2/12/2026