Use the asynchronous Python client

Use the asynchronous client to run independent conversations concurrently without blocking an event loop.

Mapping

SynchronousAsynchronous
CortexAgentClientAsyncCortexAgentClient
ConversationAsyncConversation
TurnAsyncTurn
conversation.send()await conversation.send()
turn.result()await turn.result()

Send a turn

from cortex_agent_sdk import (
    AgentResponse,
    AsyncConversation,
    AsyncCortexAgentClient,
    AsyncTurn,
)

async with AsyncCortexAgentClient(account="my_account", auth="<token>") as client:
    conversation: AsyncConversation = await client.conversations.create(
        origin_application="cortex_agent_sdk",
        agent={"type": "inline", "spec": {"models": {"orchestration": "auto"}}},
    )
    turn: AsyncTurn = await conversation.send("What is Snowflake Arctic?")
    answer: AgentResponse = await turn.result()
    print(answer.text)

Read events

turn: AsyncTurn = await conversation.send("Prepare an analysis.")

async for event in turn.events():
    print(event)

answer: AgentResponse = await turn.result()

Cancel, history, and attach

from cortex_agent_sdk import ConversationMessage

await turn.cancel()

history: list[ConversationMessage] = await conversation.history()
await conversation.rename("Arctic Q&A")

resumed: AsyncConversation = await client.conversations.attach(conversation.id)

Call cancel(), result(), and events() on the AsyncTurn that send() returned. turn.id is the server-assigned run ID. The root client does not recover a turn from that ID.

Concurrency and ordering

Independent conversations can run concurrently with asyncio.gather(). Calls to send() on the same conversation are serialized so each request uses the previous successful assistant message as its parent.

Next steps

Where your configuration of Cortex Code uses a model provided on the Model and Service Pass-Through Terms, your use of that model is further subject to the terms for that model on that page.

The data classification of inputs and outputs are as set forth in the following table.

Input data classificationOutput data classificationDesignation
Usage DataCustomer Data

Covered AI Features

[1]

For additional information, refer to Snowflake AI and ML.