Skip to main content

Python

The codespar package on PyPI: which Session methods exist under their snake_case names, and what the TypeScript SDK has that Python does not.

4 min read
View MarkdownEdit on GitHub

Python

codespar 0.11.0 on PyPI; 0.11.0 on the main branch of codespar-core.

pip install codespar

Two clients over the same backend: CodeSpar is synchronous (scripts, notebooks, Flask and Django views) and AsyncCodeSpar is asyncio (FastAPI, LangChain, anything already on an event loop). Both return a session with the same method names; the async one returns coroutines.

from codespar import CodeSpar

cs = CodeSpar(api_key="csk_test_0000")
session = cs.create("user_0000", preset="brazilian")
try:
    result = session.send("Charge R$150 via Pix and issue the NF-e")
    print(result.message)
finally:
    session.close()
    cs.close()
from codespar import AsyncCodeSpar

async with AsyncCodeSpar(api_key="csk_test_0000") as cs:
    session = await cs.create("user_0000", preset="brazilian")
    async for event in session.send_stream("Charge R$150 via Pix"):
        if event.type == "assistant_text":
            print(event.content, end="")
    await session.close()

Three things differ from the TypeScript package on purpose, and the tables below say which names exist on each side:

  • Names are snake_case. paymentStatusStream is payment_status_stream; findTools is find_tools. A TypeScript page that mentions a Python name labels it as such.
  • Timeouts are in seconds, following httpx. timeout=30 is thirty seconds here and thirty milliseconds in TypeScript; do not copy a number between the two.
  • Errors are a hierarchy under CodeSparError. ApiError carries status, code and body like CodesparApiError does; ConfigError, NotConnectedError and StreamError have no TypeScript counterpart.

Streams accept on_update= (sync or async callable) and timeout=; the promise-style signal of the TypeScript SDK has no equivalent, cancel the task instead. The Python stream relies on the httpx read timeout, which any incoming byte resets; the TypeScript one resets only on a complete SSE frame. Both are documented liveness models and are deliberately not unified.

The codespar.mandate module (verify_mandate_token, decode_mandate_token) matches the @codespar/sdk/mandate subpath: offline V3 mandate verification with no API call.

Session methods

One row per method of the TypeScript Session. The Python column is the signature as written in _sync_client.py; AsyncSession has the same names as coroutines.

TypeScriptPython (Session)AsyncSession
toolstools(self) -> list[Tool]:async tools
findToolsfind_tools(self, intent: str) -> list[Tool]:async find_tools
execute`execute(self, tool_name: str, params: dict[str, Any], *, timeout: floatNone = None) -> ToolResult:`
proxyExecute`proxy_execute(self, request: ProxyRequest, *, timeout: floatNone = None) -> ProxyResult:`
send`send(self, message: str, *, timeout: floatNone = None) -> SendResult:`
sendStream`send_stream(self, message: str, *, timeout: floatNone = None) -> Iterator[StreamEvent]:`
discover`discover(self, use_case: str, options: DiscoverOptionsNone = None, *, timeout: float
connectionWizard`connection_wizard(self, options: ConnectionWizardOptions, *, timeout: floatNone = None) -> ConnectionWizardResult:`
charge`charge(self, args: ChargeArgs, *, timeout: floatNone = None) -> ChargeResult:`
ship`ship(self, args: ShipArgs, *, timeout: floatNone = None) -> ShipResult:`
ledger`ledger(self, args: LedgerArgs, *, timeout: floatNone = None) -> LedgerResult:`
issue`issue(self, args: IssueArgs, *, timeout: floatNone = None) -> IssueResult:`
shop`shop(self, args: ShopArgs, *, timeout: floatNone = None) -> ShopResult:`
paymentStatus`payment_status(self, tool_call_id: str, *, timeout: floatNone = None) -> PaymentStatusResult:`
verificationStatus`verification_status(self, tool_call_id: str, *, timeout: floatNone = None) -> VerificationStatusResult:`
paymentStatusStream`payment_status_stream(self, tool_call_id: str, *, on_update: AnyNone = None, timeout: float
verificationStatusStream`verification_status_stream(self, tool_call_id: str, *, on_update: AnyNone = None, timeout: float
authorize`authorize(self, server_id: str, config: AuthConfig, *, timeout: floatNone = None) -> AuthResult:`
connections`connections(self, *, timeout: floatNone = None) -> list[ServerConnection]:`
close`close(self, *, timeout: floatNone = None) -> None:`

Client, functions and errors

TypeScriptPython
new CodeSpar(config)CodeSpar(api_key=..., base_url=..., project_id=..., timeout=...) and AsyncCodeSpar(...)
create`create(self, user_id: str, config: SessionConfig
cs.apinot in Python: there is no generated REST client; call api.codespar.dev with httpx directly
SessionConfigSchemanot in Python
loopnot in Python
toolssession.tools() (a method, not a free function)
findToolssession.find_tools() (a method, not a free function)
CodesparApiErrorApiError
TimeoutErrorTimeoutError
TOOL_RESULT_CODESTOOL_RESULT_CODES
ToolResultCodeToolResultCode
assertExhaustiveToolResultassert_exhaustive_tool_result
isApprovalRequiredis_approval_required
isMocksEngineErroris_mocks_engine_error
isMocksExhaustedis_mocks_exhausted
isPolicyDeniedis_policy_denied
isToolNotMockedis_tool_not_mocked
ApiClientnot in Python
createApiClientnot in Python
API_OPERATIONSnot in Python

Only in Python

  • CodeSparError (Exception)
  • ConfigError (CodeSparError)
  • NotConnectedError (CodeSparError)
  • StreamError (CodeSparError)
  • session.info (property)
  • await AsyncCodeSpar.aclose() (and CodeSpar.close()): the client owns the HTTP transport and must be closed
Python | CodeSpar