Skip to content

fix(embedder): BgeM3Embedder crashes with KeyError on 'dense_vecs' when FlagEmbedding returns unexpected shape #245

Closed

Description

@piyushmakhija28

Summary

BgeM3Embedder.embed() accesses result['dense_vecs'] with a bare dict key lookup. If the installed FlagEmbedding library returns a different key name (e.g. 'dense' or 'embeddings') or if the model is loaded without dense-output support, a KeyError propagates uncaught. Because KeyError is not in _PRIMARY_UNAVAILABILITY_ERRORS, it bypasses the fallback logic and kills the ingest worker with a non-retriable 500.

Root Cause

ingestion/embedder.py around line 201:

result = self._flag_model.encode(texts, return_dense=True)
return list(result['dense_vecs'])  # ← bare key lookup, no .get() or KeyError handling

KeyError is excluded from the fallback chain by design (it is not an infrastructure failure), so the exception propagates all the way to the HTTP layer as a 500.

Failure Scenario

  1. FlagEmbedding library is updated to rename the key from dense_vecs to dense.
  2. Worker calls BgeM3Embedder.embed([...]).
  3. result['dense_vecs'] → KeyError: 'dense_vecs'.
  4. No fallback is attempted. The ingest request returns 500.
  5. Every ingest fails until the embedder code is patched.

Also triggered when BgeM3 is configured with return_dense=False or when the model variant only supports sparse encoding.

Proposed Fix

result = self._flag_model.encode(texts, return_dense=True)

# Robust key resolution:
dense = result.get('dense_vecs') or result.get('dense') or result.get('embeddings')
if dense is None:
    raise EmbedderError(
        f"FlagEmbedding returned unexpected keys: {list(result.keys())}. "
        f"Expected 'dense_vecs'."
    )
return list(dense)

Add EmbedderError to _PRIMARY_UNAVAILABILITY_ERRORS so the fallback embedder is tried automatically on model-output format changes.

Activity

  1. added
    bugSomething isn't working
    priority: highData loss, security, or major correctness bug
    code-reviewFound via automated code review
    on Jun 21, 2026
  2. piyushmakhija28 commented on Jun 21, 2026

    @piyushmakhija28
    ContributorAuthor

    Fixed in PR #280 (Sprint R3/R4: fix(sprint-r3): root-cause fixes for all 39 bugs from code review round 3 + Sprint R4 round 4)

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

    bugSomething isn't workingcode-reviewFound via automated code reviewpriority: highData loss, security, or major correctness bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions