LmStudioSharp 1.0.0
See the version list below for details.
dotnet add package LmStudioSharp --version 1.0.0
NuGet\Install-Package LmStudioSharp -Version 1.0.0
<PackageReference Include="LmStudioSharp" Version="1.0.0" />
<PackageVersion Include="LmStudioSharp" Version="1.0.0" />
<PackageReference Include="LmStudioSharp" />
paket add LmStudioSharp --version 1.0.0
#r "nuget: LmStudioSharp, 1.0.0"
#:package LmStudioSharp@1.0.0
#addin nuget:?package=LmStudioSharp&version=1.0.0
#tool nuget:?package=LmStudioSharp&version=1.0.0
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
StreamAsyncto receiveLmStudioStreamChunkvalues as the model responds. - Chunks are yielded in the order received from LM Studio.
ReasoningandOutputchunks may interleave depending on model behavior.- A
ResponseIdchunk is emitted when the server includes an ID. - A
Warningchunk 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})");
}
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), setAllowInsecureHttp = trueinLmStudioClientOptions.
Error Handling
StreamAsyncthrowsLmStudioRequestExceptionon HTTP failures.RunAsyncreturnsnullwhen the response cannot be parsed or the request fails.RunAsync,RunForResponseAsync, andGetModelsAsyncthrowLmStudioAuthenticationExceptionfor missing/invalid bearer tokens.GetModelsAsyncthrowsLmStudioRequestExceptionon HTTP failures.
Notes
- Use the app layer to implement prompts, tool routing, and any persistence.
- Inject your own
HttpClientif 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 | 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 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. |
-
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.