Hacktoberfest 2026:维护者为十月标记出来的 issue,仍然开放、适合新手。 浏览 Hacktoberfest issue

GenerateResponse/ChatResponse silently drop the server's prompt_eval_cached_count field

未关闭 适合新手
#779 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

@pfolex 已经在做这个了。

开始于 2026年10月24日。

  • #780 来自 @pfolex —— 未关闭

评估

难度
1/5
预计耗时
1 小时以内
新手友好度
95/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
python
领域
api

调研方向

从 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 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 分钟
30 天内合并 PR
2

环境准备

这个项目没有提供开发容器、Dockerfile 或贡献指南,环境需要你自己搭建:先看它的 README,通用步骤见我们的新手贡献指南。

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

ollama/ollama-python 的其他 Issue

查看 ollama/ollama-python 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。