fix: fall back to Ollama native /api/chat for thinking-mode models (fixes #26)#38
Open
nandanadileep wants to merge 1 commit intonikmcfly:mainfrom
Open
Conversation
Thinking-mode models (e.g. Gemma 4) generate internal reasoning tokens that can exhaust max_tokens before producing any visible content. Ollama's OpenAI-compatible /v1/chat/completions endpoint strips those tokens and returns empty content, causing 500 errors in simulations. When LLMClient.chat() receives empty content from an Ollama endpoint, it now retries via the native /api/chat endpoint which correctly returns the visible response. The fallback is backwards-compatible and only triggers on empty responses. Fixes nikmcfly#26
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Thinking-mode models (e.g.
gemma4:26b) generate internal<|think|>reasoning tokens that exhaustmax_tokensbefore producing visible content. Ollama's OpenAI-compatible/v1/chat/completionsendpoint strips those tokens and returns emptycontent, causing 500 errors when starting simulations.Fix
In
LLMClient.chat(), after calling the OpenAI-compat endpoint, check ifcontentis empty. If it is and we're talking to an Ollama server, retry via the native/api/chatendpoint, which surfaces the visible response correctly.Changes in
backend/app/utils/llm_client.py:_ollama_native_base()— strips/v1suffix to get the Ollama host URL_chat_via_ollama_native()— POSTs to/api/chatwith stream=false, carries overtemperatureandnum_ctxchat(): triggers the fallback only whencontentis falsy and_is_ollama()is true — fully backwards-compatible, zero impact on non-Ollama or non-thinking-mode modelsNoneTypecrash:re.sub(…, content or '')guards againstNonecontent even without the fallbackTest plan
qwen2.5:32b) — behaviour unchangedgemma4:26b— should now return visible response instead of 500_ollama_native_base()strips/v1fromhttp://localhost:11434/v1correctlyFixes #26