ollama: Resolve context window size via API (#39941)

Previously we were guessing the context window size here:
https://github.com/zed-industries/zed/blob/8c3f09e31e3588a2494042dfe77bb3dccab1f7ba/crates/ollama/src/ollama.rs#L22

This is inaccurate and must be updated manually. This PR ensures that we
extract the context window size from the request in the same way that
the Ollama CLI does when running `ollama show <model-name>` (Relevant
code is
[here](https://github.com/ollama/ollama/blob/3d32249c749c6f77c1dc8a7cb55ae74fc2f4c08b/cmd/cmd.go#L860))

The format looks like this:

```json
{
  "model_info": {
    "general.architecture": "llama",
    "llama.context_length": 132000
  }
}
```

Once this PR is merged we could technically remove the old code
https://github.com/zed-industries/zed/blob/8c3f09e31e3588a2494042dfe77bb3dccab1f7ba/crates/ollama/src/ollama.rs#L22
I decided to keep it for now, as it is unclear if the necessary fields
are available via the API on older Ollama versions.

Release Notes:

- Fixed an issue where Ollama models would use the wrong context window
size
This commit is contained in:
Bennet Bo Fenner
2025-10-10 12:59:52 +00:00
committed by GitHub
parent 4dae3a15cc
commit 3d5ddcccf0
2 changed files with 74 additions and 7 deletions
@@ -119,16 +119,16 @@ impl State {
let api_key = api_key.clone();
async move {
let name = model.name.as_str();
let capabilities =
let model =
show_model(http_client.as_ref(), &api_url, api_key.as_deref(), name)
.await?;
let ollama_model = ollama::Model::new(
name,
None,
None,
Some(capabilities.supports_tools()),
Some(capabilities.supports_vision()),
Some(capabilities.supports_thinking()),
model.context_length,
Some(model.supports_tools()),
Some(model.supports_vision()),
Some(model.supports_thinking()),
);
Ok(ollama_model)
}