sdk-python icon indicating copy to clipboard operation
sdk-python copied to clipboard

[BUG] OllamaModel: a reasoningContent block in history raises TypeError, and streamed thinking is dropped

Open Tanmay-Ts opened this issue 4 weeks ago • 2 comments

Human Overview

So I was running ollama and I noticed some issues or things that I could fix so the ollama model currently doesnt know how to handle reasoning from other providers and it doesnt keep reasoning generated by its models when i checked thoroughly i found two bugs one caused crashes and the other caused information loss

Checks

  • [x] I have updated to the lastest minor and patch version of Strands
  • [x] I have checked the documentation and this is not expected behavior
  • [x] I have searched ./issues and there are no duplicates of my issue

SDK Language

Python

Strands Version

main @ 7eb9671 (1.63.1.dev27)

Language Runtime Version

Python 3.12

Operating System

Windows 11

Installation Method

git clone

Steps to Reproduce

  1. Give an Ollama agent a history that holds a reasoningContent block, as one written by a reasoning model before the agent moved to Ollama would. It fails before any request is sent, so Ollama doesn't need to be running:
from strands import Agent
from strands.models.ollama import OllamaModel

history = [
    {"role": "user", "content": [{"text": "Is 91 prime?"}]},
    {"role": "assistant", "content": [
        {"reasoningContent": {"reasoningText": {"text": "91 = 7 x 13", "signature": "sig"}}},
        {"text": "No, 91 = 7 x 13."},
    ]},
]
agent = Agent(model=OllamaModel(host="http://localhost:11434", model_id="qwen3:8b"), messages=history)
agent("And 97?")
  1. With Ollama 0.40.5 and qwen3:8b, run Agent(model=OllamaModel(host="http://localhost:11434", model_id="qwen3:8b"))("Is 91 a prime number? Answer yes or no.") and look at result.message["content"].

Expected Behavior

Reasoning blocks in the history are skipped with the once-per-request skip log, as OpenAIModel does, and a thinking model's streamed message.thinking arrives as reasoningContent, as it does with OpenAIModel and LiteLLMModel.

Actual Behavior

  1. TypeError: content_type=<reasoningContent> | unsupported type, raised by _format_request_message_contents (ollama.py:167).
  2. The thinking is dropped. The same question on the raw ollama client streamed 394 chunks with message.thinking (1,162 characters) before an 81-character answer. Through Strands, result.message["content"] holds only a text block, while outputTokens (427) still counts the thinking tokens.

Possible Solution

#1051 (October 2025) already does both: it skips reasoningContent blocks in requests and streams message.thinking as reasoningContent. It hasn't been reviewed and now conflicts after the move to strands-py/. The two parts go together: capturing thinking without the request-side skip would make a thinking model's own next turn raise the TypeError.

@scippacercola, are you still working on #1051? If not, I'm happy to bring it up to date, keeping your commits and logging skipped blocks through the skipped counter (#5319) as openai.py does.

Additional Context

Context
  • ollama.py is the only provider in strands-py/src/strands/models/ with no reasoningContent handling.
  • TypeScript has no Ollama provider, so this is Python only.
  • Open PR #5768 also changes OllamaModel.stream, for #5742.
  • Found while running the tests_integ/models/test_model_ollama.py agent case on qwen3:8b instead of llama3.3:70b. That case passes.

Related Issues

#1051 (open PR with the fix), #4779 (reasoning capture and replay across providers; the native OllamaModel isn't in its child-issue table)

Tanmay-Ts avatar Nov 04 '26 02:11 Tanmay-Ts

Are you planning to send a PR for this? If not, I'd be glad to take the TypeError part (skipping reasoningContent in the history, as OpenAIModel does). The streaming part looks like it overlaps #1051.

mittalpk avatar Nov 05 '26 14:11 mittalpk

Sorry for the slow reply. Yes, I'm planning a PR. Since #1051 hasn't had a reply, I'll bring it up to date with main and keep @scippacercola's commits, which covers both the TypeError and the dropped thinking stream. If you've already started on the TypeError part, I'm happy to split it: you take the history fix and I'll do streaming.

Tanmay-Ts avatar Nov 14 '26 12:11 Tanmay-Ts