Repository navigation
fix(llm-proxy): report finish_reason "length" for truncated native Ollama replies - #7808
Open
saisriram-dev wants to merge 1 commit into
Open
saisriram-dev wants to merge 1 commit into
saisriram-dev wants to merge 1 commit into
Conversation
Closes IBM#7771 Signed-off-by: Sai Sri Ram Pitta <pssr0701@gmail.com>
saisriram-dev
requested review from
Lang-Akshay,
brian-hussey,
ja8zyjits and
msureshkumar88
as code owners
December 4, 2026 18:53
This branch has not been deployed
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.
📌 Summary
Native Ollama (
/api/chat) responses always reached OpenAI clients asfinish_reason: "stop", even when Ollama cut the reply at the token limit (done_reason: "length"). Clients couldn't tell a full answer from a truncated one. This PR passes the truncation signal through.Thanks @pcbeingused33 for the detailed report and proposed fix.
Closes #7771
🔁 Reproduction Steps
/v1).max_tokens(e.g. 8)."done": true, "done_reason": "length", but the proxy returnedfinish_reason: "stop".🐞 Root Cause
_transform_ollama_responseand_transform_ollama_stream_chunkinmcpgateway/services/llm_proxy_service.pysetfinish_reasonto"stop"wheneverdonewas true and never readdone_reason.💡 Fix Description
_ollama_finish_reason(): returns"length"whendone_reason == "length","stop"for any other finished response,Nonewhile still in progress (with doctests).done_reasonvalues (e.g.load,unload) map to"stop", since OpenAI-compatible clients expect a fixed set offinish_reasonvalues./v1(OpenAI-compatible) Ollama path is unchanged.📏 Reviewability
triage🧪 Verification
My Codespace dev container failed to build (recovery mode), so I couldn't run the full suite locally.
python3 -m py_compileon both changed files: passedmake lint/make test/make coverage: relying on CI for this runAdded parametrized unit tests in
tests/unit/mcpgateway/services/test_llm_proxy_service.pyfor both the non-streaming and streaming paths (length,stop, missing, unknown), plus doctests on the helper.