QueryCatalog
relationalai.agent.cortex.queries
QueryCatalog(*query_funcs: Callable, model: Optional[rai.Model] = None)Query provider that registers Python functions as executable queries.
Extracts metadata from Python callables (functions, methods) including their docstrings, type annotations, and source code. This metadata is exposed to Cortex agents for query discovery, while the functions remain executable for query execution.
Query functions should follow these conventions:
- Return type: Must return either
rai.Fragmentorpandas.DataFrame - Parameters: Can have any parameters with type annotations (recommended)
- Docstring: Should include a clear description of what the query does
- Function name: Will be used as the query ID (e.g.,
"top_customers") (the name"dynamic"is reserved forDynamicQueries)
Parameters
Section titled “Parameters”
(*query_funcsCallable, default:()) - Variable number of Python callables to register as queries. The function name becomes the query id. The id"dynamic"is reserved forDynamicQueries.
(modelrelationalai.semantics.Model, default:None) - When provided, the catalog also exposes a dynamic-query path backed by aDynamicQueriesover the same model — the agent can author ad-hoc queries at runtime when no catalog query fits. Equivalent to wrapping the catalog inCompositeQueries(QueryCatalog(...), DynamicQueries(model)). Omittingmodelkeeps catalog-only behavior.
Examples
Section titled “Examples”Catalog-only:
from functools import partial
def top_customers(model, limit=10): return model.query(...)
queries = QueryCatalog(partial(top_customers, jaffle_model))Catalog + dynamic queries (agent can also author ad-hoc specs):
queries = QueryCatalog(partial(top_customers, jaffle_model), model=jaffle_model)Methods
Section titled “Methods”.get_available_queries()
Section titled “.get_available_queries()”QueryCatalog.get_available_queries() -> dictReturn metadata for all registered queries.
Returns:
dict- Dict with"queries","query_instructions", and"query_mode"("catalog"for a pure catalog,"dynamic"when the catalog was built withmodel=). The latter case also includes"schema"and"spec_format"from the embeddedDynamicQueries.
.explain()
Section titled “.explain()”QueryCatalog.explain(query_id: str) -> Optional[str]Return source code for a registered query by ID.
Parameters:
(query_idstr) - Unique identifier for the query to explain.
Returns:
str or None- Source code string, orNoneif the query ID is not found.
.execute()
Section titled “.execute()”QueryCatalog.execute(query_id: str, args: dict) -> Union[rai.Fragment, pandas.DataFrame]Execute a registered query by ID.
Parameters:
(query_idstr) - ID of the query to execute.
(argsdict) - Arguments to pass to the query function.
Returns:
relationalai.semantics.Fragment or pandas.DataFrame- Query results.
Raises:
KeyError- Ifquery_idis not registered.