Skip to content

[correctness/medium] OllamaEmbedder.embed_query() raises IndexError on empty or whitespace-only input #67

Description

@tomharris

embed() returns [] when all inputs are empty or whitespace-only (embedder.py:44-46).
embed_query() subscripts the result with [0] unconditionally, so a blank query raises a bare
IndexError rather than a meaningful error. The batch path already handles empty inputs correctly
by substituting zero vectors — only the single-query wrapper is affected.

Location: devrag/ingest/embedder.py:81-83

Suggested fix:

Decide the intended contract and make it explicit. Either reject blank queries with a clear error:

    def embed_query(self, text: str) -> list[float]:
        if not text.strip():
            raise ValueError("embed_query() requires non-empty text")
        return self.embed([text])[0]

or mirror the batch path's zero-vector behavior for consistency:

    def embed_query(self, text: str) -> list[float]:
        result = self.embed([text])
        return result[0] if result else [0.0] * self.dim

The first is preferable if a blank query indicates a caller bug worth surfacing; the second if
blank queries are expected input that should degrade quietly. Whichever is chosen, callers of
embed_query() should be checked for their assumption about the return value.

Source: Surfaced by /engineer-agent audit-code (June run); re-verified by hand against HEAD d97b2eb on 2026-07-15, confidence high.

Metadata

Metadata

Assignees

No one assigned

    Labels

    auditSurfaced by /engineer-agent audit-codeaudit:correctnessAudit finding: correctness

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions