Skip to content

[Bug] InfinityReranker closes its cached client after every rerank, so reranking silently stops after the first search #11137

Open

Description

@BlueX888

Summary

InfinityReranker caches an infinity_client.Client in self._client (libs/agno/agno/knowledge/reranker/infinity.py:46-59), but both _rerank (line 90) and arerank (line 162) enter that same cached instance as a context manager:

with self.client as client:
    ...

In the published infinity_client package, Client.__enter__/__exit__ delegate to the wrapper's internal httpx.Client and __exit__ closes it. The wrapper stays cached, so the next with self.client raises RuntimeError: Cannot reopen a client instance, once it has been closed. The broad except Exception in _rerank catches that and returns the input documents unchanged — so reranking is silently skipped with no error surfaced to the caller.

Impact

Create a Knowledge with an InfinityReranker and search more than once in the same process (e.g. two knowledge.search(...) calls, or the documented agentic-RAG pattern where an agent calls search_knowledge_base repeatedly): the first search is reranked by the Infinity server; the second and every later search logs Error connecting to Infinity server at http://<host>:<port> and returns the unmodified vector-db ordering with reranking_score=None. Users get silently degraded retrieval quality that looks like an Infinity outage while the server is healthy.

Repro

No Infinity server needed — the client is driven with httpx.MockTransport, mirroring the published infinity_client.Client context-manager contract:

import httpx

# stub `infinity_client` in sys.modules (the extra is excluded from agno[tests]
# due to an httpx pin conflict) with a wrapper whose __enter__/__exit__ delegate
# to the wrapped httpx.Client, as the published client's does, then:

requests = []
def handler(request):
    requests.append(request)
    return httpx.Response(200, json={"results": [{"index": 0, "relevance_score": 0.9},
                                                {"index": 1, "relevance_score": 0.1}]})

reranker = InfinityReranker(host="http://localhost:7997", ...)  # client built on MockTransport
docs = [Document(content="a"), Document(content="b")]

first  = reranker.rerank(docs)   # -> reranked, [("b", 0.9), ("a", 0.1)]
second = reranker.rerank(docs)   # -> ???  (same reranker instance)

Output on current main (commit 0aa5924):

first  rerank: [('b', 0.9), ('a', 0.1)]   (1 HTTP request)
second rerank: [('a', None), ('b', None)] (0 HTTP requests)
ERROR  Error connecting to Infinity server at http://localhost:7997
       RuntimeError: Cannot reopen a client instance, once it has been closed.

Expected: both calls rerank (2 HTTP requests, reranked scores both times).

The async path (arerank) behaves identically (async with self.client as client: at line 162).

Suggested fix

Use the cached client directly instead of entering it as a context manager per call — the generated helpers only need get_httpx_client() / get_async_httpx_client(), so client = self.client keeps the single cached client usable across every search (verified against the published infinity_client==0.0.77: two reranks, two requests, no reopen error).

Activity

  1. added a commit that references this issue on Oct 18, 2026
    ed92436
  2. xanyl commented on Oct 19, 2026

    @xanyl

    Hi maintainers — this looks like a genuine bug. Reading the code, InfinityReranker caches one infinity_client.Client but enters it as a context manager in both _rerank and arerank, so __exit__ closes the wrapped httpx.Client after the first call — every subsequent rerank raises RuntimeError, which the broad except Exception swallows, silently returning unranked documents.

    PR #11138 implemented this exact fix but was auto-closed by the triage bot since the issue lacks the ready label. Could this be triaged when you get a chance? Happy to open a by-the-book PR with tests once it's labeled. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions