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

Seven llama_sampler_init_* bindings admit keyword arguments that the ctypes function object silently drops

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

Chưa có ai nhận issue này.

Đánh giá

Độ khó
2/5
Thời gian dự kiến
1-3 giờ
Mức phù hợp với người mới
88/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 llama_cpp/llama_cpp.py tại bảy định nghĩa llama_sampler_init_* quanh các dòng 4553-4606, sau đó đọc ctypes_function trong llama_cpp/_ctypes_extensions.py:146-152 để xác nhận cách các hàm được export được tạo ra. Đặt bảy tham số binding thành positional-only, giống như các định nghĩa xung quanh, và xác minh rằng các chữ ký của chúng không còn khuyến khích việc gọi bằng keyword.

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

Mô tả

Summary

In llama_cpp/llama_cpp.py, seven bindings are declared as defs whose parameters are NOT
positional-only, while every other decorated binding in the file uses /:

line (main d736646) def
4553 def llama_sampler_init_dist(seed: int) -> llama_sampler_p
4560 def llama_sampler_init_top_k(k: int) -> llama_sampler_p
4570 def llama_sampler_init_top_p(p: float, min_keep: int) -> llama_sampler_p
4580 def llama_sampler_init_min_p(p: float, min_keep: int) -> llama_sampler_p
4590 def llama_sampler_init_typical(p: float, min_keep: int) -> llama_sampler_p
4596 def llama_sampler_init_temp(t: float) -> llama_sampler_p
4606 def llama_sampler_init_temp_ext(t: float, delta: float, exponent: float) -> llama_sampler_p

ctypes_function (llama_cpp/_ctypes_extensions.py:146-152) replaces the def with the
ctypes function object:

def decorator(f):
    if enabled:
        func = getattr(lib, name)
        func.argtypes = argtypes
        func.restype = restype
        functools.wraps(f)(func)
        return func

so the name the module exports IS getattr(lib, "llama_sampler_init_top_k"). A ctypes
function object without paramflags binds no argument by name, but functools.wraps and
the (f: F) -> F typing make inspect.signature, pyright and IDEs show the def's
signature — which invites keyword calls the object cannot honour.

What happens (CPython 3.12.10, ctypes)

>>> import ctypes; lib = ctypes.CDLL(None); f = lib.abs
>>> f.argtypes = [ctypes.c_int]; f.restype = ctypes.c_int
>>> f(-3, x=1)        # an extra keyword is DROPPED silently
3
>>> f(x=-3)           # all-keyword: refused
TypeError: this function takes at least 1 argument (0 given)

Applied to these bindings:

  • llama_sampler_init_top_k(k=40) — type-checks clean, raises TypeError at run time.
  • llama_sampler_init_top_p(0.9, min_keep=2) — type-checks clean, runs, and passes ONE
    positional to a two-parameter C function: min_keep is silently dropped and the C side
    reads whatever the register/stack held for it.

No caller inside the repository is affected today (_internals.py:705-732 and the server
pass positionals), so this is a latent API hazard rather than a current bug — but the
signature is public and the failure is invisible to every static check.

Fix

Make the seven defs positional-only, as the other bindings already are:

def llama_sampler_init_top_k(k: int, /) -> llama_sampler_p: ...

(Alternatively, build the functions through a CFUNCTYPE prototype with paramflags,
which does bind by name — but the one-character fix matches the file's convention.)

Found with SemLinker, a cross-language contract checker we are developing; happy to send a PR.

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

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

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 abetlen/llama-cpp-python

Tất cả issue của abetlen/llama-cpp-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.