Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

Studio: keep an Ollama Modelfile SYSTEM prompt in force when the date line is on (#10463)

Hugh Scottdb2d0ad84c63
diff --git a/studio/backend/routes/inference.py b/studio/backend/routes/inference.py
index a87c6ad1e71..190298081ce 100644
--- a/studio/backend/routes/inference.py
+++ b/studio/backend/routes/inference.py
@@ -5284,16 +5284,25 @@ def _apply_current_date_prompt(
return f"{date_line}\n\n{system_prompt.lstrip()}" if system_prompt else date_line
+# Ollama applies the Modelfile SYSTEM only when `req.Messages[0].Role != "system"` (its
+# server/routes.go ChatHandler, which /v1/chat/completions also routes into), so only a turn at
+# index 0 displaces it.
+_MODELFILE_SYSTEM_PROVIDERS = frozenset({"ollama"})
+
+
def _prepend_current_date_to_messages(
messages: list[dict],
request: Any = None,
*,
include_api_key: bool = False,
+ provider_type: str | None = None,
) -> list[dict]:
"""Apply the date to an already-built message list for a provider Studio proxies to.
The local path prefixes ``system_prompt`` before the messages exist; an external payload is
- assembled first, so the date goes onto its leading system turn instead.
+ assembled first, so the date goes onto its leading system turn instead. When there is no
+ such turn one is synthesized, except for ``_MODELFILE_SYSTEM_PROVIDERS``, where it is
+ dropped: the caller's silence is what lets the server's own prompt apply.
"""
if request is not None and not _wants_current_date(request):
if not include_api_key or _request_is_internal_workflow(request):
@@ -5333,6 +5342,10 @@ def _prepend_current_date_to_messages(
return copied
msg["content"] = [{"type": "text", "text": date_line}, *copied_parts]
return copied
+ if provider_type in _MODELFILE_SYSTEM_PROVIDERS:
+ # Synthesizing here is what costs an Ollama caller the Modelfile SYSTEM (#10436). Do not
+ # claim this exemption when something later will synthesize a system turn regardless.
+ return messages
return [{"role": "system", "content": date_line}, *copied]
@@ -20737,22 +20750,25 @@ async def _proxy_to_external_provider(
_reject_confirm_gate_without_channel(
payload, _ui_events, monitor_id, _catalog_names(external_studio_tools)
)
- chat_messages = _prepend_current_date_to_messages(
- chat_messages,
- request,
- include_api_key = run_studio_tool_loop,
- )
+ # Built before the date, because whether a nudge exists decides whether the Modelfile
+ # exemption is worth claiming: _append_to_system_message below displaces that prompt anyway.
+ # Full access disables the sandbox at execution time, so the schemas must say so too.
+ _external_nudge = ""
if run_studio_tool_loop and payload.bypass_permissions:
- # Full access disables the sandbox at execution time, so the schemas must
- # say so too rather than describing a sandbox the model will not get.
_external_nudge = _build_tool_action_nudge(
tools = external_studio_tools,
model_name = model,
full_access = True,
full_access_only = True,
)
- if _external_nudge:
- chat_messages = _append_to_system_message(chat_messages, _external_nudge)
+ chat_messages = _prepend_current_date_to_messages(
+ chat_messages,
+ request,
+ include_api_key = run_studio_tool_loop,
+ provider_type = None if _external_nudge else provider_type,
+ )
+ if _external_nudge:
+ chat_messages = _append_to_system_message(chat_messages, _external_nudge)
cancel_event = threading.Event()
cancel_keys = tuple(key for key in (payload.cancel_id, payload.session_id) if key)
diff --git a/studio/backend/tests/test_current_date_prompt_settings.py b/studio/backend/tests/test_current_date_prompt_settings.py
index b9750bdc144..5bd868e60d4 100644
--- a/studio/backend/tests/test_current_date_prompt_settings.py
+++ b/studio/backend/tests/test_current_date_prompt_settings.py
@@ -197,6 +197,31 @@ class TestExternalProviderMessages:
assert out[0] == {"role": "system", "content": "The current date is 2026-08-15."}
assert out[1] == {"role": "user", "content": "hi"}
+ def test_ollama_keeps_its_modelfile_system_prompt_when_studio_sends_none(self):
+ # Ollama applies the Modelfile SYSTEM only while messages[0] is not a system turn (#10436).
+ import routes.inference as inference
+
+ messages = [{"role": "user", "content": "hi"}]
+ out = inference._prepend_current_date_to_messages(messages, provider_type = "ollama")
+ assert out is messages
+
+ def test_ollama_still_dates_a_system_turn_studio_composed(self):
+ import routes.inference as inference
+
+ out = inference._prepend_current_date_to_messages(
+ [{"role": "system", "content": "Be terse."}, {"role": "user", "content": "hi"}],
+ provider_type = "ollama",
+ )
+ assert out[0]["content"] == "The current date is 2026-08-15.\n\nBe terse."
+ assert out[1] == {"role": "user", "content": "hi"}
+
+ def test_other_local_servers_still_get_a_synthesized_system_turn(self):
+ import routes.inference as inference
+ out = inference._prepend_current_date_to_messages(
+ [{"role": "user", "content": "hi"}], provider_type = "llama_cpp"
+ )
+ assert out[0] == {"role": "system", "content": "The current date is 2026-08-15."}
+
def test_date_prefixes_text_inside_structured_content(self):
messages = [{"role": "system", "content": [{"type": "text", "text": "Be terse."}]}]
out = self._prepend(messages)
diff --git a/studio/backend/tests/test_external_hosted_tool_passthrough.py b/studio/backend/tests/test_external_hosted_tool_passthrough.py
index 8f1ab910485..8e0f98e1c16 100644
--- a/studio/backend/tests/test_external_hosted_tool_passthrough.py
+++ b/studio/backend/tests/test_external_hosted_tool_passthrough.py
@@ -219,6 +219,103 @@ def test_an_api_request_without_resolved_server_tools_stays_undated(monkeypatch)
]
+def test_an_ollama_connection_keeps_its_modelfile_prompt_when_studio_sends_no_system_turn(
+ monkeypatch,
+):
+ # A synthesized date-only turn at index 0 is what displaces the Modelfile SYSTEM (#10436).
+ inf = _install(monkeypatch, "ollama")
+ monkeypatch.setattr(
+ inf,
+ "current_date_prompt_line",
+ lambda **_kwargs: "The current date is 2026-08-15.",
+ )
+
+ _run(inf, _payload())
+
+ assert FakeExternalClient.last["passthrough"]["messages"] == [
+ {"role": "user", "content": "what is 2+2?"}
+ ]
+
+
+def test_an_ollama_connection_still_dates_a_studio_composed_system_prompt(monkeypatch):
+ """Over-firing would drop the date for every Ollama user, not just empty-prompt ones."""
+ inf = _install(monkeypatch, "ollama")
+ monkeypatch.setattr(
+ inf,
+ "current_date_prompt_line",
+ lambda **_kwargs: "The current date is 2026-08-15.",
+ )
+
+ _run(
+ inf,
+ _payload(
+ messages = [
+ {"role": "system", "content": "Be terse."},
+ {"role": "user", "content": "what is 2+2?"},
+ ]
+ ),
+ )
+
+ assert FakeExternalClient.last["passthrough"]["messages"] == [
+ {"role": "system", "content": "The current date is 2026-08-15.\n\nBe terse."},
+ {"role": "user", "content": "what is 2+2?"},
+ ]
+
+
[email protected]("provider_type", ("llama_cpp", "vllm", "custom"))
+def test_the_other_self_hosted_providers_still_get_the_synthesized_turn(monkeypatch, provider_type):
+ """These have no Modelfile SYSTEM to lose, so the exemption must not widen to them."""
+ inf = _install(monkeypatch, provider_type)
+ monkeypatch.setattr(
+ inf,
+ "current_date_prompt_line",
+ lambda **_kwargs: "The current date is 2026-08-15.",
+ )
+
+ _run(inf, _payload())
+
+ assert FakeExternalClient.last["passthrough"]["messages"] == [
+ {"role": "system", "content": "The current date is 2026-08-15."},
+ {"role": "user", "content": "what is 2+2?"},
+ ]
+
+
+def test_full_access_on_ollama_keeps_the_date_the_nudge_costs_nothing_to_carry(monkeypatch):
+ """Full access synthesizes its own system turn, displacing the Modelfile SYSTEM regardless.
+
+ Withholding the date there gives it up for a prompt that is lost anyway, which is the one
+ way the exemption can leave a caller worse off than having no exemption at all.
+ """
+ inf = _install(monkeypatch, "ollama")
+ monkeypatch.setattr(
+ inf,
+ "current_date_prompt_line",
+ lambda **_kwargs: "The current date is 2026-08-15.",
+ )
+ seen = {}
+
+ def _capture(*_args, **kwargs):
+ seen["messages"] = list(kwargs["run"].messages)
+ raise LoopEntered(kwargs.get("policy"))
+
+ monkeypatch.setattr(inf, "stream_with_studio_tools", _capture)
+
+ with pytest.raises(LoopEntered):
+ _run(
+ inf,
+ _payload(
+ enable_tools = True,
+ enabled_tools = ["terminal"],
+ run_tools_locally = True,
+ bypass_permissions = True,
+ ),
+ )
+
+ assert seen["messages"][0]["role"] == "system"
+ assert seen["messages"][0]["content"].startswith("The current date is 2026-08-15.\n\n")
+ assert "sandbox" in seen["messages"][0]["content"], "the Full access nudge is still delivered"
+
+
def test_a_hosted_code_execution_is_not_dropped(monkeypatch):
"""The regression in one line: `code_execution` has no local implementation,
so a loop that captures this request executes web_search itself and silently