Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:CrewAIInc CrewAI ContextualAI Query Tool

From Leeroopedia
Knowledge Sources
Domains Tools, RAG, ContextualAI
Last Updated 2026-02-11 00:00 GMT

Overview

Concrete tool for querying Contextual AI RAG agents with document access provided by CrewAI.

Description

The ContextualAIQueryTool class extends BaseTool to query Contextual AI RAG agents that have access to uploaded documents. Before querying, the tool checks if documents in the specified datastore are ready (not in "processing" or "pending" status) via the _check_documents_ready method. If documents are still processing, it asynchronously polls every 30 seconds for up to 20 attempts using asyncio, with nest_asyncio support for nested event loop contexts such as Jupyter notebooks. The query is sent via contextual_client.agents.query.create with a user message, and the tool extracts content from various response formats (content, message, messages). Requires the contextual-client package and an api_key.

Usage

Use this tool when CrewAI agents need to perform document-based question answering through Contextual AI RAG agents, especially in production workflows where document readiness must be verified before querying.

Code Reference

Source Location

  • Repository: CrewAI
  • File: lib/crewai-tools/src/crewai_tools/tools/contextualai_query_tool/contextual_query_tool.py
  • Lines: 1-119

Signature

class ContextualAIQueryTool(BaseTool):
    name: str = "Contextual AI Query Tool"
    description: str = "Use this tool to query a Contextual AI RAG agent..."
    args_schema: type[BaseModel] = ContextualAIQuerySchema
    api_key: str
    contextual_client: Any = None

    def __init__(self, **kwargs): ...
    def _check_documents_ready(self, datastore_id: str) -> bool: ...
    async def _wait_for_documents_async(self, datastore_id: str, ...) -> bool: ...
    def _run(self, query: str, agent_id: str, datastore_id: str | None = None) -> str: ...

Import

from crewai_tools import ContextualAIQueryTool

I/O Contract

Inputs

Name Type Required Description
api_key str Yes Contextual AI API key (constructor)
query str Yes Natural language query to send to the RAG agent
agent_id str Yes ID of the Contextual AI agent to query
datastore_id str or None No Optional datastore ID for document readiness verification

Outputs

Name Type Description
_run() returns str Agent response content from the RAG query, or error message

Usage Examples

Basic Usage

from crewai_tools import ContextualAIQueryTool

tool = ContextualAIQueryTool(api_key="your-api-key")
result = tool.run(
    query="What are the key findings in the Q4 report?",
    agent_id="agent-abc123",
    datastore_id="ds-xyz789"
)

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment