Hacktoberfest 2026: những issue maintainer đã đánh dấu cho tháng Mười, đang mở và phù hợp người mới. Xem issue Hacktoberfest

GenerateResponse/ChatResponse silently drop the server's prompt_eval_cached_count field

Đang mở Phù hợp với người mới
#779 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

@pfolex đang làm issue này rồi.

Từ ngày 24/10/2026.

  • #780 của @pfolex — đang mở

Đánh giá

Độ khó
1/5
Thời gian dự kiến
Dưới một giờ
Mức phù hợp với người mới
95/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
python
Lĩnh vực
api

Hướng nghiên cứu

Bắt đầu trong ollama/_types.py và kiểm tra BaseGenerateResponse, lớp cơ sở chung của GenerateResponse và ChatResponse. Thêm trường tùy chọn prompt_eval_cached_count bên cạnh các trường đánh giá prompt hiện có, sau đó chạy bản tái hiện tối thiểu của issue với GenerateResponse.model_validate() và kiểm tra model_dump(). Thay đổi hoàn tất khi cả hai loại phản hồi vẫn giữ trường từ máy chủ và vẫn tương thích với các phản hồi không có trường này.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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

Ngôn ngữ chính
Python
Star
10.6k
Fork
1.2k
Merge trung bình
1 giờ 45 phút
Pull request đã merge (30 ngày)
2

Chuẩn bị môi trường

Dự án này không cung cấp dev container, Dockerfile hay hướng dẫn đóng góp, nên bạn cần tự thiết lập môi trường: hãy bắt đầu từ README và xem hướng dẫn đóng góp lần đầu của chúng tôi để biết các bước chung.

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của ollama/ollama-python

Tất cả issue của ollama/ollama-python

Issue tương tự

Thêm issue về Python

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.