Skip to content

Cortex Analyst

solver

CortexAnalystSolver: a Solver that answers questions with Snowflake Cortex Analyst.

CortexAnalystSolver

CortexAnalystSolver(
    client: CortexTransport,
    *,
    semantic_view: str | None = None,
    semantic_model_file: str | None = None,
)

A Solver that sends a question to Cortex Analyst and returns the SQL it generates.

Configure the solver.

Parameters:

Name Type Description Default
client CortexTransport

The Cortex Analyst client to send questions through.

required
semantic_view str | None

A fully qualified semantic view, e.g. "DB.SCHEMA.VIEW".

None
semantic_model_file str | None

A stage path to a semantic-model YAML, e.g. "@db.schema.stage/model.yaml".

None

Raises:

Type Description
ValueError

If not exactly one of semantic_view/semantic_model_file is given.

solve

solve(case: EvalCase) -> SolverOutput

Produce SQL for case by asking Cortex Analyst its input question.

Returns:

Type Description
SolverOutput

A SolverOutput carrying the generated SQL and telemetry, or a typed SolverError

SolverOutput

on an expected transport, HTTP, or no-SQL failure.

client

CortexAnalystClient: a thin HTTP client for the Snowflake Cortex Analyst REST endpoint.

CortexTransport

Bases: Protocol

Sends a question to Cortex Analyst and returns the raw JSON reply or a typed error.

send

send(
    question: str, semantic_ref: dict[str, str]
) -> dict[str, Any] | SolverError

Send question against semantic_ref, returning the JSON body or a SolverError.

CortexAnalystClient

CortexAnalystClient(
    *,
    host: str,
    token_provider: Callable[[], str],
    session: Session | None = None,
    timeout: float = 60.0,
)

Sends a question to the Cortex Analyst REST endpoint and returns the raw JSON reply.

Configure the client.

Parameters:

Name Type Description Default
host str

The Snowflake account host, e.g. "myacct.snowflakecomputing.com".

required
token_provider Callable[[], str]

Returns the session token to place in the Authorization header, read fresh on each call.

required
session Session | None

The requests.Session to send through, or None to create one.

None
timeout float

Per-request timeout in seconds.

60.0

from_connection classmethod

from_connection(
    connection: SnowflakeConnection,
    *,
    timeout: float = 60.0,
) -> CortexAnalystClient

Build a client that authenticates with connection's live session token.

Parameters:

Name Type Description Default
connection SnowflakeConnection

An open Snowflake connection whose host and session token are reused.

required
timeout float

Per-request timeout in seconds.

60.0

Returns:

Type Description
CortexAnalystClient

A CortexAnalystClient bound to connection's host and token.

send

send(
    question: str, semantic_ref: dict[str, str]
) -> dict[str, Any] | SolverError

POST question against semantic_ref, returning the JSON body or a typed error.

Parameters:

Name Type Description Default
question str

The natural-language question to answer.

required
semantic_ref dict[str, str]

The semantic-model reference field, e.g. {"semantic_view": "DB.SCH.SV"}.

required

Returns:

Type Description
dict[str, Any] | SolverError

The decoded JSON response body, or a SolverError on an expected transport or HTTP

dict[str, Any] | SolverError

failure.