mastra-ai/mastra · error

No relevance score found on Cohere response

Error message

No relevance score found on Cohere response

What it means

The Cohere API returned 200 but the parsed response contained no finite numeric relevance_score at results[0]. This is a defensive check against malformed or empty rerank responses (e.g. empty results array, unexpected schema from a changed API version).

Source

Thrown at packages/rag/src/rerank/relevance/cohere/index.ts:55

        Authorization: `Bearer ${this.apiKey}`,
      },
      body: JSON.stringify({
        query,
        documents: [text],
        model: this.model,
        top_n: 1,
      }),
    });

    if (!response.ok) {
      throw new Error(`Cohere API error: ${response.status} ${await response.text()}`);
    }

    const data = (await response.json()) as CohereRerankingResponse;
    const relevanceScore = data.results[0]?.relevance_score;

    if (typeof relevanceScore !== 'number' || !Number.isFinite(relevanceScore)) {
      throw new Error('No relevance score found on Cohere response');
    }

    return relevanceScore;
  }
}

View on GitHub (pinned to 75dd419e61)

Solutions

  1. Ensure the query and document text passed to getRelevanceScore are non-empty
  2. Pin/upgrade to a supported rerank model (e.g. rerank-v3.5) and matching @mastra/rag version
  3. Log the raw response to inspect why results[0].relevance_score is missing

Example fix

// before
const score = await reranker.getRelevanceScore(query, ''); // empty doc -> likely no result
// after
if (!text.trim()) return 0;
const score = await reranker.getRelevanceScore(query, text);
Defensive patterns

Strategy: fallback

Validate before calling

if (!queryText.trim() || !documents.every(d => d.text?.trim())) throw new Error('Empty rerank input');

Try / catch

try { score = await reranker.getRelevanceScore(q, t); } catch (e) { if (e.message.includes('No relevance score')) score = 0; else throw e; }

Prevention

When it happens

Trigger: Cohere returns results: [] (e.g. documents array empty or all filtered out by top_n), or the response schema no longer matches CohereRerankingResponse.

Common situations: Passing empty/whitespace text documents; using a deprecated rerank model whose response shape changed; proxy/gateway returning a success with an unexpected body.

Related errors


AI-assisted analysis of mastra-ai/mastra@75dd419e61 (2026-08-30). Data as JSON: /api/errors/871cf429eae76bd9. Report an issue: GitHub.