You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
ollama[patch]: fix model validation, ensure per-call reasoning can be set, tests (#31927)
* update model validation due to change in [Ollama
client](https://github.com/ollama/ollama) - ensure you are running the
latest version (0.9.6) to use `validate_model_on_init`
* add code example and fix formatting for ChatOllama reasoning
* ensure that setting `reasoning` in invocation kwargs overrides
class-level setting
* tests
You can enable reasoning mode for models that support it by setting
367
+
the ``reasoning`` parameter to ``True`` in either the constructor or
368
+
the ``invoke``/``stream`` methods. This will enable the model to think
369
+
through the problem and return the reasoning process separately in the
370
+
``additional_kwargs`` of the response message, under ``reasoning_content``.
371
+
372
+
If ``reasoning`` is set to ``None``, the model will use its default reasoning
373
+
behavior, and any reasoning content will *not* be captured under the
374
+
``reasoning_content`` key, but will be present within the main response content
375
+
as think tags (``<think>`` and ``</think>``).
376
+
377
+
.. note::
378
+
This feature is only available for `models that support reasoning <https://ollama.com/search?c=thinking>`__.
379
+
380
+
.. code-block:: python
381
+
382
+
from langchain_ollama import ChatOllama
383
+
384
+
llm = ChatOllama(
385
+
model = "deepseek-r1:8b",
386
+
reasoning= True,
387
+
)
388
+
389
+
user_message = HumanMessage(content="how many r in the word strawberry?")
390
+
messages: List[Any] = [user_message]
391
+
llm.invoke(messages)
392
+
393
+
# or, on an invocation basis:
394
+
395
+
llm.invoke(messages, reasoning=True)
396
+
# or llm.stream(messages, reasoning=True)
397
+
398
+
# If not provided, the invocation will default to the ChatOllama reasoning
399
+
# param provided (None by default).
400
+
401
+
.. code-block:: python
402
+
403
+
AIMessage(content='The word "strawberry" contains **three \'r\' letters**. Here\'s a breakdown for clarity:\n\n- The spelling of "strawberry" has two parts ... be 3.\n\nTo be thorough, let\'s confirm with an online source or common knowledge.\n\nI can recall that "strawberry" has: s-t-r-a-w-b-e-r-r-y — yes, three r\'s.\n\nPerhaps it\'s misspelled by some, but standard is correct.\n\nSo I think the response should be 3.\n'}, response_metadata={'model': 'deepseek-r1:8b', 'created_at': '2025-07-08T19:33:55.891269Z', 'done': True, 'done_reason': 'stop', 'total_duration': 98232561292, 'load_duration': 28036792, 'prompt_eval_count': 10, 'prompt_eval_duration': 40171834, 'eval_count': 3615, 'eval_duration': 98163832416, 'model_name': 'deepseek-r1:8b'}, id='run--18f8269f-6a35-4a7c-826d-b89d52c753b3-0', usage_metadata={'input_tokens': 10, 'output_tokens': 3615, 'total_tokens': 3625})
0 commit comments