{"record":{"id":"4d4d0020c39169b6","repo":"mem0ai/mem0","slug":"llmreranker-requires-an-llm-instance-rerankerfact","errorCode":null,"errorMessage":"LLMReranker requires an LLM instance; RerankerFactory should always provide one for the llm_reranker provider.","messagePattern":"LLMReranker requires an LLM instance; RerankerFactory should always provide one for the llm_reranker provider\\.","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"mem0-ts/src/oss/src/rerankers/llm.ts","lineNumber":24,"sourceCode":"\nScore the relevance on a scale from 0.0 to 1.0, where:\n- 1.0 = Perfectly relevant and directly answers the query\n- 0.8-0.9 = Highly relevant with good information\n- 0.6-0.7 = Moderately relevant with some useful information\n- 0.4-0.5 = Slightly relevant with limited useful information\n- 0.0-0.3 = Not relevant or no useful information\n\nRespond with only a single numerical score between 0.0 and 1.0. Do not include any explanation or additional text.`;\n\nconst MAX_INPUT_LEN = 4000;\n\nexport class LLMReranker implements Reranker {\n  private llm: LLM;\n  private topK?: number;\n\n  constructor(config: RerankerConfig, llm: LLM) {\n    if (!llm) {\n      throw new Error(\n        \"LLMReranker requires an LLM instance; RerankerFactory should always provide one for the llm_reranker provider.\",\n      );\n    }\n    this.llm = llm;\n    this.topK = config.topK;\n  }\n\n  async rerank(\n    query: string,\n    documents: string[],\n    topK?: number,\n  ): Promise<RerankResult[]> {\n    if (documents.length === 0) return [];\n\n    const scored = await Promise.all(\n      documents.map(async (document, index) => {\n        try {\n          const rerankScore = await this.score(query, document);","sourceCodeStart":6,"sourceCodeEnd":42,"githubUrl":"https://github.com/mem0ai/mem0/blob/001c235229be8795e3834520467bd0d661ed8f34/mem0-ts/src/oss/src/rerankers/llm.ts#L6-L42","documentation":"Thrown by the LLMReranker constructor when it is instantiated without an LLM instance. The llm_reranker provider scores documents by prompting the configured LLM, so RerankerFactory is responsible for passing the memory instance's LLM as the second constructor argument. Seeing this error means the factory/reranker wiring was broken — it is effectively an internal invariant, not a user configuration error.","triggerScenarios":"RerankerFactory.create('llm_reranker', config) being called without an LLM (e.g. an older mem0-ts version whose factory does not forward the LLM, or custom code instantiating LLMReranker directly with new LLMReranker(config) and omitting the llm argument).","commonSituations":"Using a custom or outdated RerankerFactory; writing a custom reranker integration that bypasses the factory; upgrading only part of the monorepo so the factory and reranker signatures disagree.","solutions":["Let RerankerFactory construct the reranker via the Memory config (reranker: { provider: 'llm_reranker', config: { model: ... } }) so the LLM is injected","If constructing directly, pass the LLM: new LLMReranker(config, memoryLLM)","Update mem0ai to a matching version so factory and reranker signatures agree"],"exampleFix":"// before\nconst reranker = new LLMReranker({ topK: 5 }); // no LLM\n\n// after\nconst reranker = new LLMReranker({ topK: 5 }, memoryLLM);","handlingStrategy":"validation","validationCode":"if (!llm) throw new Error('llm_reranker requires a configured LLM instance');\nconst reranker = new LLMReranker(config, llm);","typeGuard":"const isLLM = (x: unknown): x is { generateResponse: (...a: unknown[]) => Promise<unknown> } =>\n  x != null && typeof (x as { generateResponse?: unknown }).generateResponse === 'function';","tryCatchPattern":null,"preventionTips":["Always construct rerankers through RerankerFactory / the Memory config so the LLM is injected","Keep mem0ai package versions aligned so factory and constructor signatures match"],"tags":["internal","reranker","llm","invariant","typescript"],"backgroundTag":null,"analyzedSha":"001c235229be8795e3834520467bd0d661ed8f34","analyzedAt":"2026-08-15T01:55:42.685Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}