GenerateResponse/ChatResponse silently drop the server's prompt_eval_cached_count field
メンテナーはふだん 1 日以内に返信
評価
調査の方向性
ollama/_types.pyを読み、GenerateResponseとChatResponseで共有されるBaseGenerateResponseを見つけます。既存のプロンプト評価フィールドをissueのペイロード例と比較します。そこにキャッシュ済み件数のオプションフィールドを追加し、両方のレスポンスモデルでパース時とシリアライズ時にその値が保持されることを確認します。
索引モデルが issue の本文から書いたものです。
説明
Actual behavior
The Ollama server returns prompt_eval_cached_count (number of prompt tokens served from the prompt cache) alongside prompt_eval_count on both /api/generate and /api/chat responses. It's documented in the server's own OpenAPI spec and actually implemented server-side:
-
docs/openapi.yaml(ollama/ollama): declared onGenerateResponse(line 146),GenerateStreamEvent(line 193), andChatResponse(line 353). -
api/types.go(ollama/ollama), lines 566 and 634:PromptEvalCachedCount *int `json:"prompt_eval_cached_count,omitempty"`consumed in
Metrics.Summary()(lines 1090-1101) to printprompt eval cached: N token(s)inollama run --verboseoutput and to compute the uncached-token eval rate.
ollama/_types.py's BaseGenerateResponse (the shared base of both GenerateResponse and ChatResponse) never declares this field. Pydantic's default extra='ignore' then silently drops it during parsing — every sibling duration/count field survives, this one alone disappears with no error or warning.
Minimal reproduction:
from ollama._types import GenerateResponse
server_json = {
'model': 'llama3.2:3b', 'done': True,
'prompt_eval_count': 50, 'prompt_eval_cached_count': 42,
'eval_count': 10,
}
r = GenerateResponse.model_validate(server_json)
r.prompt_eval_count # 50 (present)
r.prompt_eval_cached_count # AttributeError: 'GenerateResponse' object has no attribute 'prompt_eval_cached_count'
'prompt_eval_cached_count' in r.model_dump() # False
Same result constructing a ChatResponse from the equivalent chat payload.
Expected behavior
prompt_eval_cached_count is preserved as Optional[int] = None on BaseGenerateResponse, the same way prompt_eval_count/prompt_eval_duration already are — additive and backward compatible, since older servers simply omit the key.
Why this matters
Prompt caching is one of the main levers for making Ollama fast on repeated or long-context requests, and this field is the one piece of data that lets API consumers actually confirm whether their prompt was served from cache. The CLI already surfaces it (prompt eval cached: N token(s)). Right now, anyone trying to diagnose prompt-cache effectiveness through ollama-python instead of the CLI is working blind — the field is there in the server's response and vanishes silently on the way into the SDK's model.
Suggested fix
Add to BaseGenerateResponse in ollama/_types.py:
prompt_eval_cached_count: Optional[int] = None
'Number of prompt tokens read from the cache.'
Same additive, backward-compatible shape as #751's fix for the related Logprob/TokenLogprob server-bytes gap (#750).
Environment
ollama-python: main @ 35d9667e84ebbd9d792826868d050027f75c1cc1
pydantic 2.13.5, Python 3.13
- 主要言語
- Python
- スター
- 10.6k
- フォーク
- 1.2k
- 平均マージ
- 1時間 45分
- マージ済み PR(30日)
- 2
環境構築
このプロジェクトには開発コンテナ、Dockerfile、コントリビューションガイドがありません。まず README を読み、一般的な手順ははじめてのコントリビューションガイドを参照してください。
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
ollama/ollama-python のほかの issue
-
GenerateResponse/ChatResponse silently drop the server's prompt_eval_cached_count field対応中かも @pfolex が 4 日前に担当しました。 オープン
難易度 1/5 1時間未満 初心者へのやさしさ 95/100
ollama/ollama-python#779 ·
メンテナーはふだん 1 日以内に返信
-
Empty tools: [] triggers tool-calling on Llama 3.x client-side templates対応中かも @Mrun25 が 10 日前に担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100
ollama/ollama-python#767 · コメント 2 件 ·
メンテナーはふだん 1 日以内に返信
-
TokenLogprob/Logprob drop server bytes field対応中かも @vineoy が 29 日前に担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
ollama/ollama-python#750 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
-
convert_function_to_tool exposes *args and **kwargs as required string parameters対応中かも @aniketkrs が 23 日前に担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 88/100
ollama/ollama-python#735 ·
メンテナーはふだん 1 日以内に返信
-
A single image passed as a bare string/bytes is iterated character-by-character対応中かも このイシューにリンクされたプルリクエストがオープン中、またはマージ済みです。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
ollama/ollama-python#734 ·
メンテナーはふだん 1 日以内に返信
ollama/ollama-python の issue をすべて見る
似ている issue
-
area/profiles comp/cli P3 type/feature
難易度 2/5 1〜3時間 初心者へのやさしさ 74/100
NousResearch/hermes-agent#146372 ·
メンテナーはふだん 1 日以内に返信
-
[BUG] OpenAI-compatible embedding model alias breaks embedder construction対応中かも このイシューにリンクされたプルリクエストがオープン中、またはマージ済みです。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 65/100
メンテナーはふだん 1 日以内に返信
-
[oblt-aw][autodoc] Document obs-aw-automerge contract and fix automerge doc inconsistencies対応中かも @github-actions が今日担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
メンテナーはふだん 1 日以内に返信
-
GeoSeries scale and skew tests fail with pytest 9.2.0対応中かも @jiayuasu が今日担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 82/100
メンテナーはふだん 1 日以内に返信
-
admission-deferred priority: medium status: ready work-class: scoped
難易度 2/5 1〜3時間 初心者へのやさしさ 70/100
melodic-software/claude-code-plugins#10887 · コメント 4 件 ·
メンテナーはふだん 1 日以内に返信