Skip to content

fix(llm-proxy): report finish_reason "length" for truncated native Ollama replies - #7808

Open
saisriram-dev wants to merge 1 commit into
IBM:mainfrom
saisriram-dev:fix/7771-ollama-length-finish-reason
Open

saisriram-dev wants to merge 1 commit into
IBM:mainfrom
saisriram-dev:fix/7771-ollama-length-finish-reason

Conversation

@saisriram-dev

Copy link
Copy Markdown

📌 Summary

Native Ollama (/api/chat) responses always reached OpenAI clients as finish_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

  1. Configure an Ollama provider with a native base URL (no /v1).
  2. Send a chat completion with a small max_tokens (e.g. 8).
  3. Ollama returns "done": true, "done_reason": "length", but the proxy returned finish_reason: "stop".

🐞 Root Cause

_transform_ollama_response and _transform_ollama_stream_chunk in mcpgateway/services/llm_proxy_service.py set finish_reason to "stop" whenever done was true and never read done_reason.

💡 Fix Description

  • Added _ollama_finish_reason(): returns "length" when done_reason == "length", "stop" for any other finished response, None while still in progress (with doctests).
  • Used it in both transforms so streaming and non-streaming stay consistent.
  • Unknown done_reason values (e.g. load, unload) map to "stop", since OpenAI-compatible clients expect a fixed set of finish_reason values.
  • The /v1 (OpenAI-compatible) Ollama path is unchanged.

📏 Reviewability

  • This PR has one clear purpose
  • The linked issue is not labeled triage
  • Tests are included with the code they validate
  • If AI-assisted, I understand and can explain the generated changes

🧪 Verification

My Codespace dev container failed to build (recovery mode), so I couldn't run the full suite locally.

  • python3 -m py_compile on both changed files: passed
  • make lint / make test / make coverage: relying on CI for this run

Added parametrized unit tests in tests/unit/mcpgateway/services/test_llm_proxy_service.py for both the non-streaming and streaming paths (length, stop, missing, unknown), plus doctests on the helper.

Closes IBM#7771

Signed-off-by: Sai Sri Ram Pitta <pssr0701@gmail.com>

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: LLM proxy reports finish_reason "stop" for native Ollama responses truncated by max_tokens

1 participant