#3040·ragas

answer_relevancy blocks the entire event loop via synchronous embed_query/embed_documents, stalling unrelated concurrent tasks under evaluate()

Author: ecalogeroCreated Sep 27, 2026Updated Sep 27, 2026
Labelsbug

[ -] I have checked the documentation and related resources and couldn't resolve my bug.

Description

AnswerRelevancy/ResponseRelevancy._ascore correctly awaits the LLM call for question generation, but then calls _calculate_score → calculate_similarity synchronously, which calls self.embeddings.embed_query(...) and self.embeddings.embed_documents(...) directly — the sync methods, not aembed_query/aembed_documents.

Because evaluate() runs everything on a single asyncio event loop (via asyncio.run), a blocking network call anywhere on that loop stalls every other concurrently scheduled task, regardless of RunConfig.max_workers. In practice this means: whenever any one row is computing answer_relevancy, every other row and every other metric currently in flight (faithfulness, context_precision, context_recall, etc.) freezes for the duration of that embeddings API round-trip.

Where

ragas/metrics/_answer_relevance.py (ragas 0.4.3):

python def calculate_similarity(self, question: str, generated_questions: list[str]): ... question_vec = np.asarray(self.embeddings.embed_query(question)).reshape(1, -1) # line 100 gen_question_vec = np.asarray( self.embeddings.embed_documents(generated_questions) # line 102 ).reshape(len(generated_questions), -1)

called from:

python def _calculate_score(self, answers, row) -> float: # line 114 ... cosine_sim = self.calculate_similarity(question, gen_questions) # line 126 — sync call, not awaited

async def _ascore(self, row, callbacks) -> float: # line 137 ... responses = await self.question_generation.generate_multiple(...) # correctly async return self._calculate_score(responses, row) # line 141 — the blocking call happens here

ragas/metrics/base.py shows the correct pattern is already used elsewhere in the codebase (checking hasattr(embedding_model, "embed_query") and falling back to await ...aembed_query(text) when available), so this looks like an oversight specific to _answer_relevance.py rather than a deliberate design choice.

Reproduction ragas==0.4.3, langchain-core==0.3.86, langchain-google-vertexai==2.1.2, Python 3.12 evaluate(dataset=..., metrics=[faithfulness, answer_relevancy, context_precision, context_recall], run_config=RunConfig(timeout=700, max_workers=16)) over a 22-row dataset, embeddings backed by VertexAIEmbeddings (a real network-backed embeddings provider, not a local/instant one) Traced the run with a Langfuse callback handler passed via callbacks= Observed behavior

Multiple faithfulness and context_precision tasks raised TimeoutError() from ragas.executor despite their own underlying ChatVertexAI generation calls (visible in the trace) completing in roughly 1–1.5 minutes each. In the traces, the wrapping metric span consistently ran far longer than the single LLM generation nested inside it — e.g. one context_precision sub-step showed a 10m09s wrapper duration around an 80-second model call, with no other child span accounting for the remaining ~9 minutes. On one row, two independent metrics (faithfulness and context_precision) timed out at the exact same elapsed duration (12m37s), which is very hard to explain except as two tasks stalled by the same external block and released at the same instant.

Expected behavior

answer_relevancy should not be able to stall metrics that have no dependency on it. Either the embedding calls should use the async variants (aembed_query/aembed_documents) when the embeddings object supports them (mirroring the pattern already in base.py), or the sync calls should be offloaded via asyncio.to_thread/an executor so they don't block the shared event loop.

Workaround

For now we're running answer_relevancy in its own separate evaluate() call at low max_workers, isolated from the other three metrics, so its blocking behavior only affects itself.

-- Authored by Claude