Skip to content

.Net: feat(ollama): add Think property to OllamaPromptExecutionSettings#14122

Open
arashzjahangiri wants to merge 2 commits into
microsoft:mainfrom
arashzjahangiri:fix/ollama-think-property
Open

.Net: feat(ollama): add Think property to OllamaPromptExecutionSettings#14122
arashzjahangiri wants to merge 2 commits into
microsoft:mainfrom
arashzjahangiri:fix/ollama-think-property

Conversation

@arashzjahangiri

Copy link
Copy Markdown

Summary

  • Adds Think (bool?) property to OllamaPromptExecutionSettings to control thinking for Ollama reasoning models (deepseek-r1, qwen3, phi4-reasoning)
  • Maps Think to GenerateRequest.Think in OllamaTextGenerationService.CreateRequest()
  • Bumps OllamaSharp from 5.4.12 to 5.4.25 which introduces GenerateRequest.Think

Motivation

Fixes #14078. When a reasoning model has thinking enabled by default (e.g. qwen3, phi4-reasoning), the model output lands in a separate thinking stream rather than the standard response field. This causes GetTextContentsAsync to return empty content. The only way to get a usable response is to pass think=false to suppress thinking, but there was no way to set that from OllamaPromptExecutionSettings.

Changes

OllamaPromptExecutionSettings.cs

  • New Think property with bool? type, JSON name think, WhenWritingNull ignore condition, ThrowIfFrozen() guard, and Clone() support

OllamaTextGenerationService.cs

  • CreateRequest() maps settings.Think to GenerateRequest.Think using OllamaSharp.Models.Chat.ThinkValue

Directory.Packages.props

  • OllamaSharp bumped from 5.4.12 to 5.4.25 (adds GenerateRequest.Think and ThinkValue)

Tests

  • OllamaPromptExecutionSettingsTests: serialization round-trip, Clone, Freeze guard for Think
  • OllamaTextGenerationTests: verifies Think is present/absent in the serialized request payload for both GetTextContentsAsync and GetStreamingTextContentsAsync

Test plan

  • All 115 existing unit tests pass (1 pre-existing skip)
  • ThinkPropertyRoundTripsViaSerialization - true/false round-trips via JSON
  • ThinkPropertyIsPreservedByClone - Clone copies the value
  • ThinkPropertyThrowsWhenFrozen - setter throws after Freeze
  • GetTextContentsShouldSendThinkSettingAsync - request payload contains correct think field
  • GetTextContentsShouldNotSendThinkWhenNotSetAsync - request payload omits think when null
  • GetStreamingTextContentsShouldSendThinkSettingAsync - streaming path also sends think

Adds a Think property (bool?) to OllamaPromptExecutionSettings that
controls thinking behavior for Ollama reasoning models (deepseek-r1,
qwen3, phi4-reasoning).

When a reasoning model has thinking enabled by default, its output
lands in a separate thinking stream rather than the standard response
field, causing GetTextContentsAsync to return empty content. Setting
Think = false suppresses thinking so all output appears in the standard
response field.

Changes:
- OllamaPromptExecutionSettings: adds Think property with backing field,
  JSON serialization (think), ThrowIfFrozen guard, and Clone support
- OllamaTextGenerationService: maps Think to GenerateRequest.Think in
  CreateRequest()
- Bumps OllamaSharp from 5.4.12 to 5.4.25 (adds GenerateRequest.Think)
- Tests: serialization round-trip, Clone, Freeze, and request payload
  verification for both GetTextContentsAsync and
  GetStreamingTextContentsAsync

Fixes microsoft#14078
@arashzjahangiri arashzjahangiri requested a review from a team as a code owner June 28, 2026 11:28
Copilot AI review requested due to automatic review settings June 28, 2026 11:28
@moonbox3 moonbox3 added .NET Issue or Pull requests regarding .NET code kernel Issues or pull requests impacting the core kernel labels Jun 28, 2026
@github-actions github-actions Bot changed the title feat(ollama): add Think property to OllamaPromptExecutionSettings .Net: feat(ollama): add Think property to OllamaPromptExecutionSettings Jun 28, 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.

Pull request overview

This PR extends the .NET Ollama connector to support controlling “thinking” behavior for reasoning-capable models by introducing a Think setting on OllamaPromptExecutionSettings and wiring it through to the Ollama request payload (enabled by the OllamaSharp dependency update).

Changes:

  • Added nullable Think (bool?) to OllamaPromptExecutionSettings, including freeze-guarding and clone support.
  • Mapped OllamaPromptExecutionSettings.Think into the generated Ollama GenerateRequest.
  • Updated OllamaSharp dependency to a version that exposes GenerateRequest.Think, and added unit tests covering serialization/clone/freeze and request payload behavior.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
dotnet/src/Connectors/Connectors.Ollama/Settings/OllamaPromptExecutionSettings.cs Adds Think execution setting with JSON serialization behavior and clone support.
dotnet/src/Connectors/Connectors.Ollama/Services/OllamaTextGenerationService.cs Includes Think in the generated request sent to Ollama.
dotnet/src/Connectors/Connectors.Ollama.UnitTests/Settings/OllamaPromptExecutionSettingsTests.cs Adds tests for Think serialization, cloning, and freeze behavior.
dotnet/src/Connectors/Connectors.Ollama.UnitTests/Services/OllamaTextGenerationTests.cs Verifies think is included/omitted in request payloads for streaming and non-streaming paths.
dotnet/Directory.Packages.props Bumps OllamaSharp to a version that includes GenerateRequest.Think.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@arashzjahangiri

Copy link
Copy Markdown
Author

@microsoft-github-policy-service agree

- Simplify ThinkValue mapping: use settings.Think.Value (non-null bool)
  instead of passing nullable bool to constructor
- Use ToLowerInvariant() instead of ToLower() in test JSON builder
  to avoid culture-sensitive casing
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

kernel Issues or pull requests impacting the core kernel .NET Issue or Pull requests regarding .NET code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

.Net: Bug: kernel.InvokeAsync returns empty result for thinking-enabled models (Qwen3.5) via SK's Ollama connector

3 participants