crewAIInc/crewAI · error · ValueError

Client is not initialized

Error message

Client is not initialized

What it means

ValueError raised in CrewAIRagAdapter.query/search: the adapter holds no knowledge-storage client (self._client is None), so the search cannot run. The client is normally attached when the adapter is wired into the underlying storage/tool; calling query before that wiring (or on a detached adapter) triggers this.

Source

Thrown at lib/crewai-tools/src/crewai_tools/adapters/crewai_rag_adapter.py:95

    ) -> str:
        """Query the knowledge base with a question.

        Args:
            question: The question to ask
            similarity_threshold: Minimum similarity score for results (default: 0.6)
            limit: Maximum number of results to return (default: 5)

        Returns:
            Relevant content from the knowledge base
        """
        search_limit = limit if limit is not None else self.limit
        search_threshold = (
            similarity_threshold
            if similarity_threshold is not None
            else self.similarity_threshold
        )
        if self._client is None:
            raise ValueError("Client is not initialized")

        results: list[SearchResult] = self._client.search(
            collection_name=self.collection_name,
            query=question,
            limit=search_limit,
            score_threshold=search_threshold,
        )

        if not results:
            return "No relevant content found."

        contents: list[str] = []
        for result in results:
            content: str = result.get("content", "")
            if content:
                contents.append(content)

        return "\n\n".join(contents)

View on GitHub (pinned to 754d7323be)

Solutions

  1. Use the adapter through the owning tool/service so the framework injects the client instead of calling it bare.
  2. Ensure any connect/initialize step completes before the first query.
  3. Guard calls with a client check (see validation below) and fail with a clear message at the call site.

Example fix

# before
adapter = CrewAIRagAdapter(...)
answer = adapter.query("what is crewai?")  # ValueError: Client is not initialized

# after
if adapter._client is None:
    raise RuntimeError("connect the RAG adapter before querying")
answer = adapter.query("what is crewai?")
Defensive patterns

Strategy: type-guard

Validate before calling

if adapter._client is None:
    raise RuntimeError("RAG adapter has no client; run its connect/initialize step first")

Type guard

def rag_adapter_ready(adapter: CrewAIRagAdapter) -> bool:
    return getattr(adapter, "_client", None) is not None

Try / catch

try:
    answer = adapter.query(q)
except ValueError as e:
    if "Client is not initialized" in str(e):
        raise RuntimeError("connect adapter before querying") from e
    raise

Prevention

When it happens

Trigger: Instantiating CrewAIRagAdapter directly and calling its query/retrieve method without connecting it to a knowledge client, or reusing an adapter after its client was closed/reset.

Common situations: Using the adapter standalone in tests; calling search before initialize/connect; copying example code that assumes a framework-managed lifecycle.

Related errors


AI-assisted analysis of crewAIInc/crewAI@754d7323be (2026-08-15). Data as JSON: /api/errors/45b1c33bfbe6b866. Report an issue: GitHub.