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 net10.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 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 official OpenAPI spec from the Ollama repository
- Writes it to
src/libs/Ollama/openapi.yaml - Generates C# code using
autosdk generate
Generated Code (src/libs/Ollama/Generated/):
OllamaClient.g.cs- Main client- Flat endpoint methods such as
GenerateAsync(),GenerateAsStreamAsync(),ChatAsync(),ChatAsStreamAsync(),PullAsync(),PullAsStreamAsync(),EmbedAsync(), andListAsync() - 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 callingOllamaClientExtensions.cs- Extension methods for improved ergonomics:Chat()- Creates a new Chat instanceWaitAsync()- Combines streaming responses into single responsesEnsureSuccessAsync()- Ensures streamed model operations succeed
ChatMessage.cs- Partial class adding implicit string-to-ChatMessageconversionExtensions/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 OllamaClient exposes a flat method surface generated directly from the official Ollama spec:
client.GenerateAsync()/client.GenerateAsStreamAsync()client.ChatAsync()/client.ChatAsStreamAsync()client.PullAsync()/client.PullAsStreamAsync()client.EmbedAsync(),client.ListAsync(),client.ShowAsync(),client.PsAsync(), and other model-management methods
Key pattern: operations that support both JSON and NDJSON have two generated methods. Extension methods in OllamaClientExtensions.cs provide:
WaitAsync()methods that aggregate NDJSON streams into single responsesGetAwaiter()methods enabling directawaiton streamed enumerablesEnsureSuccessAsync()for streamed pull/create/push status events
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.
| Project | Purpose |
|---|---|
src/libs/Ollama/ |
Main SDK library (OllamaClient) |
src/tests/Ollama.IntegrationTests/ |
Integration tests (Testcontainers in CI, local server in debug) |
Tests in src/tests/Ollama.IntegrationTests/Examples are the single source of truth for both test coverage and documentation:
- Each file has a JSDoc header (
order,title,slug) consumed byautosdk docs sync . - Comments prefixed with
////become prose paragraphs in generated docs - CI workflow (
.github/workflows/mkdocs.yml) auto-generatesdocs/examples/and populatesEXAMPLES:START/ENDmarkers in README.md, docs/index.md, and mkdocs.yml - Config:
autosdk.docs.jsonpoints tosrc/tests/Ollama.IntegrationTests/Examples
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