# ollama: Don't override model's default stop tokens \(\#48119\) · gitcafe/zed

[View on GitCafe](https://git.cafe/gitcafe/zed/commit/a80b0195bb759894fa42082e7bce4bf731bff90d)

Repository: [gitcafe/zed](https://git.cafe/gitcafe/zed)

Visibility: public

Requested revision: a80b0195bb759894fa42082e7bce4bf731bff90d

Requested commit: a80b0195bb759894fa42082e7bce4bf731bff90d

Commit: a80b0195bb759894fa42082e7bce4bf731bff90d

Tree: 69c70b305c5c140132cc1859651a04693dd1b8f4

Author: little Kitchen

Committer: GitHub

## Message

```
ollama: Don't override model's default stop tokens (#48119)

## Summary

When no stop tokens are provided, Zed was sending an empty array
(`"stop": []`) to Ollama. This caused Ollama to override the model's
default stop tokens (defined in its Modelfile) with nothing, resulting
in models like rnj-1:8b generating infinitely with literal stop tokens
appearing in the output.

## Problem

Models with custom stop tokens in their Modelfile (like `<|eot_id|>` for
rnj-1:8b) would generate forever because:

1. Agent thread creates request with `stop: Vec::new()` (empty)
2. Ollama provider converts this to `stop: Some(vec![])`
3. Serializes as `"stop": []` in JSON
4. Ollama interprets this as "override default stop tokens with nothing"
5. Model generates forever, outputting stop tokens as literal text

## Solution

1. In `crates/language_models/src/provider/ollama.rs`:
   - Only send `stop` when explicitly provided
   - When empty, use `None` so the field is omitted from JSON
   
2. In `crates/ollama/src/ollama.rs`:
- Add `#[serde(skip_serializing_if = "Option::is_none")]` to all
`ChatOptions` fields
   - Ensures `None` values are omitted, not serialized as `null`

## Testing

Added 4 new tests in `crates/ollama/src/ollama.rs`:
- `test_chat_options_serialization`: Verifies None fields are omitted
- `test_chat_request_with_stop_tokens`: Verifies stop tokens are
serialized when provided
- `test_chat_request_without_stop_tokens_omits_field`: Verifies empty
stop is omitted

All 11 ollama tests pass, plus 1 language_models ollama test.

Fixes #47798

Release Notes:

- Fixed Ollama models with custom stop tokens generating infinitely by
not overriding model defaults when no stop tokens are specified.
```

## Parents

- [905d28cc5406860984db6dce1e947773a2c68bfb](https://git.cafe/gitcafe/zed/commit/905d28cc5406860984db6dce1e947773a2c68bfb?format=markdown)

[Source at this commit](https://git.cafe/gitcafe/zed/tree/a80b0195bb759894fa42082e7bce4bf731bff90d?format=markdown)
