Skip to content

fix: do not prefix Ollama replies with <think>None</think> - #2619

Open
pcbeingused333 wants to merge 1 commit into
MemTensor:mainfrom
pcbeingused333:fix/ollama-empty-thinking
Open

pcbeingused333 wants to merge 1 commit into
MemTensor:mainfrom
pcbeingused333:fix/ollama-empty-thinking

Conversation

@pcbeingused333

Copy link
Copy Markdown

Description

OllamaLLM.generate prefixes every reply with the literal text <think>None</think> when the model returns no thinking.

Root cause. ollama 0.5 (the pinned range) declares Message.thinking as optional with default None, so the attribute is always present. generate treated hasattr(response.message, "thinking") as "there is thinking" and interpolated the value, so None becomes the characters None. remove_think_prefix defaults to False, so callers with the default config receive the prefix. generate_stream already skips empty thinking.

Fix. Build the <think> wrapper only when thinking is non-empty. A missing attribute, None and "" produce no prefix; a real thinking string is still wrapped unchanged.

Related Issue (Required): none filed; this is a small bug fix, which CONTRIBUTING.md allows as a direct PR.

Type of change

  • Bug fix (non-breaking change which fixes an issue)

How Has This Been Tested?

  • Unit Test

tests/llms/test_ollama.py::test_generate_does_not_stringify_none_thinking builds an ollama.Message with only role and content (so thinking is None), mocks client.chat, and asserts generate returns "Hello". It fails on main with <think>None</think>Hello. The existing tests still check that a real thinking string is wrapped.

Reproduction: pytest tests/llms/test_ollama.py

Checklist

  • I have performed a self-review of my own code | 我已自行检查了自己的代码
  • I have commented my code in hard-to-understand areas | 我已在难以理解的地方对代码进行了注释
  • I have added tests that prove my fix is effective or that my feature works | 我已添加测试以证明我的修复有效或功能正常

This change was prepared with the help of an AI assistant.

Message.thinking is Optional and defaults to None in ollama>=0.5, so
hasattr is always true and generate stringified that None into every
reply. Skip the prefix when thinking is empty, as generate_stream does.
@Memtensor-AI Memtensor-AI added area:model llm + embedder + reranker status:in-progress Someone or AI is working on it | 人工或 AI 正在处理 labels Nov 13, 2026
@Memtensor-AI
Memtensor-AI requested a review from endxxxx November 13, 2026 15:03
@Memtensor-AI

Copy link
Copy Markdown
Collaborator

🤖 Open Code Review

Target: PR #2619
Task: 860edb8e0565fb51
Base: main
Head: fix/ollama-empty-thinking
Head SHA: a30d4baf3d2e55450523db5bab0d9052ece31491

✅ OpenCodeReview: Review complete: 0 finding(s) across 1 selected item(s).

Generated by cloud-assistant via Open Code Review.

@Memtensor-AI Memtensor-AI added status:ready Ready for implementation; waiting for assignee or AI dispatch | 可进入实现,等待认领或派发 and removed status:in-progress Someone or AI is working on it | 人工或 AI 正在处理 labels Nov 17, 2026
@Memtensor-AI

Copy link
Copy Markdown
Collaborator

✅ Automated Test Results: PASSED

Required tests passed (3/3 executed, 0 skipped). Duration: 15s [advisory, non-gating] AI-generated tests on branch test/auto-gen-860edb8e0565fb51-20261117094923: 36/36 passed — these do NOT affect the PR verdict; review the branch manually.

Branch: fix/ollama-empty-thinking

Tested source: a30d4baf3d2e55450523db5bab0d9052ece31491
Tested base: 3a16e5fe3048a209f722806f4123fa2e533588fd
Tested commit: a30d4baf3d2e55450523db5bab0d9052ece31491
These results apply only to this immutable source/base snapshot.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:model llm + embedder + reranker status:ready Ready for implementation; waiting for assignee or AI dispatch | 可进入实现,等待认领或派发

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants