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

Open Beginner friendly
#7,771 1 comment 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 2 days

@saisriram-dev is already working on this.

Since Dec 4, 2026.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
86/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
ollama, python

Research direction

Read _transform_ollama_response and _transform_ollama_stream_chunk in mcpgateway/services/llm_proxy_service.py, then find the unit tests for both response paths. Check how native Ollama's done_reason is represented, especially length, and add tests for the mapping described in the issue. Done when both paths return length for token limit truncation and preserve stop for other completion reasons.

Written by the indexing model from the issue text.

Description

When the LLM proxy talks to a native Ollama endpoint (/api/chat), _transform_ollama_response and _transform_ollama_stream_chunk in mcpgateway/services/llm_proxy_service.py always emit finish_reason: "stop" once done is true, ignoring Ollama's done_reason. So a reply cut off by the token limit (options.num_predict, which the proxy sets from max_tokens) comes back as done_reason: "length" from Ollama but as finish_reason: "stop" to OpenAI clients, and they can't detect the truncation. Minimal check against Ollama: POST /api/chat with "options": {"num_predict": 8} returns "done": true, "done_reason": "length". Proposed fix: map done_reason == "length" to "length" (anything else stays "stop") in both functions, with unit tests for both paths. Happy to open a PR once this is triaged.

Dominant language
Python
Stars
4.9k
Forks
1k
Avg merge
3d 9h
Merged PRs (30d)
91

Getting set up

Open in Codespaces

Starts the project's dev container in your browser, under your own GitHub account.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from IBM/mcp-context-forge

All issues in IBM/mcp-context-forge

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.