I want to run CoCo headless
For a developer scripting CoCo from a CI job or a terminal · 15 min · a terminal, no Streamlit UI needed
You end up with a script that drives the agent without importing Streamlit, either for a single turn or a multi-turn session, resolving any approvals in code.
Before you start#
streamlit-coco[sdk]installed;check_environment().readyisTrueforthe connection you will use (see Getting started).
- Core headless symbols (
CocoSession,query,CocoOptions, thepermission and error types) import without loading Streamlit — UI exports resolve lazily, so a plain
import streamlit_cocois safe in a script that never touches Streamlit.
Steps#
- For a single turn, use
query():import asyncio import streamlit_coco as st_coco async def main() -> None: options = st_coco.CocoOptions(connection="my_connection") async for event in st_coco.query("Summarize sales by region.", options=options): if event.type == "assistant_text": print(event.text) asyncio.run(main()) - For a multi-turn session without any UI, drive
CocoSessiondirectly:session = st_coco.CocoSession(options=options, key="headless") session.start() await session.ensure_ready(timeout=120) result = await session.run("List the tables in my schema.") async for event in session.stream(): ... session.close() - Resolve approvals in code instead of a UI — check
session.chat_result().pending_approvalafter a run and callapprove_pending()/deny_pending()as shown in I want to configure tool approvals, or pre-approve everything you expect viaallowed_toolsso the script never blocks. - Convert accumulated events to a table when you need to inspect or log
them in bulk:
df = st_coco.events_to_dataframe(session.events) # needs pandas
Result#
query() yields CocoEvent objects for a single turn and returns nothing else — there is no session object to keep around afterwards. CocoSession run outside Streamlit behaves exactly as it does inside a panel(): the same transcript, the same CocoRunStatus transitions, the same approval pauses — only the rendering is missing.
Variations#
examples/headless_pipeline.py(make headless) shows bothquery()andCocoSessionfrom a plain terminal script, each in its own event loop.query()raisesSDKNotInstalledErrororQueryError(or anotherCocoErrorsubclass) instead of returning a partial result on failure — wrap the call accordingly for a CI job.