Skip to content

Llama.embed() calls LlamaBatch.add_sequence with old 3-arg signature; missing logits_array #2211

Description

@emptyngton

Prerequisites

  • I am running the latest code. Development is very rapid so there are no tagged versions as of now.
  • I carefully followed the README.md.
  • I searched using keywords relevant to my issue to make sure that I am creating a new issue that is not already open (or closed).
  • I reviewed the Discussions, and have a new bug or useful enhancement to share.

Expected Behavior

Llama.embed() should successfully compute embeddings when called on a model constructed with embeddings=True.

Current Behavior

Llama.embed() raises a TypeError immediately, before any embedding is computed:

TypeError: LlamaBatch.add_sequence() missing 1 required positional argument: 'logits_array'

The cause: Llama.embed() in llama_cpp/llama.py (around line 1678) calls add_sequence with three positional arguments:

self._batch.add_sequence(tokens, p_batch, logits_all)

But LlamaBatch.add_sequence in llama_cpp/_internals.py (around line 1013) requires four:

def add_sequence(
    self,
    token_array: Sequence[int],
    pos_array: Sequence[int],
    seq_ids: Sequence[Sequence[int]],
    logits_array: Sequence[bool]
)

llama_cpp/llama_embedding.py (around line 262) already calls add_sequence correctly with the four-arg shape — the call site in Llama.embed() was apparently missed during the LlamaBatch.add_sequence refactor.

Environment and Context

  • Hardware: x86_64, NVIDIA GeForce RTX 4090
  • OS: Windows 10 22H2
  • Python 3.12.9
  • llama-cpp-python 0.3.36 (CUDA 12.8 prebuilt wheel)
$ python --version
Python 3.12.9

$ pip show llama-cpp-python | findstr Version
Version: 0.3.36

Failure Information (for bugs)

This is a clean regression — LlamaBatch.add_sequence was refactored from a 3-arg signature to a 4-arg one, and the call sites were updated everywhere except in Llama.embed(). llama_embedding.py shows what the new shape should look like for the embedding code path.

Steps to Reproduce

from llama_cpp import Llama

m = Llama(model_path="path/to/model.gguf", embeddings=True)
m.embed("hello")

Result:

TypeError: LlamaBatch.add_sequence() missing 1 required positional argument: 'logits_array'

Failure Logs

Traceback (most recent call last):
  File "...\Lib\site-packages\llama_cpp\llama.py", line 1678, in embed
    self._batch.add_sequence(tokens, p_batch, logits_all)
TypeError: LlamaBatch.add_sequence() missing 1 required positional argument: 'logits_array'

Suggested fix

Mirror the call shape already used in llama_cpp/llama_embedding.py:

# In llama.py Llama.embed(), replace:
self._batch.add_sequence(tokens, p_batch, logits_all)

# With something like:
self._batch.add_sequence(
    token_array=tokens,
    pos_array=list(range(len(tokens))),
    seq_ids=[p_batch],
    logits_array=[True] * len(tokens) if logits_all else [False] * (len(tokens) - 1) + [True],
)

Workaround

Monkey-patching LlamaBatch.add_sequence to detect 3-arg legacy calls and synthesize the missing pos_array works as a stopgap. Hit while running Tencent's HY-Motion text-to-motion model, whose text encoder uses Llama.embed() against GGUF Qwen3 weights.

Activity

  1. SanjanaB123 commented on May 13, 2026

    @SanjanaB123
    Contributor

    After investigating the current codebase (0.3.23), it appears this was fixed as part of the recent embedding fixes in #2205. LlamaBatch.add_sequence now uses a 3-arg signature. Could a maintainer confirm and close this if resolved?

  2. lxcxjxhx commented on Jul 7, 2026

    @lxcxjxhx

    Hi @abetlen and maintainers,

    I'd like to work on fixing this bug. The issue is clear: Llama.embed() is calling LlamaBatch.add_sequence() with the old 3-argument signature, but the method now requires 4 arguments after the refactor.

    I've analyzed the code and found:

    • The problematic call is in llama_cpp/llama.py around line 1678
    • The correct 4-argument call pattern is already implemented in llama_cpp/llama_embedding.py around line 262
    • The fix is straightforward: update the call to match the new signature

    I'll prepare a PR with:

    1. Fix the add_sequence() call in Llama.embed()
    2. Add a regression test to ensure this doesn't break again

    Is this still needed? I want to make sure I'm not duplicating any existing work.

    Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions