This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
This is a fully generated C# SDK for the Ollama API, built using OpenAPI specifications. The core library targets multiple frameworks (netstandard2.0, net4.6.2, net8.0, net9.0) and emphasizes modern .NET features including nullability, trimming, and NativeAOT support.
dotnet build Ollama.slnx# Run all tests
dotnet test Ollama.slnx
# Run tests with GitHub Actions logger
dotnet test Ollama.slnx --logger GitHubActions
# Run a single test
dotnet test --filter "FullyQualifiedName~Tests.Tools"The integration tests are located in src/tests/Ollama.IntegrationTests and use MSTest with Testcontainers to spin up Ollama instances.
CRITICAL: The Generated/ directory (151 files) is auto-generated and should NEVER be manually edited. All generated code is created using the autosdk.cli tool based on the OpenAPI specification.
cd src/libs/Ollama
./generate.shThis script:
- Downloads the latest OpenAPI spec from langchain_dart repository
- Runs
FixOpenApiSpechelper to preprocess the spec - Generates C# code using
autosdk generatecommand
Generated Code (src/libs/Ollama/Generated/):
OllamaApiClient.g.cs- Main client with endpoint clients as properties- Endpoint clients:
CompletionsClient,ChatClient,EmbeddingsClient,ModelsClient - All API models, enums, converters, and serialization contexts
- Generated from
openapi.yamlusing AutoSDK
Manual Code (files at root of src/libs/Ollama/):
Chat.cs- High-level chat abstraction with conversation history and tool callingOllamaApiClientExtensions.cs- Extension methods for improved ergonomics:Chat()- Creates a new Chat instanceWaitAsync()- Combines streaming responses into single responsesEnsureSuccessAsync()- Ensures model pull operations succeed
Message.cs- Partial class adding implicit string-to-Message conversionExtensions/StringExtensions.cs- Helper methods for creating messages and converting tools:AsUserMessage(),AsSystemMessage(),AsToolMessage(),AsAssistantMessage()AsOllamaTools()- Converts CSharpToJsonSchema tools to Ollama formatAsJson()- Serializes objects using source-generated context
The OllamaApiClient exposes endpoint clients as properties:
client.Completions- For completion generationclient.Chat- For chat completionsclient.Embeddings- For embeddingsclient.Models- For model management (list, pull, push, delete, etc.)
Key pattern: Most API methods return IAsyncEnumerable<T> for streaming. Extension methods in OllamaApiClientExtensions.cs provide:
WaitAsync()methods that aggregate streams into single responsesGetAwaiter()methods enabling directawaiton enumerables- This allows both streaming (
await foreach) and non-streaming (await) usage
The Chat class provides:
- Conversation history management
- Automatic tool calling when
AutoCallTools = true - Tool registration via
AddToolService() - Simplified API via
SendAsync()
Uses CSharpToJsonSchema for defining tools through C# interfaces:
- Define interface with
[GenerateJsonSchema]attribute - Use
[Description]attributes for function/parameter descriptions - Convert using
service.AsTools().AsOllamaTools() - Register calls using
service.AsCalls()
See src/tests/Ollama.IntegrationTests/WeatherTools.cs for complete example.
src/helpers/FixOpenApiSpec/- Preprocesses OpenAPI spec before generationsrc/helpers/GenerateDocs/- Documentation generation utilitysrc/helpers/TrimmingHelper/- Tests trimming/NativeAOT compatibility
src/libs/Ollama/openapi.yaml- OpenAPI specification sourcesrc/libs/Ollama/generate.sh- Code generation scriptsrc/libs/Ollama/Ollama.csproj- Main library project with multi-targetingsrc/Directory.Build.props- Shared build configuration
Key runtime dependencies:
System.Text.Json(9.0.8) - JSON serializationCSharpToJsonSchema(3.10.1) - Tool definition source generatorPolySharp(1.15.0) - Polyfills for older frameworks
Test dependencies:
- MSTest for test framework
- Testcontainers for integration tests
- AwesomeAssertions for fluent assertions