pine-assistant
Pine AI SDK for Python. Let Pine AI handle your digital chores.
Install
pip install pine-assistant # SDK only
pip install pine-assistant[cli] # SDK + CLI
Instinct and other agents
Follow Set up Pine in Instinct to connect with a user token
and the Pine skill. The skill prefers Socket.IO when available
and uses REST in HTTP-only environments; Python is not required.
Quick Start (Async)
from pine_assistant import AsyncPineAI
client = AsyncPineAI(access_token="...", user_id="...")
async with client:
await client.connect()
session = await client.sessions.create()
await client.join_session(session["id"])
await client.rebuild(session["id"]) # load the session's messages
async for event in client.chat(session["id"], "Negotiate my Comcast bill",
turn_timeout=120):
print(event.type, event.data)
A client tracks one session. Concurrent sessions need one client each.
disconnect() only ends the real-time connection. aclose() (including the
end of async with) also releases the SDK-owned HTTP client. If you pass an
httpx.AsyncClient, it remains your responsibility to close it. Use
api_base_path to select an API prefix; it defaults to /api for compatibility.
REST identity and sessions
async with AsyncPineAI(access_token="...") as client:
identity = await client.auth.me() # AuthIdentity(user_id="...")
sessions = await client.sessions.list(limit=20) # SessionListResponse
session = await client.sessions.get("123") # SessionInfo
The list call sends ensure_copilot=false by default. It is read-only when
used with a backend that supports this query option; older backends may ignore
it and retain their legacy Copilot behavior.
Use await client.sessions.end_task(session_id) to request a user-ended task;
the synchronous client provides the same method without await. The backend
checks ownership and eligibility, and the method returns the updated
SessionInfo. This is not proof that the task objective succeeded or that an
external action has already stopped. The SDK never retries this write: after a
timeout or server error, query the session and its history before deciding
whether to retry, because the state may already have changed.
Use sessions.send_message() when an application needs the REST write
acknowledgement without joining Socket.IO. It returns a typed status: received
means the message was persisted, delivered means it was handed to the Agent,
and delivery_failed means that handoff failed. None means that a task has
finished. A timeout or connection error leaves persistence unknown, so the SDK
never retries a send; recover Socket history before deciding what to do next.
status = await client.sessions.send_message(
"123", "Continue the task", request_id="ui-click-42",
)
if status.status == "delivered":
print(status.message_id, status.revision)
outcomes = await client.sessions.outcomes("123")
for outcome in outcomes.items:
print(outcome.outcome_id, outcome.outcome_narrative)
outcomes() returns newest-first persisted Outcomes. Follow next_cursor to
request older pages with before=...; total is a first-page snapshot and is
not a pagination signal.
For structured form answers, use the async method on a connected client:
await client.connect()
try:
receipt = await client.submit_form_response(
"123", "456", {"contact_name": "Example User"},
)
finally:
await client.disconnect()
The method re-reads the original agent form from authenticated history, preserves
its message and request IDs, validates visible required fields, and JSON-encodes
array answers like the web app. Callers cannot override field privacy levels.
The backend's session:message_status receipt supplies the persisted reply ID:
delivered means it reached the agent and received means it was persisted but
delivery was not observed before the deadline. unknown requires checking
history before deciding whether to submit again. Each Socket.IO connection sends
a form only once because a late receipt cannot identify an attempt; reconnects
never retry or replay form submissions. A transport ACK is not a delivery
receipt. The legacy synchronous send_form_response() is deprecated because it
cannot verify the original form request.
Platform API for partners
A partner is a business that builds on Pine through the Platform API. It
holds a secret key, pine_sk_live_.... Keep it on your servers: read it
from an environment variable or secret store, and never ship it in browser,
mobile or other client-side code, prompts, or logs. To get a key, contact
support@19pine.ai. Each end user of your product becomes a managed user,
identified by your own external_id (1-128 of letters, digits, ., _, :,
|, -, not starting with .; no personal data).
All managed users of one partner use that partner's credits. A partner can
limit the total credits that its managed users use.
The SDK methods for managed users are listed in
docs/sdk-api.md.
If you do not use Python, see the HTTP API in
docs/http-api.md
and the Socket.IO reference in
docs/socketio.md.
Create the managed user once, and store its id in your own user table. The
call is idempotent per external_id: a repeat returns the stored user
unchanged, even if the profile you send differs. phone is optional, in E.164
form, and must be a number you have verified. get() finds a user by
external_id if you did not store the id. update() changes only the
fields you give; phone="" removes the phone.
import os
from pine_assistant import AsyncPineAI
api_key = os.environ["PINE_API_KEY"]
async with AsyncPineAI(api_key=api_key) as partner_client:
user = await partner_client.platform.managed_users.create(
"usr_01J9Z3K8QF",
email="ada@example.com",
name="Ada Lovelace",
phone="+14155550100",
)
same_user = await partner_client.platform.managed_users.get("usr_01J9Z3K8QF")
moved_user = await partner_client.platform.managed_users.update("usr_01J9Z3K8QF", phone="+14155550123")
# Save user.id with your own user record.
email is optional. A managed user without an email cannot do email tasks.
user.id is the Pine user ID, a decimal string such as "123456789". To act
as that user, pass it as user_id together with the key. Every user-scoped
REST request then sends the key and a Pine-Managed-User-Id header. The
Socket.IO handshake sends the key and managed_user_id, and every Socket.IO
message carries the same ID. The SDK checks the form of user_id when you
create the client. It accepts only a string, never an int.
async with AsyncPineAI(api_key=api_key, user_id=user.id) as client:
session = await client.sessions.create()
await client.connect()
await client.join_session(session["id"])
message = "Call my dentist to move Tuesday's appointment"
async for event in client.chat(session["id"], message):
print(event.type, event.data)
A client with api_key and no user_id can call only the Platform API. A
user-scoped call from it, such as sessions.list() or connect(), raises
ValueError before any request is sent. These also raise ValueError:
api_key together with access_token, platform.* on a client without
api_key, and auth.verify_code() or set_token() on a client with api_key.
A key client keeps its key; use a separate client for a user token.
Every error below is a PineAIError with code and status_code, so catch
PineAIError. Some methods raise a subclass: SessionError from
sessions.list, get, get_config, update_config, send_message,
history and outcomes, and
AuthError from auth.*. Other sessions methods, such as create and
start_task, raise PineAIError itself.
- 400
invalid_request: the backend did not accept the managed-user ID.
- 404
platform_managed_user_not_found: the ID is not one of your managed users.
- 403
platform_managed_user_unavailable: the managed user is banned or
archived.
client.auth.mint_ticket() always fails with 403
platform_route_not_allowed, raised as AuthError. Managed users cannot mint
login tickets. Other routes closed to a partner key fail in the same way.
Billing and admission can also reject sessions and chat for a managed user:
402 platform_spend_limit_reached, 403 platform_access_suspended, 409
platform_session_state_conflict, 409 platform_session_limit_reached from
sessions.start_task when the running-task limit is reached.
If the server refuses the Socket.IO handshake, it closes the connection and
sends no error code. connect() then raises ConnectionError when
ready_timeout (15 seconds by default) ends. Its code is always
connection_error, so it does not tell you the cause. The message tells you to
check api_key and user_id.
PineAI accepts the same api_key and user_id for synchronous REST. Platform
API failures raise PlatformError with a stable code and status_code (for
example 401 for a bad key, 403 for a suspended partner, 404
platform_managed_user_not_found, 409 platform_managed_user_limit_reached).
Like every SDK error, it never reproduces the upstream body, the key, the
external_id or the user ID.
Quick Start (Sync REST)
PineAI is a synchronous REST client. It returns values directly for auth and
session resources; use AsyncPineAI for Socket.IO and streaming.
from pine_assistant import PineAI
with PineAI(access_token="...") as client:
print(client.auth.me().user_id)
for session in client.sessions.list(limit=20).sessions:
print(session.id, session.title)
Quick Start (CLI)