feat(ollama): logprobs support in Ollama (#34218)

Closes #34207 

---

Expose log probabilities from the Ollama Python SDK through
`ChatOllama`. The ollama client already returns a `logprobs` field on
chat responses for supported models, but `ChatOllama` had no way to
request or surface it.

## Changes
- Add `logprobs` and `top_logprobs` fields to `ChatOllama`, forwarded to
the client via `_build_chat_params`. Setting `top_logprobs` without
`logprobs=True` auto-enables it with a warning; setting it with
`logprobs=False` raises a `ValueError`
- Surface per-token logprobs on intermediate streaming chunks (both sync
`_create_chat_stream` and async `_create_async_chat_stream`) via
`response_metadata["logprobs"]`, accumulated into the final response on
`invoke()`
- Bump minimum `ollama` SDK from `>=0.6.0` to `>=0.6.1` — the version
that added logprobs support

---------

Co-authored-by: Mason Daugherty <mason@langchain.dev>
Co-authored-by: Mason Daugherty <github@mdrxy.com>
This commit is contained in:
Mohammad Mohtashim
2026-04-07 02:06:51 +05:00
committed by GitHub
parent 642c981d70
commit 0aa482d0cd
4 changed files with 397 additions and 40 deletions

View File

@@ -23,7 +23,7 @@ classifiers = [
version = "1.0.1"
requires-python = ">=3.10.0,<4.0.0"
dependencies = [
"ollama>=0.6.0,<1.0.0",
"ollama>=0.6.1,<1.0.0",
"langchain-core>=1.2.21,<2.0.0",
]
@@ -114,6 +114,7 @@ asyncio_mode = "auto"
[tool.ruff.lint.extend-per-file-ignores]
"tests/**/*.py" = [
"S101", # Tests need assertions
"S105", # False positive on dict key "token" in logprobs assertions
"S311", # Standard pseudo-random generators are not suitable for cryptographic purposes
"ARG001", # Unused function arguments in tests (e.g. kwargs)
"PLR2004", # Magic value in comparisons