Skip to content

Python: Report the finish reason and keep zero-prompt-token usage for Ollama chat - #14674

Open
alexcastgonz (pcbeingused333) wants to merge 1 commit into
microsoft:mainfrom
pcbeingused333:fix/ollama-finish-reason
Open

alexcastgonz (pcbeingused333) wants to merge 1 commit into
microsoft:mainfrom
pcbeingused333:fix/ollama-finish-reason

Conversation

@pcbeingused333

Copy link
Copy Markdown
Contributor

Motivation and Context

Ollama reports why generation stopped in done_reason, but the Ollama chat connector never put it on the returned content, so finish_reason was always None. 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_response dropped the usage whenever prompt_eval_count was 0 (a fully cached prompt), because it tested truthiness instead of is not None.

Description

  • OllamaChatCompletion._get_finish_reason maps done_reason: length -> FinishReason.LENGTH; stop -> FinishReason.STOP, or FinishReason.TOOL_CALLS when the message has tool calls (Ollama reports stop for those, other connectors report tool_calls); anything else (including a streaming chunk that is not the last one) -> None.
  • It is applied on all four paths: ChatResponse and mapping responses, streaming and not.
  • The ChatResponse usage check now uses is not None, so prompt_eval_count == 0 keeps the usage.
  • Tests (tests/unit/connectors/ai/ollama/services/test_ollama_chat_completion.py): finish reason for stop / length / stop with tool calls / unknown / missing on real ollama ChatResponse objects and on mappings; a streamed response where only the last chunk carries length; usage with prompt_eval_count == 0. These fail on main and pass with the change; the full Ollama unit test directory passes and ruff check / ruff format --check are clean. Not run against a live Ollama server.

Contribution Checklist

This change was made with Claude Code (Claude Sonnet 5.5).

… 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>
Copilot AI balanced review requested due to automatic review settings October 29, 2026 01:52
@semantic-kernel-automation semantic-kernel-automation Bot added the python Pull requests for the Python Semantic Kernel label Oct 29, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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_reason to 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

1 active deployment
github-app-auth — 15d0f436 Deployed Oct 29, 2026 by pcbeingused333 via add_label #29479
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

python Pull requests for the Python Semantic Kernel

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants