Repository navigation
Python: Report the finish reason and keep zero-prompt-token usage for Ollama chat - #14674
Open
alexcastgonz (pcbeingused333) wants to merge 1 commit into
Open
alexcastgonz (pcbeingused333) wants to merge 1 commit into
alexcastgonz (pcbeingused333) wants to merge 1 commit into
Conversation
… Ollama chat Ollama reports why generation stopped in done_reason, but the connector never passed it on, so a reply cut off by the token limit looked like a complete one. Map stop/length (and stop with tool calls) to FinishReason on the four response paths, and keep the usage when the prompt was fully cached (prompt_eval_count 0). Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
alexcastgonz (pcbeingused333)
deployed
to
github-app-auth
October 29, 2026 01:53 — with
GitHub Actions
Active
Contributor
There was a problem hiding this comment.
🟡 Changes recommended
Both streaming converters incorrectly report STOP instead of TOOL_CALLS when tool calls precede an empty terminal chunk, requiring a fix and regression coverage for both response types.
1 open issue
What changed in this PR
This PR updates the Python Ollama chat connector to expose generation finish reasons and preserve usage data when prompt token counts are zero.
Changes:
- Maps Ollama’s
done_reasonto finish reasons for typed and mapping responses, including streaming. - Preserves token usage when counts are zero rather than missing.
- Adds tests for finish reasons, terminal streaming chunks, and zero-prompt-token usage.
| File | Description |
|---|---|
python/tests/unit/connectors/ai/ollama/services/test_ollama_chat_completion.py |
Adds finish-reason and zero-token usage tests; updates response doubles. |
python/semantic_kernel/connectors/ai/ollama/services/ollama_chat_completion.py |
Adds finish-reason mapping across response paths and retains zero-token usage. |
Review effort: Balanced
| inner_content=response, | ||
| ai_model_id=self.ai_model_id, | ||
| metadata=self._get_metadata_from_chat_response(response), | ||
| finish_reason=self._get_finish_reason(response.done_reason, bool(response.message.tool_calls)), |
This branch was successfully 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.

Motivation and Context
Ollama reports why generation stopped in
done_reason, but the Ollama chat connector never put it on the returned content, sofinish_reasonwas alwaysNone. A reply cut off by the token limit (done_reason == "length") could not be told apart from a complete one. Separately,_get_metadata_from_chat_responsedropped the usage wheneverprompt_eval_countwas 0 (a fully cached prompt), because it tested truthiness instead ofis not None.Description
OllamaChatCompletion._get_finish_reasonmapsdone_reason:length->FinishReason.LENGTH;stop->FinishReason.STOP, orFinishReason.TOOL_CALLSwhen the message has tool calls (Ollama reportsstopfor those, other connectors reporttool_calls); anything else (including a streaming chunk that is not the last one) ->None.ChatResponseand mapping responses, streaming and not.ChatResponseusage check now usesis not None, soprompt_eval_count == 0keeps the usage.tests/unit/connectors/ai/ollama/services/test_ollama_chat_completion.py): finish reason for stop / length / stop with tool calls / unknown / missing on realollamaChatResponseobjects and on mappings; a streamed response where only the last chunk carrieslength; usage withprompt_eval_count == 0. These fail onmainand pass with the change; the full Ollama unit test directory passes andruff check/ruff format --checkare clean. Not run against a live Ollama server.Contribution Checklist
This change was made with Claude Code (Claude Sonnet 5.5).