d+
devoteam
Snowflake Partner

API reference

User Guide · Reference

The public Python API of streamlit-coco, as exported from streamlit_coco (see the index for the exact version this guide describes). Private modules (bridge, tool_cards, tool_extract, and others not listed here) are implementation details, not stable API — they are not documented as reference entries.

CocoOptions#

The configuration dataclass for a session or a query() call (streamlit_coco/options.py).

FieldTypeDefaultEffect
connection`str \None`NoneSnowflake CLI connection name to use
cwdstr"."Working directory the agent's tools operate in
model`str \None`NoneModel selection; prefer "auto" when set
allowed_toolslist[str][]Tools that may run without approval
disallowed_toolslist[str][]Tools blocked entirely
permission_modestr"default"Overall permission mode, e.g. "plan" for ExitPlanMode flows
profile`str \None`NoneSDK profile name
cli_path`str \None`NoneOverride the cortex CLI path (CORTEX_CODE_CLI_PATH env also works)
mcp_serversdict{}MCP server configuration forwarded to the SDK
hooksdict{}SDK hooks passthrough
require_approval_for`list[str] \Callable`[]Tools that pause for human approval; AskUserQuestion/ExitPlanMode always pause
output_schema`dict \None`NoneJSON Schema forwarded to the SDK as output_format ($schema/$id stripped before forwarding)
max_turns`int \None`NoneCaps the number of agent turns
approval_timeout_secondsfloat600.0Seconds to wait for a pending approval before ApprovalTimeoutError
extra_sdk_optionsdict{}Passthrough kwargs forwarded to the SDK's own options object

Methods#

  • options_hash() -> str — a stable hash of the options that changes only

    when a value affecting the running session changes (used to decide whether the background worker must restart).

  • to_sdk_options(*, can_use_tool=None) — builds the SDK-native options

    object; raises SDKNotInstalledError if the SDK is not installed.

  • auto_allow_tools() -> list[str] — the effective set of tools that run

    without approval.

  • tools_requiring_approval() -> list[str] — the effective set of tools that

    pause for approval.

python
options = st_coco.CocoOptions(
    connection="my_connection",
    cwd=".",
    allowed_tools=["Read", "Glob", "Grep"],
    require_approval_for=["Edit", "Write", "Bash"],
    approval_timeout_seconds=300.0,
)

Used by: I want to build a minimal CoCo app

CocoRunStatus#

A str enum (streamlit_coco/session.py) giving a session's current state.

ValueMeaning
idleNo session activity started yet
connectingBackground worker starting
readyConnected, waiting for a prompt
runningA turn is in progress
awaiting_userA tool call is paused on a pending approval
completedThe turn finished normally
errorThe turn or the session failed
cancelledThe turn was cancelled
python
if session.chat_result().status == st_coco.CocoRunStatus.awaiting_user:
    st.warning("Waiting on an approval.")

Used by: Concepts

CocoChatResult#

A dataclass snapshot of one turn's outcome, returned by panel(), chat() and session.chat_result().

FieldTypeDefaultEffect
last_prompt`str \None`—The most recent prompt sent
last_result`CocoEvent \None`—The most recent terminal event
pending_approval`dict \None`—The pending approval request, if any
structured_outputAny—The last structured payload, if output_schema was set
eventslist[CocoEvent]—All events for the session so far
statusCocoRunStatus—The current run status
python
result = session.chat_result()
if result.pending_approval:
    st.info(f"Waiting on: {result.pending_approval['tool_name']}")

Used by: I want to configure tool approvals

CocoSession#

A multi-turn session backed by a CortexCodeSDKClient on a background thread (streamlit_coco/session.py).

MemberEffect
send(prompt)Queue a prompt; raises SessionStartError/SessionNotReadyError if not ready
start()Start the background worker
ensure_ready(timeout=120)Await readiness; raises SessionNotReadyError on timeout or boot failure
run(prompt, *, timeout=None)Send a prompt and await its result
stream()Async-iterate events for the current/next turn
set_permission_mode(mode)Change the permission mode
execute_plan(...)Execute a plan produced under permission_mode="plan"
cancel()Cancel the in-flight turn
close()Tear down the background worker
reset()Clear the transcript and restart the worker
chat_result()Return the current CocoChatResult
is_running / is_ready / is_connecting / needs_pollingBoolean state properties
transcript / messages / eventsThe accumulated turn/event history
last_error / init_info / structured_outputDiagnostics and the last structured payload
permission_managerThe session's approval-state manager
add_event_listener(cb)Register a callback invoked per new event
sync_options(options)Apply new CocoOptions; restarts the worker if options_hash() changed
set_show_structured_inline(bool)Toggle inline structured-output rendering
python
session = st_coco.CocoSession(options=options, key="copilot")
session.start()
result = await session.run("List the tables in my schema.")

Used by: I want to run CoCo headless

Session registry helpers#

FunctionEffect
`get_session(key) -> CocoSession \None`Look up an existing session by key in the process registry
get_or_create_session(options, *, key="coco", sync_options=True) -> CocoSessionStreamlit helper: get or create a session in st.session_state[key]
reset_session(options, session_key="coco", warm_up=False) -> CocoSessionReset (or create) the session at session_key
stop_session(session_key="coco", gate_key="coco_started") -> NoneTear down the session and clear the start-gate flag
python
session = st_coco.get_or_create_session(options, key="coco")

Used by: I want to build a minimal CoCo app

UI — preferred pattern#

panel(session, **kwargs) -> CocoChatResult#

Renders the native transcript or field output, approval controls, and a Stop button for a session.

ParameterTypeDefaultEffect
output_modestr"transcript""transcript" or "field"
output_label`str \None`"CoCo output"None hides the bordered output container
show_tool_detailsboolTrueShow tool card detail
show_thinkingboolFalseShow thinking blocks
show_approvalsboolTrueRender the approval controls
show_stopboolTrueShow the Stop button while polling
show_statusboolTrueShow connect/turn status chrome
show_plan_bannerboolTrueShow the plan-mode banner and Execute plan CTA
warm_upboolFalseCall session.start() on mount when idle
status_expandedstr"auto""auto" / "always" / "never"
approval_key_prefixstr"coco"Widget key namespace
use_fragmentboolTruePoll inside an @st.fragment
run_everyfloat0.25Poll interval in seconds; paused while an approval is pending
text_renderer`str \Callable \None`None"markdown" (default) / "write" / "text" / "caption" / "code" or a callable
show_copyboolTrueShow clipboard controls
max_messages`int \None`NoneTranscript window; Load earlier reveals older items
preview_chars`int \None`NoneCap user/assistant text to the first N characters
on_structured_output`Callable[[dict, CocoChatResult], None] \None`NoneCalled when a structured payload arrives
structured_output_containercontainer \NoneNoneContainer to render structured output into
python
st_coco.panel(session, output_mode="transcript", show_thinking=False)

Used by: I want to build a minimal CoCo app

copilot_rail(session, *, title="Copilot", …) -> None#

A right-rail Copilot around panel(), for multipage apps.

ParameterTypeDefaultEffect
sessionCocoSession—The session to render
connectedboolTrueWhether the rail shows as connected
connections`list \None`NoneConnection choices, if switching is offered
toml_file`str \None`NoneSnowflake config file to read connections from
on_connect / on_disconnect`Callable \None`NoneConnection lifecycle callbacks
job`dict \None`NoneA queued job: {prompt, label, status, expect_structured}
on_job_sent / on_job_finished`Callable \None`NoneJob lifecycle callbacks
show_copyboolFalseShow clipboard controls
show_transcript_filtersboolTrueShow the transcript display-config popover
example_questions`list \None`None{title, question} dicts or (title, question) tuples
deferredboolFalseDefer rendering (also settable per example question)
python
st_coco.copilot_rail(session, title="Copilot", example_questions=[
    ("Summarize", "Summarize this sprint."),
])

Used by: Example apps reference

transcript_display_config / transcript_view_pills#

transcript_display_config(*, key=…, last_n=8, preview_chars=200) -> tuple[int | None, int | None] — the display-settings icon popover (:material/display_settings:) with Last messages / First n characters pills and sliders.

transcript_view_pills(*, key=…, last_n=8, preview_chars=200) — the same pills as a standalone widget, without the popover chrome.

python
last_n, preview_chars = st_coco.transcript_display_config(key="rail")

Used by: Concepts

app_viewer(app_dir, *, key="coco_app_viewer", …) -> None#

Embeds a live preview of an app the agent just wrote.

ParameterTypeDefaultEffect
app_dirstr— (required)Folder containing streamlit_app.py
keystr"coco_app_viewer"Widget key namespace
port`int \None`NoneFirst free port from 8511–8520 if unset
addressstr"127.0.0.1"Bind address for the preview process
env`dict \None`NoneExtra environment variables for the child process
iframe_heightint520Preview iframe height in pixels
show_fixboolTrueShow the "Fix with CoCo" action
on_fix`Callable[[str], None] \None`NoneCallback receiving the fix prompt
on_close`Callable \None`NoneCallback when the preview closes
title_extra`str \None`NoneExtra text appended to the title
titlestr"Preview"Panel title
script_namestr"streamlit_app.py"Entry-point filename inside app_dir

Streamlit-free helper functions: start_app_preview, stop_app_preview, preview_running, preview_url, last_preview_exception, preview_log_tail, default_fix_prompt(traceback, app_dir) -> str.

python
st_coco.app_viewer("examples/app_builder/out/my-app", title="Preview")

Used by: I want to run the example apps

chat_input_bar(session, *, placeholder=…, connecting_placeholder=…, key=None, accept_file=False, …) -> str | None#

The chat input bar, with optional file attachment.

ParameterTypeDefaultEffect
accept_file`bool \str`FalseTrue or "multiple" to accept uploads
file_type`list[str] \None`NoneRestrict accepted extensions
max_upload_size`int \None`NoneOverride the default max upload size
upload_subdirstr"_uploads"Subdirectory of cwd uploads are written to
upload_overwritestr"replace""error" / "replace" / "skip"
inject_upload_pathsboolTrueAppend uploaded file paths to the prompt text
python
prompt = st_coco.chat_input_bar(session, accept_file=True, upload_overwrite="replace")

Used by: I want to add file uploads

Upload helpers#

upload_to_cwd(target, files, *, subdir="_uploads", overwrite="error", max_bytes=DEFAULT_MAX_BYTES, allowed_extensions=DEFAULT_ALLOWED_EXTENSIONS) -> list[UploadedPath]#

Writes uploaded files into a session's, options', or path's working directory. target may be a path, a CocoOptions, or a CocoSession. Raises CwdUploadError on a bad filename (path-traversal guard), a disallowed extension, an oversize payload, or an overwrite conflict.

python
paths = st_coco.upload_to_cwd(session, files, overwrite="error")

Used by: I want to add file uploads

cwd_uploader(target, *, label=…, overwrite="error", …) -> list[UploadedPath]#

A sidebar/chrome st.file_uploader wired to upload_to_cwd(), with an inventory caption of files already uploaded.

python
uploaded = st_coco.cwd_uploader(session, label="Upload a reference file")

Used by: I want to add file uploads

Upload constants (streamlit_coco/upload.py)#

NameTypeDefaultEffect
DEFAULT_UPLOAD_SUBDIRstr"_uploads"Default upload subdirectory name
DEFAULT_MAX_BYTESint10 * 1024 * 1024 (10 MiB)Default per-file size limit
DEFAULT_ALLOWED_EXTENSIONSfrozenset[str]a fixed allowlistDefault allowed file extensions; read the source constant for the exact list rather than assuming one

Used by: I want to add file uploads

Approvals and prompting#

send_prompt(session, prompt) -> None#

Strips and queues a prompt on a session; surfaces any CocoError via st.error instead of raising into the page.

python
st_coco.send_prompt(session, "Summarize this sprint.")

Used by: I want to build a minimal CoCo app

render_approvals(session, *, key_prefix="coco") -> bool#

Renders the HITL approval UI for a pending request. Button order: **Approve once · Always allow · Deny**. AskUserQuestion and ExitPlanMode requests never show Always allow.

python
st_coco.render_approvals(session, key_prefix="coco")

Used by: I want to configure tool approvals

render_plan_banner(session, …) -> bool#

Renders the plan-mode banner with an Execute plan call to action.

python
st_coco.render_plan_banner(session)

Used by: I want to configure tool approvals

approve_pending(session, request_id, *, always=False, updated_input=None) / deny_pending(session, request_id, *, reason=None)#

Low-level resolvers for a pending approval, for custom approval UIs.

python
st_coco.approve_pending(session, request_id, always=False)
st_coco.deny_pending(session, request_id, reason="not now")

Used by: I want to configure tool approvals

Display helpers#

FunctionEffect
render_transcript(session, …, text_renderer=None)Render the transcript standalone (what panel() uses internally)
render_output_field(session, …, text_renderer=None)Render only the latest output field
render_session_status(session, …)Render the status/connect chrome standalone
get_latest_assistant_text(session) -> strReturn the latest assistant text as a plain string
python
latest = st_coco.get_latest_assistant_text(session)

Used by: Concepts

Bootstrap and gate#

render_environment_status(env=None, *, connection=None, toml_file=None, stacked=False, show_title=True)#

Renders the SDK/CLI/Snowflake-config status block, given a CocoEnvironment (or probing one itself if env is omitted).

python
st_coco.render_environment_status(connection="my_connection")

Used by: Getting started

render_start_gate(options, *, session_key=…, gate_key=…, warm_up=True, env=None) -> bool#

Renders the landing screen (environment status plus a Start action) and returns False while the environment is not ready or the user has not confirmed. The caller must call st.stop() on False — the gate does not stop the script itself.

python
if not st_coco.render_start_gate(options):
    st.stop()

Used by: I want to build a minimal CoCo app

Legacy CCv2#

chat(**kwargs) -> CocoChatResult#

An all-in-one Custom Component v2 widget with a built-in input, predating panel(). Prefer panel() for new apps. Notable parameters: session / options, key, height, placeholder, use_fragment, run_every, output_mode, on_structured_output, and optional on_event / submit / approval / cancel callbacks.

python
result = st_coco.chat(options=options, key="legacy")

Used by: I want to run the example apps

request_input(question, *, key="coco_input", default="", schema=None, …) -> str | dict | None#

An app-owned clarification form shown between turns — not a CCv2 channel. Returns a dict when schema= is given, otherwise a str (or None if unanswered).

python
answer = st_coco.request_input("Which environment?", schema={"type": "string"})

Used by: Example apps reference

Headless#

async query(prompt, *, options=None, output_schema=None) -> AsyncIterator[CocoEvent]#

A single-turn wrapper that runs without any session object or Streamlit import. Raises SDKNotInstalledError, QueryError, or another CocoError subclass on failure.

python
async for event in st_coco.query("List the tables in my schema.", options=options):
    ...

Used by: I want to run CoCo headless

events_to_dataframe(events) -> DataFrame#

Flattens a list of CocoEvent into a pandas DataFrame (requires pandas installed).

python
df = st_coco.events_to_dataframe(session.events)

Used by: I want to run CoCo headless

CocoEvent#

A normalized, JSON-serializable unit of agent activity (streamlit_coco/messages.py), with to_dict() / from_dict() for storage or transport.

FieldEffect
typeOne of assistant_text, stream_event, thinking, tool_use, tool_result, permission_request, result, system, error
text / deltaText content or streamed delta
name / tool_use_id / tool_name / tool_input / inputTool-call identification and arguments
content / is_errorTool result content and error flag
request_idIdentifies a pending approval request
subtype / duration_ms / cost_usdResult metadata
structured_outputThe structured payload, when present
message / code / metadataError/system detail
python
event = st_coco.CocoEvent.from_dict(raw)
event.to_dict()

Used by: Concepts

Environment and errors#

check_environment(*, connection=None, cli_path=None, toml_file=None) -> CocoEnvironment#

Probes the SDK, CLI and Snowflake config. Never raises.

Field / propertyEffect
sdk_installed / sdk_versionWhether the SDK is importable, and its version
cli_path / cli_versionThe resolved CLI path and its reported version
snowflake_config_file / connection_hint / toml_fileSnowflake config resolution detail
readyTrue when everything needed to start a session is present
cli_okTrue when the CLI probed successfully
snowflake_config_found / snowflake_config_display / snowflake_config_missing_labelSnowflake config presence and display text
python
env = st_coco.check_environment(connection="my_connection")

Used by: Getting started

require_environment(*, connection=None, cli_path=None, toml_file=None, require_snowflake_config=False) -> CocoEnvironment#

Like check_environment(), but raises a typed error instead of returning a not-ready result.

python
env = st_coco.require_environment(connection="my_connection", require_snowflake_config=True)

Used by: Getting started

Snowflake config helpers#

FunctionEffect
list_snowflake_toml_files() -> list[Path]*.toml files under ~/.snowflake/, connections.toml/config.toml first
list_snowflake_connections(toml_file=None) -> list[str]Profile names; legacy config.toml reads [connections], other files read top-level tables
resolve_snowflake_config_path(toml_file=None, *, must_exist=True)Resolves a bare filename or path to a config file under ~/.snowflake/
default_snowflake_connection_name(toml_file=None)The connection name used when none is given
python
files = st_coco.list_snowflake_toml_files()

Used by: Getting started

Exception hierarchy (streamlit_coco/errors.py)#

ClassRaised whenMessage
CocoErrorBase class for every exception below—
SDKNotInstalledError (also ImportError)The SDK is not installedcortex-code-agent-sdk is required. Install with: pip install streamlit-coco[sdk]
CLINotFoundErrorThe cortex CLI is not on PATHCoCo CLI not found on PATH... or set CORTEX_CODE_CLI_PATH. (or not found at {cli_path!r})
CLIProbeErrorThe CLI binary exists but does not answer --versionCoCo CLI at {cli_path!r} did not respond to --version
SnowflakeConfigNotFoundErrorNo Snowflake config file foundNo Snowflake config found. Create ~/.snowflake/connections.toml (or legacy config.toml)...
CocoConnectionErrorBad connection name or auth failure—
SessionStartErrorThe background worker failed to start—
SessionNotReadyErrorThe session is not ready (connect timeout / boot failure)—
ApprovalTimeoutError (also TimeoutError)No approval response within approval_timeout_secondsApproval timed out for tool {tool_name}
QueryErrorA headless query() call failed—
CwdUploadErrorAn upload failed (size / extension / path / overwrite)—

wrap_exception() maps raw SDK/runtime exceptions to these by message and a context= kwarg ("session_start", "session_ready"/"session", "query", "approval").

python
try:
    env = st_coco.require_environment(connection="my_connection")
except st_coco.SDKNotInstalledError:
    st.error("Install the SDK: pip install streamlit-coco[sdk]")

Used by: FAQ

Tool helpers#

FunctionEffect
tool_family(name)Maps a tool name to its family enum, for tool-card display
is_sql_tool(name)True for the SQL tool
is_ask_user_question(name)True for AskUserQuestion
is_exit_plan_mode(name)True for ExitPlanMode
is_debug_mode(*, session_state=None)True when STREAMLIT_COCO_DEBUG or COCO_DEBUG is truthy (1/true/yes/on, case-insensitive), or st.session_state["coco_debug"] is truthy
python
if st_coco.is_debug_mode():
    st.caption("Debug mode is on.")

Used by: FAQ

Package metadata#

st_coco.__version__ — the installed package version, for example "0.1.9".

python
import streamlit_coco as st_coco
print(st_coco.__version__)

Used by: Getting started