Hacktoberfest 2026:メンテナが10月に向けて印を付けた、オープンで初心者向けの issue。 Hacktoberfest の issue を見る

GenerateResponse/ChatResponse silently drop the server's prompt_eval_cached_count field

オープン 初心者向け
#773 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

@shuaiyafei がすでに取り組んでいます。

2026年10月22日 から。

  • #774 @shuaiyafei による — オープン

評価

難易度
1/5
見積もり時間
1時間未満
初心者へのやさしさ
88/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
python
領域
api

調査の方向性

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 on GenerateResponse (line 146), GenerateStreamEvent (line 193), and ChatResponse (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 print prompt eval cached: N token(s) in ollama run --verbose output 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 を読み、一般的な手順ははじめてのコントリビューションガイドを参照してください。

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

ollama/ollama-python のほかの issue

ollama/ollama-python の issue をすべて見る

似ている issue

Python の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。