GenerateResponse/ChatResponse silently drop the server's prompt_eval_cached_count field
维护者通常 1 天内回复
评估
调研方向
从 ollama/_types.py 开始,检查 BaseGenerateResponse,它是 GenerateResponse 和 ChatResponse 的共享基类。在现有的 prompt 评估字段旁添加可选字段 prompt_eval_cached_count,然后运行 issue 中使用 GenerateResponse.model_validate() 的最小复现,并检查 model_dump()。当两种响应类型都保留服务器字段,同时仍与省略该字段的响应兼容时,改动即告完成。
由索引模型根据 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 分钟
- 30 天内合并 PR
- 2
环境准备
这个项目没有提供开发容器、Dockerfile 或贡献指南,环境需要你自己搭建:先看它的 README,通用步骤见我们的新手贡献指南。
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
ollama/ollama-python 的其他 Issue
-
GenerateResponse/ChatResponse silently drop the server's prompt_eval_cached_count field可能已有人在做 @shuaiyafei 于 8 天前认领。 未关闭
难度 1/5 1 小时以内 新手友好度 88/100
ollama/ollama-python#773 ·
维护者通常 1 天内回复
-
Empty tools: [] triggers tool-calling on Llama 3.x client-side templates可能已有人在做 @Mrun25 于 11 天前认领。 未关闭
难度 2/5 1-3 小时 新手友好度 68/100
ollama/ollama-python#767 · 2 条评论 ·
维护者通常 1 天内回复
-
TokenLogprob/Logprob drop server bytes field可能已有人在做 @vineoy 于 30 天前认领。 未关闭
难度 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 于 24 天前认领。 未关闭
难度 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可能已有人在做 关联的 PR 仍在进行中或已合并。 未关闭
难度 2/5 1-3 小时 新手友好度 72/100
ollama/ollama-python#734 ·
维护者通常 1 天内回复
查看 ollama/ollama-python 的全部 Issue
相似的 Issue
-
infrastructure
难度 2/5 1-3 小时 新手友好度 76/100
NatLabRockies/H2Integrate#977 ·
维护者通常 1 天内回复
-
bug
难度 2/5 1-3 小时 新手友好度 72/100
topoteretes/cognee#6470 · 1 条评论 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 62/100
-
area:docs priority:p2 type:docs wave:9
难度 1/5 1 小时以内 新手友好度 84/100
jejjohnson/radtrax#293 ·
维护者通常 1 天内回复
-
bug core plugins
难度 2/5 1-3 小时 新手友好度 68/100
bunkerity/bunkerweb#4121 · 1 条评论 ·
维护者通常 1 天内回复