[BUG]: Incomplete chunk validation in embed_chunks causes unhandled KeyError; eager SentenceTransformer instantiation blocks startup by ~11s
Valutazione
- Difficoltà
- 2/5
- Tempo stimato
- 1-3 ore
- Idoneità per principianti
- 25/100
- Tipo di issue
- Bug
- Chiarezza
- Specificata chiaramente
- Stato di attività
- Ferma
- Stack tecnologico
- python, pytorch
- Ambito
- backend, machine-learning
Direzione di ricerca
Start in backend/app/modules/vector_store/embed.py, where embed_chunks checks only for text before calling embedder.encode, and where the module-level SentenceTransformer is created. Validation of id, text and metadata should happen before any encode call, and the model should move into a lazy get_embedder() singleton. Pull request #172 already targets this, so check it before starting. Done means a malformed chunk raises a ValueError without running the model, and importing the module no longer loads the model.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
Fix Chunk Validation and Lazy-Load SentenceTransformer
Bug Description
There are two reliability and startup performance issues in backend/app/modules/vector_store/embed.py.
1. Incomplete Chunk Validation
The embed_chunks(chunks) function validates the input chunk structure before generating embeddings. However, the current validation checks only whether the text field exists.
Later, the function directly accesses chunk["id"] and chunk["metadata"]. If either field is missing, the validation passes, and the function executes the expensive model inference step using embedder.encode(texts) before raising an unhandled KeyError.
Expected behavior: Validate all required fields (id, text, and metadata) before generating embeddings. If a chunk is malformed, raise a descriptive ValueError immediately, without performing model inference.
2. Eager SentenceTransformer Initialization
The model is initialized unconditionally at module scope:
embedder = SentenceTransformer("all-MiniLM-L6-v2")
As a result, importing embed.py synchronously loads PyTorch and the model weights, adding approximately 11 seconds to cold-start import time.
This delay affects application initialization, health checks, and unit tests that import the module without immediately needing embeddings.
Expected behavior: Initialize the model lazily on the first call using a get_embedder() singleton. Subsequent calls should reuse the same model instance, preserving existing functionality and avoiding unnecessary initialization during module imports.
Steps to Reproduce
- Open a terminal in the
backenddirectory with the project virtual environment activated. - Run the following script:
import time
t0 = time.perf_counter()
import app.modules.vector_store.embed as embed
t1 = time.perf_counter()
print(f"Cold-start import latency: {t1 - t0:.2f}s")
malformed_chunk = [{"text": "Sample article excerpt"}]
try:
embed.embed_chunks(malformed_chunk)
except KeyError as err:
print(f"Unhandled KeyError: {err}")
Actual Behavior
- Importing the module takes approximately 11 seconds on a cold start.
- A chunk containing
textbut missingidormetadatapasses the existing validation. - The embedding model runs before the function raises an unhandled
KeyError.
Expected Behavior
- Importing the module does not initialize the SentenceTransformer model.
- The model initializes only when embeddings are first requested and is reused for subsequent calls.
- All required chunk fields (
id,text, andmetadata) are validated before model inference. - Invalid chunks raise a descriptive
ValueErrorwithout triggering embedding generation.
Logs and Screenshots
Cold-start import latency: 11.39s
Unhandled KeyError after running model inference: 'id'
Environment Details
- OS: Windows / Linux
- Python: 3.13
- File:
backend/app/modules/vector_store/embed.py - Key packages:
sentence-transformers,torch
Impact
Severity: Medium — Feature works under valid inputs but has reliability and startup performance issues.
Proposed Solution
- Validate that each chunk contains the required
id,text, andmetadatafields before generating embeddings. - Raise a descriptive
ValueErrorwhen required fields are missing or invalid. - Replace eager model initialization with a lazy
get_embedder()singleton. - Preserve the existing
embed_chunks(chunks)interface and embedding output behavior.
Code of Conduct
- I have joined the [Discord server](https://discord.gg/hjUhu33uAn) and will post updates there.
- I have searched existing issues to avoid duplicates.
- Lingua principale
- TypeScript
- Stelle
- 58
- Fork
- 90
- Merge medio
- 11h 51m
- PR unite (30g)
- 3
Preparare l'ambiente
- Nessun Dockerfile né file Docker Compose
- Nessun modello di pull request
- Leggi la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di AOSSIE-Org/Perspective
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 83/100
AOSSIE-Org/Perspective#191 ·
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 85/100
AOSSIE-Org/Perspective#190 ·
-
[BUG]: Missing input validation in URlRequest and ChatQuery causes 500 crashes and wasteful scraper executionForse già presa @agarwalpranav0711 l’ha presa 25 giorni fa. Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 76/100
AOSSIE-Org/Perspective#178 ·
-
[BUG]: chunk_rag_data crashes with ValueError when perspective is a dict, silently dropping RAG vector storageForse già presa @agarwalpranav0711 l’ha presa 36 giorni fa. Apertabug
Difficoltà 2/5 1-3 ore Idoneità per principianti 82/100
AOSSIE-Org/Perspective#175 ·
-
[BUG]: Scraper passes headers as query params in requests.get (User-Agent never sent)Forse già presa @Th-Shivam l’ha presa 50 giorni fa. Apertabug
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 92/100
AOSSIE-Org/Perspective#161 · 1 commento ·
Tutte le issue di AOSSIE-Org/Perspective
Issue simili
-
documentation lens:human needs-triage
Difficoltà 2/5 1-3 ore Idoneità per principianti 73/100
thebristolsound/birdbrain#2246 ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 78/100
I maintainer di solito rispondono entro 1 giorno
-
A voting window past about 69.9 million hours stores a deadline that reads back as no deadlineApertabug ready-for-human
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
BoardGamesEmpire/board-games-empire-backend#1452 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno
-
good first issue stretch
Difficoltà 2/5 1-3 ore Idoneità per principianti 65/100
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 76/100
I maintainer di solito rispondono entro 1 giorno