Skip to content

feat(llm): add an OpenAI-compatible provider with a configurable base URL - #88

Merged
goldbarth merged 1 commit into
mainfrom
feat/78-openai-compatible-provider
Aug 3, 2026
Merged

goldbarth merged 1 commit into
mainfrom
feat/78-openai-compatible-provider

Conversation

@goldbarth

Copy link
Copy Markdown
Owner

Closes #78.

llm.provider accepted one value, so the key documented a choice the user did not have. It now accepts openai-compatible, which reaches any endpoint speaking the OpenAI chat-completions dialect - hosted ones that are cheaper, and a server on the user's own machine, where release data never leaves it.

What

  • LlmProvider + LlmProviderParser, in the shape ThinkingMode already uses. An unknown name throws rather than falling back, because silently reaching a different provider would send release data somewhere the user did not choose.
  • LlmProviderDefaults takes the defaults out of the property initializers. A default that is right for one provider is wrong for the other.
  • llm.baseUrl, applying to both providers - AnthropicClient.BaseUrl exists, and a proxy in front of the Anthropic API is the same need.
  • OpenAiCompatibleChatClient is the only place the OpenAI package is named, which is what keeps it out of the domain.

Decisions worth disagreeing with

Neither model nor baseUrl has a default for openai-compatible. The models an endpoint serves are its own. The endpoint is the more deliberate omission: a default would have to be a hosted one, and this provider exists so release data can stay on the user's machine - anyone configuring only the model would then send it off the machine without being asked. Both errors name the missing key and give an example.

A missing API key builds a client anyway. Ollama, LM Studio, llama.cpp and vLLM do not read the Authorization header. ApiKeyCredential rejects an empty string in its constructor, so a placeholder stands in - otherwise "no key needed" becomes a crash before any request goes out. A hosted endpoint sees the placeholder and answers 401, which is the point: the refusal comes from the endpoint, as it already did for Anthropic.

thinking is refused rather than dropped. It travels in an Anthropic request type. Handing that to another client fails on the first call; refusing at startup fails where the setting was made.

Native AOT

Trimmer publish, linux-x64, self-contained, on 2026-08-03:

baseline with this change delta
trim warnings 1230 1230 0
publish size 29.70 MB 31.40 MB +1.63 MiB (+5.7%)

This inverts the expectation in the issue. Of the 1230 warnings, 1204 come from Anthropic.* and 24 from YamlDotNet; this package adds none, because the OpenAI SDK serializes through System.ClientModel with hand-written readers rather than reflection. The only new transitive dependency on net10.0 is System.ClientModel - not Azure.Core. The already-shipped provider SDK is the AOT blocker, not this one.

Caveat: a trimmer run is not a native AOT run. clang is absent on the machine, so there is no native binary size and IL3050 from package assemblies is unmeasured.

Two things found by running it

NetworkTimeout raised to 10 minutes. The SDK default is 100 seconds - fine for a hosted API, wrong for the case this provider exists for. Against a local 14B model it killed two of three audience texts, each after four retries.

The base URL scheme is checked, not just the parse. localhost:11434 parses as an absolute URI whose scheme is localhost and whose path is 11434. A forgotten http:// would have surfaced far from the setting that caused it.

Smoke runs

Three runs against Ollama 0.21.0 on 2026-08-03, v0.1.0 of this repository:

Model Context Thorough claims Rule-based claims Tokens
qwen2.5-coder:7b 4096 (default) 3 2 26,232
qwen2.5:14b 4096 (default) 0 8 26,303
qwen2.5:14b 24576 1 34 78,448

The first two measured a broken configuration rather than the models. Ollama truncated every prompt from ~12,400 tokens to 4,096, and since the instruction sits at the front while truncation keeps the end, the model received the text under review with neither the facts nor the task. Raising the context produced a usable changelog from the same model.

Two findings were filed rather than fixed here: #85 (a truncated prompt must fail the run) and #86 (a thorough check that never saw the facts must not report as clean).

AC 6 is only half met. The hosted smoke run is outstanding - it needs a key, and it is the one that exercises the real key path, HTTPS, and an endpoint that does not enforce the schema by constrained decoding. Recorded as an open item on #78 rather than quietly dropped.

On #79

Its trigger did not fire: Ollama held the JSON schema on every call. Precisely, though - it enforces the schema by constrained decoding rather than the model following it; a request for a poem, sent with the verdict schema, came back as a valid verdict. The poor text quality is not a #79 trigger, since a native adapter would not have changed it. That was the context window. One runner, two models: LM Studio, llama.cpp and vLLM stay unverified.

Tests

12 new, 315 green. No test covers NetworkTimeout - it is an SDK-internal option observable only through a genuinely slow response, and a test reproducing that would tie the suite's runtime to a timeout.

🤖 Generated with Claude Code

… URL

`llm.provider` accepted one value, so the key documented a choice the user
did not have. It now accepts `openai-compatible` alongside `anthropic`, which
reaches any endpoint speaking the OpenAI chat-completions dialect: hosted ones
that are cheaper, and servers on the user's own machine, where release data
never leaves it. Ollama, LM Studio, llama.cpp and vLLM all serve that dialect,
so one adapter covers them rather than one per runner.

Defaults move out of the property initializers into `LlmProviderDefaults`,
because a default that is right for one provider is wrong for the other. For
`openai-compatible` neither `model` nor `baseUrl` has one: the models an
endpoint serves are its own, and an endpoint default would have to be a hosted
one, which would send release data off the machine for anyone who configured
only the model. Both failures name the key they are missing.

`baseUrl` applies to both providers, since `AnthropicClient.BaseUrl` exists and
a proxy in front of the Anthropic API is the same need.

A missing API key is tolerated. The endpoints this exists for do not read the
Authorization header at all, and `ApiKeyCredential` rejects an empty string in
its constructor, so a placeholder stands in rather than letting "no key needed"
crash before a request goes out. A hosted endpoint sees the placeholder and
answers 401, which is the intended outcome: the refusal comes from the endpoint.

`thinking` is refused for this provider at startup instead of being dropped
silently. It travels in an Anthropic request type, so passing it to any other
client would fail on the first call rather than while wiring up.

Two things found by running it rather than by reading it:

- `NetworkTimeout` is raised to 10 minutes. The SDK default of 100 seconds
  suits a hosted API and fails the case this provider exists for; against a
  local 14B model it killed two of three audience texts, each after four
  retries.
- `localhost:11434` parses as an absolute URI whose scheme is `localhost`, so
  the scheme is checked too. A forgotten http:// would otherwise surface far
  from the setting that caused it.

`Chartula.Core` is untouched. The package is referenced by `Chartula.Cli`
alone, and costs no trim warnings: of the 1230 in a trimmer publish, 1204 come
from the Anthropic SDK and none from this one.

Closes #78
@goldbarth

Copy link
Copy Markdown
Owner Author

Related to #87, which this PR does not close.

The thinking handling here is a stop, not a mapping: for openai-compatible any value other than provider-default fails at config load. That satisfies AC 3 of #87 and none of the other four - llm.thinking keeps Anthropic's semantics under a provider-neutral name, and this PR is what makes that concrete rather than hypothetical.

Also related: #85 and #86 were found while smoke-testing this branch and are now bundled under #94, which is where the work should happen. Both carry release:blocker.

@goldbarth goldbarth self-assigned this Aug 3, 2026
@goldbarth
goldbarth merged commit 69a5755 into main Aug 3, 2026
@goldbarth
goldbarth deleted the feat/78-openai-compatible-provider branch August 3, 2026 14:20
goldbarth added a commit that referenced this pull request Oct 1, 2026
… alone (#319)

Follows #318, which added the ADR index and template.

- **Why:** the reason the domain knows no model provider was argued in
one bullet of `architecture.md`, in `CLAUDE.md`, and implied in
`testing.md`, but recorded nowhere as a decision with its date and its
cost. A contributor who wanted to call a provider feature from
`Chartula.Core` found a rule without the reasons behind it.
- **ADR 0004** records it in the template of #318: decided 2026-07-16 in
#41 (for #2), recorded here.
- **Context:** two operations need a model, both with structured output;
the product promises several providers (#75); a domain tied to one SDK
ties the pipeline to it, and lets a test of the domain reach a real
model, which costs tokens, needs a key and depends on the model.
- **Decision:** `Chartula.Core` references `Microsoft.Extensions.AI`
alone, `ChatModel` sends to an `IChatClient`, the provider packages are
referenced by `Chartula.Cli` alone, and what the domain asks of a model
is expressed in `Microsoft.Extensions.AI` terms. Provider knowledge such
as `ClaudeThinkingSupport` stays in `Cli/Composition`.
- **Consequences, precise about the tests:** `Chartula.Core.Tests`
cannot reach a model by construction. `Chartula.Cli.Tests` does
reference the SDKs, on purpose, and stays offline because its tests
replace the network, not because of its references; the ADR says so
rather than claiming the whole suite is safe by construction. Also: a
provider joins in the composition root (#88 changed no file in
`Chartula.Core`), the domain is limited to what `IChatClient` expresses,
a provider package is weighed for trimming (ADR 0002), and referencing
one from `Core` or `Infrastructure` starts as an issue.
- **`architecture.md`** keeps the choice as one line and links the ADR
instead of arguing it; **`testing.md`** links the ADR for why no Core
test can reach a model.
- The index in `adr/README.md` lists 0004.

**Verified:**
- The references were read from the project files:
`Chartula.Core.csproj` holds `Microsoft.Extensions.AI` and nothing else;
`Anthropic` and `Microsoft.Extensions.AI.OpenAI` are in
`Chartula.Cli.csproj` only; `Chartula.Cli.Tests` references
`Chartula.Cli`.
- The decision date and pull request from `git log -S IChatClient` and
#41's description; the provider promise from the decision comment on
#75; that #88 touched no file in `src/Chartula.Core` from its diff.
- `GetResponseAsync<T>`, `ReasoningOptions` and `UsageDetails` as used
in `ChatModel.cs` and `ChatModelOptions.cs`.
- Every relative link and anchor in all Markdown files of the repository
resolves (script, with a negative test).

Tests: none, documentation only.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add an OpenAI-compatible provider with a configurable base URL

1 participant