Ollama: pass discovered num_ctx in chat requests

_build_ollama_payload sends options.temperature and options.num_predict
to /api/chat, but never options.num_ctx. Ollama defaults num_ctx to 2048
when the option is omitted, so prompts going to any Ollama backend are
silently truncated there regardless of the model's actual capability.

Thread the discovered context length through the three call sites
(llm_call, llm_call_async, stream_llm) and emit options.num_ctx when it
is known and positive. The builder filters out the DEFAULT_CONTEXT
fallback (128000) so we don't lie to Ollama about models whose window
we couldn't actually discover. The issue's literal 'when > 2048'
heuristic is dropped: a model with a real context smaller than 2048
would OOM if Ollama used its default, so we pass the real value
regardless of size. Matches how src/context_compactor.py uses the
same helper.

Sister fix to PR #753 — that PR teaches the compactor the right budget,
this one tells Ollama to actually use that budget on the way in.
This commit is contained in:
Ernest Hysa
2026-06-02 12:27:24 +01:00
committed by GitHub
parent f6b0dcbe58
commit a8a34bd22a
2 changed files with 154 additions and 3 deletions
+127
View File
@@ -113,3 +113,130 @@ def test_ollama_payload_tolerates_malformed_arguments():
payload = llm_core._build_ollama_payload("m", msgs, temperature=0.0, max_tokens=0)
# Falls back to an empty object rather than raising.
assert payload["messages"][0]["tool_calls"][0]["function"]["arguments"] == {}
# ---------------------------------------------------------------------------
# num_ctx threading (issue #909)
#
# Ollama defaults num_ctx to 2048 when the option is omitted, so prompts
# going to any Ollama backend are silently truncated there regardless of
# the model's actual capability. The builder must accept a discovered
# context length and emit options.num_ctx — but only when the value is
# trusted and larger than 2048.
# ---------------------------------------------------------------------------
def test_build_ollama_payload_emits_num_ctx_when_known_and_large():
"""num_ctx passes through when the caller supplies a trusted value
larger than Ollama's 2048 default."""
payload = llm_core._build_ollama_payload(
"kimi-k2", [{"role": "user", "content": "x"}],
temperature=0.5, max_tokens=100, num_ctx=131072,
)
assert payload["options"]["num_ctx"] == 131072
def test_build_ollama_payload_emits_num_ctx_for_small_known_models():
"""A model with a real context smaller than Ollama's 2048 default
would OOM if Ollama used its own default. Pass the real value."""
payload = llm_core._build_ollama_payload(
"tiny-llm", [{"role": "user", "content": "x"}],
temperature=0.5, max_tokens=100, num_ctx=1024,
)
assert payload["options"]["num_ctx"] == 1024
def test_build_ollama_payload_omits_none_and_zero():
"""None means the caller didn't look it up; 0 is nonsensical.
Both should be dropped, not emitted as a 0-context request."""
for ctx in (None, 0):
payload = llm_core._build_ollama_payload(
"m", [{"role": "user", "content": "x"}],
temperature=0.5, max_tokens=100, num_ctx=ctx,
)
assert "num_ctx" not in payload.get("options", {}), (
f"num_ctx={ctx} should not be emitted"
)
def test_build_ollama_payload_omits_default_context_fallback():
"""get_context_length returns DEFAULT_CONTEXT (128000) when it can't
discover the model's actual window. Emitting that as num_ctx would
lie to Ollama for unknown models, so the builder filters it out."""
from src.model_context import DEFAULT_CONTEXT
payload = llm_core._build_ollama_payload(
"unknown-llm-9001", [{"role": "user", "content": "x"}],
temperature=0.5, max_tokens=100, num_ctx=DEFAULT_CONTEXT,
)
assert "num_ctx" not in payload.get("options", {})
def test_llm_call_threads_discovered_num_ctx(monkeypatch):
"""When get_context_length returns a real, large value, it ends up
in the outgoing Ollama request as options.num_ctx (issue #909)."""
monkeypatch.setattr(llm_core, "get_context_length",
lambda url, model: 32768)
seen = {}
def fake_post(url, headers=None, json=None, timeout=None):
seen["json"] = json
request = httpx.Request("POST", url)
return httpx.Response(
200, request=request,
json={"message": {"content": "OK"}, "done": True},
)
monkeypatch.setattr(llm_core.httpx, "post", fake_post)
llm_core.llm_call(
"https://ollama.com/api",
"kimi-k2",
[{"role": "user", "content": "Say OK"}],
temperature=0.2,
max_tokens=7,
)
assert seen["json"]["options"]["num_ctx"] == 32768
def test_stream_llm_threads_discovered_num_ctx(monkeypatch):
"""stream_llm goes through the same ollama branch and must also
pass num_ctx through to the streaming request body."""
import asyncio
seen = {}
def spy_build_ollama_payload(*args, **kwargs):
seen["num_ctx"] = kwargs.get("num_ctx")
seen["stream"] = kwargs.get("stream")
return {
"model": "kimi-k2",
"messages": [{"role": "user", "content": "x"}],
"stream": True,
}
monkeypatch.setattr(llm_core, "get_context_length",
lambda url, model: 32768)
monkeypatch.setattr(llm_core, "_build_ollama_payload",
spy_build_ollama_payload)
# Short-circuit before the actual HTTP call: host is "dead" → yields
# an error SSE chunk and returns. The call to _build_ollama_payload
# still happens before the host check, so we can inspect it.
monkeypatch.setattr(llm_core, "_is_host_dead", lambda url: True)
async def collect():
return [chunk async for chunk in llm_core.stream_llm(
"https://ollama.com/api",
"kimi-k2",
[{"role": "user", "content": "Say OK"}],
temperature=0.2,
max_tokens=7,
)]
out = asyncio.run(collect())
assert seen["num_ctx"] == 32768
assert seen["stream"] is True
assert out # we got the SSE error chunk