Beam Search over Efficient llama.cpp generations + Grammar constraints
This package enables combined beam search and grammmatical constraints on llama.cpp models.
Theory
Our rough idea was to get a complete (holistic) probabilistic view of the tokens and prune accordingly. To save on the search space, we only take $f$ expansions, or go depth first.
Example Usage
from llama_cpp_beamsearch.completion import BeamSearchCompletion
from llama_cpp_beamsearch.config import BeamSearchConfig
from llama_cpp import Llama, LlamaGrammar, ChatCompletionRequestMessage
import re
model = Llama.from_pretrained(
repo_id="unsloth/Qwen3-0.6B-GGUF",
filename="*Q4_0.gguf",
verbose=False,
logits_all=True,
)
allowed_tokens = ["Wrench", "Screwdriver", "Ornament"]
allowed_tokens_str = " | ".join(f'"{t}"' for t in allowed_tokens)
grammar = LlamaGrammar(
_grammar=f"""
root ::= allowed_tokens
start ::= ( allowed_tokens " " )*
allowed_tokens ::= {allowed_tokens_str}
"""
)
config = BeamSearchConfig(
max_depth=1,
end_token=None,
k_progress=[2, 3],
)
completion = BeamSearchCompletion(model, grammar, config)
message = "Hello?"
result = completion.completion_beam_search(message, max_tokens=5)
assert re.match(r"^((Wrench|Screwdriver|Ornament) ?)*$", result)
Install
pip install llama-cpp-beam-search
pip install git+https://github.com/Dakantz/llama-cpp-python.git@main
# OR
uv add llama-cpp-beam-search
uv pip install git+https://github.com/Dakantz/llama-cpp-python.git@main
Multi-Platform Compatibility
You can prepend the variable to trigger the llama-cpp-python build for your specific platform/accelerator (see here).
# MacOS + PIP
CMAKE_ARGS="-DGGML_METAL=on" pip install git+https://github.com/Dakantz/llama-cpp-python.git@main
# MacOS + UV
CMAKE_ARGS="-DGGML_METAL=on" uv pip install git+https://github.com/Dakantz/llama-cpp-python.git@main
# CUDA + PIP
CMAKE_ARGS="-DGGML_CUDA=on" pip install git+https://github.com/Dakantz/llama-cpp-python.git@main
# CUDA + UV
CMAKE_ARGS="-DGGML_CUDA=on" uv pip install git+https://github.com/Dakantz/llama-cpp-python.git@main
Metadata
Release files for llama-cpp-beam-search 0.1.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| llama_cpp_beam_search-0.1.2.tar.gz | 8.0 kB | With provenance | Details for llama_cpp_beam_search-0.1.2.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| llama_cpp_beam_search-0.1.2-py3-none-any.whl | Python 3 | none | any | Details for llama_cpp_beam_search-0.1.2-py3-none-any.whl |
Total release size: 17.0 kB
Release files / llama_cpp_beam_search-0.1.2.tar.gz
| Download URL | llama_cpp_beam_search-0.1.2.tar.gz |
|---|---|
| Size | 8.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
931cffa97dd43b38a48b5242554e2c08ad85e0f1965963d36b2f5108c1831f90
|
|
BLAKE2b-256 checksum How to use checksums |
6ded570b893963a3a32a7007ee7ed83e4dc00190b8720aedd3c477f999d094b3
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Sep 14, 2026.
Transparency logRelease files / llama_cpp_beam_search-0.1.2-py3-none-any.whl
| Download URL | llama_cpp_beam_search-0.1.2-py3-none-any.whl |
|---|---|
| Size | 9.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
88cdf5f4805e7d4bf48b5110b87ee611056fb2645b39d70f45509bc9be8341d9
|
|
BLAKE2b-256 checksum How to use checksums |
126cdd55c49064e4f9e8a309b14e062ea55996ff6e03f306b26e9da233dcc94f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Sep 14, 2026.
Transparency logSecurity for llama-cpp-beam-search 0.1.2
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
All 2 files in this release have been uploaded with provenance metadata.
Attestation for llama_cpp_beam_search-0.1.2.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Sep 14, 2026.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.