[BUG]: Incomplete chunk validation in embed_chunks causes unhandled KeyError; eager SentenceTransformer instantiation blocks startup by ~11s

Abierto
#171 0 comentarios 0 reacciones 0 asignados Ver en GitHub

@agarwalpranav0711 ya está trabajando en esto.

Desde el 13/10/2026.

  • #172 de @agarwalpranav0711 — abierto

Evaluación

Dificultad
2/5
Tiempo estimado
1-3 horas
Aptitud para principiantes
25/100
Tipo de issue
Error
Claridad
Bien especificado
Estado de actividad
Estancado
Stack tecnológico
python, pytorch

Línea de trabajo

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.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

bug

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
  1. Open a terminal in the backend directory with the project virtual environment activated.
  2. 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 text but missing id or metadata passes 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, and metadata) are validated before model inference.
  • Invalid chunks raise a descriptive ValueError without 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
  1. Validate that each chunk contains the required id, text, and metadata fields before generating embeddings.
  2. Raise a descriptive ValueError when required fields are missing or invalid.
  3. Replace eager model initialization with a lazy get_embedder() singleton.
  4. Preserve the existing embed_chunks(chunks) interface and embedding output behavior.
Code of Conduct
Lenguaje dominante
TypeScript
Estrellas
58
Forks
90
Merge medio
11 h 51 min
PR fusionados (30 d)
3

Preparar el entorno

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de AOSSIE-Org/Perspective

Todos los issues de AOSSIE-Org/Perspective

Issues similares

Más issues de TypeScript

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.