Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
Mirrored from https://github.com/Camo67/composio.git
Synced Nov 21, 04:45 UTC
| 1 | --- |
| 2 | title: "Session" |
| 3 | description: "A Composio session \u2014 the object returned by `composio.create(...)` / `composio.use(...)`. Use it to fetch session-scoped tools, authorize toolkits,..." |
| 4 | --- |
| 5 | |
| 6 | ## Properties |
| 7 | |
| 8 | | Name | Type | |
| 9 | |------|------| |
| 10 | | `session_id` | `str` | |
| 11 | | `config` | `ToolRouterSessionConfig` | |
| 12 | | `experimental` | `'ToolRouterSessionExperimental'` | |
| 13 | | `config_version` | `int | None` | |
| 14 | | `preload` | `Any` | |
| 15 | |
| 16 | ## Methods |
| 17 | |
| 18 | ### tools() |
| 19 | |
| 20 | Get provider-wrapped tools for execution with your AI framework. Returns tools configured for this session, wrapped in the format expected by your AI provider (OpenAI, Anthropic, LangChain, etc.). When custom tools are bound to the session, execution of COMPOSIO_MULTI_EXECUTE_TOOL is intercepted: local tools are executed in-process, remote tools are sent to the backend. |
| 21 | |
| 22 | ```python |
| 23 | def tools(modifiers: 'Modifiers' | None = ...) -> TToolCollection |
| 24 | ``` |
| 25 | |
| 26 | **Parameters** |
| 27 | |
| 28 | | Name | Type | |
| 29 | |------|------| |
| 30 | | `modifiers?` | `'Modifiers' \| None` | |
| 31 | |
| 32 | **Returns** |
| 33 | |
| 34 | `TToolCollection` |
| 35 | |
| 36 | --- |
| 37 | |
| 38 | ### authorize() |
| 39 | |
| 40 | Authorize a toolkit for the user and get a connection request. Initiates the OAuth flow and returns a ConnectionRequest with redirect URL. |
| 41 | |
| 42 | ```python |
| 43 | def authorize(toolkit: str, *, callback_url: str | None = ..., alias: str | None = ..., experimental: session_link_params.Experimental | None = ...) -> ConnectionRequest |
| 44 | ``` |
| 45 | |
| 46 | **Parameters** |
| 47 | |
| 48 | | Name | Type | Description | |
| 49 | |------|------|-------------| |
| 50 | | `toolkit` | `str` | | |
| 51 | | `callback_url?` | `str \| None` | | |
| 52 | | `alias?` | `str \| None` | Human-readable alias for the connection. Must be unique per userId and toolkit within the project. | |
| 53 | | `experimental?` | `session_link_params.Experimental \| None` | Experimental options for this connection. Pass an `Experimental` dict with `account_type` and/or `acl_config_for_shared` to create a SHARED connection with a per-user ACL. Experimental — shape may change in future releases. | |
| 54 | |
| 55 | **Returns** |
| 56 | |
| 57 | `ConnectionRequest` |
| 58 | |
| 59 | --- |
| 60 | |
| 61 | ### toolkits() |
| 62 | |
| 63 | Get toolkit connection states for the session. |
| 64 | |
| 65 | ```python |
| 66 | def toolkits(*, toolkits: List[str] | None = ..., next_cursor: str | None = ..., limit: int | None = ..., is_connected: bool | None = ..., search: str | None = ...) -> ToolkitConnectionsDetails |
| 67 | ``` |
| 68 | |
| 69 | **Parameters** |
| 70 | |
| 71 | | Name | Type | |
| 72 | |------|------| |
| 73 | | `toolkits?` | `List[str] \| None` | |
| 74 | | `next_cursor?` | `str \| None` | |
| 75 | | `limit?` | `int \| None` | |
| 76 | | `is_connected?` | `bool \| None` | |
| 77 | | `search?` | `str \| None` | |
| 78 | |
| 79 | **Returns** |
| 80 | |
| 81 | `ToolkitConnectionsDetails` |
| 82 | |
| 83 | --- |
| 84 | |
| 85 | ### search() |
| 86 | |
| 87 | Search for tools by semantic use case. Returns relevant tools for the given query with schemas and guidance. |
| 88 | |
| 89 | ```python |
| 90 | def search(*, query: str, model: str | None = ...) -> ToolRouterSessionSearchResponse |
| 91 | ``` |
| 92 | |
| 93 | **Parameters** |
| 94 | |
| 95 | | Name | Type | |
| 96 | |------|------| |
| 97 | | `query` | `str` | |
| 98 | | `model?` | `str \| None` | |
| 99 | |
| 100 | **Returns** |
| 101 | |
| 102 | `ToolRouterSessionSearchResponse` |
| 103 | |
| 104 | --- |
| 105 | |
| 106 | ### execute() |
| 107 | |
| 108 | Execute a tool within the session. Use `tool_slug=...` (not the direct Tools API's `slug=...`) and `arguments={...}`. The return value is a Pydantic model: read `result.result_type == "completed"` for execution success, `result.model_dump(mode="json")` for a JSON-compatible dictionary, or `result.model_dump_json()` for a JSON string. Unlike provider helper results and `after_execute` modifier dictionaries, it has no `successful` key. This call has no model tool-call ID; retain that ID separately when building provider messages. For custom tools, accepts the full slug (e.g. "LOCAL_GREP") or the original slug (e.g. "GREP") when that original slug is unique across the session's custom tools and toolkits. Custom tools are executed in-process; remote tools are sent to the Composio backend. |
| 109 | |
| 110 | ```python |
| 111 | def execute(tool_slug: str, *, arguments: Dict[str, Any] | None = ..., account: str | None = ..., input_responses: Dict[str, ToolRouterInputResponse] | None = ..., request_state: str | None = ...) -> ToolRouterSessionExecuteResponse |
| 112 | ``` |
| 113 | |
| 114 | **Parameters** |
| 115 | |
| 116 | | Name | Type | Description | |
| 117 | |------|------|-------------| |
| 118 | | `tool_slug` | `str` | | |
| 119 | | `arguments?` | `Dict[str, Any] \| None` | | |
| 120 | | `account?` | `str \| None` | Account ID or alias for direct app tool execution in multi-account sessions. Helper/meta tools either ignore this top-level field or define their own account-selection fields. | |
| 121 | | `input_responses?` | `Dict[str, ToolRouterInputResponse] \| None` | Experimental. The user's answers to an earlier `ToolInputRequiredError`, keyed by the IDs in its `input_requests`. Repeat the same call (same tool and arguments) with them. An approved call runs; a denied or declined one returns `result_type="failed"`. | |
| 122 | | `request_state?` | `str \| None` | Experimental. The `request_state` of that error, sent back unchanged. Both paths return a `ToolRouterSessionExecuteResponse` with `data`, `error`, `log_id`, `result_type`, and `instant_charge` attributes. `result_type` is `"completed"`, `"failed"` or `"blocked"`; a failed execution can carry a `None` or empty `error`, so read `result_type` to tell them apart. A `"blocked"` call (a session hook blocked it) has empty `data`, the hook's message in `error`, and `block_reason` (`type` and `hook_id`). `hook_results` lists every session hook that applied. A log ID correlates an execution; it does not prove that a provider performed a side effect. Inspect the tool-specific `data` as well. Transport and authentication errors raise instead of returning an execution result. Handle authentication recovery separately from returned tool failures; retry/idempotency decisions belong to you. | |
| 123 | |
| 124 | **Returns** |
| 125 | |
| 126 | `ToolRouterSessionExecuteResponse` |
| 127 | |
| 128 | **Raises** |
| 129 | |
| 130 | - `ToolInputRequiredError` — If the tool needs input from the user (for example an approval) before it can run. Nothing was executed. |
| 131 | - `ValidationError` — If the API response carries no `result_type`, or one this SDK does not know. |
| 132 | |
| 133 | --- |
| 134 | |
| 135 | ### execute_meta() |
| 136 | |
| 137 | Execute a Composio meta tool (`COMPOSIO_*`) within the session. `execute` also runs meta tools. This method only accepts meta tool slugs, so a typo is a type error instead of a failed request. The call is sent to the Composio backend as is: custom tools named in a `COMPOSIO_MULTI_EXECUTE_TOOL` call are not run in-process here. |
| 138 | |
| 139 | ```python |
| 140 | def execute_meta(slug: ToolRouterSessionMetaToolSlug, *, arguments: Dict[str, Any] | None = ..., input_responses: Dict[str, ToolRouterInputResponse] | None = ..., request_state: str | None = ...) -> ToolRouterSessionExecuteResponse |
| 141 | ``` |
| 142 | |
| 143 | **Parameters** |
| 144 | |
| 145 | | Name | Type | Description | |
| 146 | |------|------|-------------| |
| 147 | | `slug` | `ToolRouterSessionMetaToolSlug` | The meta tool slug to execute. | |
| 148 | | `arguments?` | `Dict[str, Any] \| None` | Optional meta tool arguments. | |
| 149 | | `input_responses?` | `Dict[str, ToolRouterInputResponse] \| None` | Experimental. The user's answers to an earlier `ToolInputRequiredError`, keyed by the IDs in its `input_requests`. Repeat the same call with them, as in `execute`. | |
| 150 | | `request_state?` | `str \| None` | Experimental. The `request_state` of that error, sent back unchanged. | |
| 151 | |
| 152 | **Returns** |
| 153 | |
| 154 | `ToolRouterSessionExecuteResponse` — The tool execution result, in the same shape as `execute`. |
| 155 | |
| 156 | **Raises** |
| 157 | |
| 158 | - `ToolInputRequiredError` — If the tool needs input from the user (for example an approval) before it can run. Nothing was executed. |
| 159 | - `ValidationError` — If the API response carries no `result_type`, or one this SDK does not know. |
| 160 | |
| 161 | **Example** |
| 162 | |
| 163 | ```python |
| 164 | result = session.execute_meta( |
| 165 | "COMPOSIO_SEARCH_TOOLS", |
| 166 | arguments={"queries": [{"use_case": "send an email"}]}, |
| 167 | ) |
| 168 | print(result.result_type, result.data) |
| 169 | ``` |
| 170 | |
| 171 | --- |
| 172 | |
| 173 | ### local_tools() |
| 174 | |
| 175 | List all local tools registered in this session. Returns tools with their final slugs, schemas, and resolved toolkit. |
| 176 | |
| 177 | ```python |
| 178 | def local_tools(*, toolkit: str | None = ...) -> List[RegisteredLocalTool] |
| 179 | ``` |
| 180 | |
| 181 | **Parameters** |
| 182 | |
| 183 | | Name | Type | Description | |
| 184 | |------|------|-------------| |
| 185 | | `toolkit?` | `str \| None` | Filter by toolkit slug (e.g. 'gmail', 'DEV_TOOLS') | |
| 186 | |
| 187 | **Returns** |
| 188 | |
| 189 | `List[RegisteredLocalTool]` — Array of registered local tools |
| 190 | |
| 191 | --- |
| 192 | |
| 193 | ### local_toolkits() |
| 194 | |
| 195 | List all toolkits of local tools registered in this session. Returns toolkits with their tools showing final slugs. |
| 196 | |
| 197 | ```python |
| 198 | def local_toolkits() -> List[RegisteredLocalToolkit] |
| 199 | ``` |
| 200 | |
| 201 | **Returns** |
| 202 | |
| 203 | `List[RegisteredLocalToolkit]` |
| 204 | |
| 205 | --- |
| 206 | |
| 207 | ### custom_tools() |
| 208 | |
| 209 | Deprecated alias for `local_tools`. |
| 210 | |
| 211 | ```python |
| 212 | def custom_tools(*, toolkit: str | None = ...) -> List[RegisteredCustomTool] |
| 213 | ``` |
| 214 | |
| 215 | **Parameters** |
| 216 | |
| 217 | | Name | Type | |
| 218 | |------|------| |
| 219 | | `toolkit?` | `str \| None` | |
| 220 | |
| 221 | **Returns** |
| 222 | |
| 223 | `List[RegisteredCustomTool]` |
| 224 | |
| 225 | --- |
| 226 | |
| 227 | ### custom_toolkits() |
| 228 | |
| 229 | Deprecated alias for `local_toolkits`. |
| 230 | |
| 231 | ```python |
| 232 | def custom_toolkits() -> List[RegisteredCustomToolkit] |
| 233 | ``` |
| 234 | |
| 235 | **Returns** |
| 236 | |
| 237 | `List[RegisteredCustomToolkit]` |
| 238 | |
| 239 | --- |
| 240 | |
| 241 | ### proxy_execute() |
| 242 | |
| 243 | Proxy an API call through Composio's auth layer. |
| 244 | |
| 245 | ```python |
| 246 | def proxy_execute(*, toolkit: str, endpoint: str, method: Literal['GET', 'POST', 'PUT', 'DELETE', 'PATCH'], body: Any = ..., parameters: List[Dict[str, Any]] | None = ...) -> ToolRouterSessionProxyExecuteResponse |
| 247 | ``` |
| 248 | |
| 249 | **Parameters** |
| 250 | |
| 251 | | Name | Type | Description | |
| 252 | |------|------|-------------| |
| 253 | | `toolkit` | `str` | Composio toolkit slug (e.g. 'gmail', 'github') | |
| 254 | | `endpoint` | `str` | API endpoint URL | |
| 255 | | `method` | `Literal['GET', 'POST', 'PUT', 'DELETE', 'PATCH']` | HTTP method | |
| 256 | | `body?` | `Any` | Request body (for POST, PUT, PATCH) | |
| 257 | | `parameters?` | `List[Dict[str, Any]] \| None` | Query/header parameters | |
| 258 | |
| 259 | **Returns** |
| 260 | |
| 261 | `ToolRouterSessionProxyExecuteResponse` — The proxied API response with `result_type="completed"`, or `result_type="blocked"` with the hook's `message` when a session hook blocked the request (it then has no `status`) |
| 262 | |
| 263 | **Raises** |
| 264 | |
| 265 | - `ToolInputRequiredError` — If the call needs input from the user before it can run. Nothing was executed. |
| 266 | |
| 267 | --- |
| 268 | |
| 269 | ### update() |
| 270 | |
| 271 | Partially update the session configuration. Only the fields provided are changed; omitted fields are preserved. For each policy block `None` removes the stored override (which can increase access: `toolkits=None` restores the unrestricted default, while `toolkits={"enable": []}` denies every app toolkit and is sent as-is). Supplied `tools`, `auth_configs` and `connected_accounts` maps replace the stored map entirely. Inside `manage_connections`, `callback_url=None` removes only the stored callback URL. Experimental `instant` accepts `False` to disable billed access or an object to set its filters; it does not accept `None`. Any object, even one that only sets `return_instant_charge` or `return_prices_in_search`, re-enables Instant usage on a Session set to `False`. `proxy_execute` takes a bool or `{"enable": bool}`; `True` exposes the `COMPOSIO_PROXY_EXECUTE` meta tool, `False` also turns off the sandbox `proxy_execute()` helper, and `None` removes the stored block. `hooks` replaces the whole list of hooks, and `None` removes every hook. `hooks` cannot be combined with `experimental={'session_config_id': ...}` (`hooks=None` counts). By default the request carries no precondition: the last writer wins. Pass `expected_config_version` (for example this object's `config_version`) to make the update conditional: the API then applies it only when the stored version still matches, and a concurrent change raises `SessionConfigConflictError` (HTTP 409) instead of being overwritten. The API must support the `expected_config_version` field; otherwise it rejects the request with a 400. `expected_config_version=False` is the same as omitting it. The PATCH is never retried by the transport, so a 409 is reported exactly once. On conflict this object stays unchanged: re-fetch the session with `composio.sessions.use(session_id)` and retry against the fresh `config_version`. `config`, `config_version` and `preload` are refreshed in place only after a successful response, and the updated `config` is returned. `workbench` is a backwards-compatible alias for `sandbox`. Prefer `sandbox` in new code. All other parameters use the same types as the generated `client.tool_router.session.patch()` method. |
| 272 | |
| 273 | ```python |
| 274 | def update(*, toolkits: Union[session_patch_params.Toolkits | None, 'Omit'] = ..., instant: Union[Literal[False], ToolRouterInstantConfig, 'Omit'] = ..., tools: Union[Dict[str, session_patch_params.Tools] | None, 'Omit'] = ..., tags: Union[session_patch_params.Tags | None, 'Omit'] = ..., auth_configs: Union[Dict[str, str] | None, 'Omit'] = ..., connected_accounts: Union[Dict[str, SequenceNotStr[str]] | None, 'Omit'] = ..., manage_connections: Union[session_patch_params.ManageConnections | None, ToolRouterUpdateManageConnectionsConfig | None, 'Omit'] = ..., sandbox: Union[session_patch_params.Workbench | None, 'Omit'] = ..., workbench: Union[session_patch_params.Workbench | None, 'Omit'] = ..., multi_account: Union[session_patch_params.MultiAccount | None, ToolRouterUpdateMultiAccountConfig | None, 'Omit'] = ..., proxy_execute: Union[Union[bool, ToolRouterProxyExecuteConfig] | None, 'Omit'] = ..., hooks: Union[List[ToolRouterHook] | None, 'Omit'] = ..., preload: Union[session_patch_params.Preload | None, 'Omit'] = ..., search: Union[session_patch_params.Search | None, 'Omit'] = ..., execute: Union[session_patch_params.Execute | None, 'Omit'] = ..., experimental: Union[session_patch_params.Experimental | None, ToolRouterUpdateExperimentalConfig | None, 'Omit'] = ..., expected_config_version: Union[int, None, Literal[False]] = ...) -> ToolRouterSessionConfig |
| 275 | ``` |
| 276 | |
| 277 | **Parameters** |
| 278 | |
| 279 | | Name | Type | |
| 280 | |------|------| |
| 281 | | `toolkits?` | `Union[session_patch_params.Toolkits \| None, 'Omit']` | |
| 282 | | `instant?` | `Union[Literal[False], ToolRouterInstantConfig, 'Omit']` | |
| 283 | | `tools?` | `Union[Dict[str, session_patch_params.Tools] \| None, 'Omit']` | |
| 284 | | `tags?` | `Union[session_patch_params.Tags \| None, 'Omit']` | |
| 285 | | `auth_configs?` | `Union[Dict[str, str] \| None, 'Omit']` | |
| 286 | | `connected_accounts?` | `Union[Dict[str, SequenceNotStr[str]] \| None, 'Omit']` | |
| 287 | | `manage_connections?` | `Union[session_patch_params.ManageConnections \| None, ToolRouterUpdateManageConnectionsConfig \| None, 'Omit']` | |
| 288 | | `sandbox?` | `Union[session_patch_params.Workbench \| None, 'Omit']` | |
| 289 | | `workbench?` | `Union[session_patch_params.Workbench \| None, 'Omit']` | |
| 290 | | `multi_account?` | `Union[session_patch_params.MultiAccount \| None, ToolRouterUpdateMultiAccountConfig \| None, 'Omit']` | |
| 291 | | `proxy_execute?` | `Union[Union[bool, ToolRouterProxyExecuteConfig] \| None, 'Omit']` | |
| 292 | | `hooks?` | `Union[List[ToolRouterHook] \| None, 'Omit']` | |
| 293 | | `preload?` | `Union[session_patch_params.Preload \| None, 'Omit']` | |
| 294 | | `search?` | `Union[session_patch_params.Search \| None, 'Omit']` | |
| 295 | | `execute?` | `Union[session_patch_params.Execute \| None, 'Omit']` | |
| 296 | | `experimental?` | `Union[session_patch_params.Experimental \| None, ToolRouterUpdateExperimentalConfig \| None, 'Omit']` | |
| 297 | | `expected_config_version?` | `Union[int, None, Literal[False]]` | |
| 298 | |
| 299 | **Returns** |
| 300 | |
| 301 | `ToolRouterSessionConfig` |
| 302 | |
| 303 | --- |
| 304 | |
| 305 | ### list_config_history() |
| 306 | |
| 307 | List the configuration history of this session, newest first. Every `update()` records a new config version; this returns those versions with cursor-based pagination. |
| 308 | |
| 309 | ```python |
| 310 | def list_config_history(**query: Unpack[session_config_history_params.SessionConfigHistoryParams]) -> session_config_history_response.SessionConfigHistoryResponse |
| 311 | ``` |
| 312 | |
| 313 | **Parameters** |
| 314 | |
| 315 | | Name | Type | |
| 316 | |------|------| |
| 317 | | `query?` | `Unpack[session_config_history_params.SessionConfigHistoryParams]` | |
| 318 | |
| 319 | **Returns** |
| 320 | |
| 321 | `session_config_history_response.SessionConfigHistoryResponse` — The config versions under `.items` plus pagination fields. |
| 322 | |
| 323 | **Example** |
| 324 | |
| 325 | ```python |
| 326 | history = session.list_config_history(limit=10) |
| 327 | for entry in history.items: |
| 328 | print(entry.version, entry.is_current) |
| 329 | ``` |
| 330 | |
| 331 | --- |
| 332 | |
| 333 | ### delete() |
| 334 | |
| 335 | Delete this session. Deleted sessions immediately stop being retrievable or executable. An already-deleted session surfaces the backend 404. |
| 336 | |
| 337 | ```python |
| 338 | def delete() -> ToolRouterSessionDeleteResponse |
| 339 | ``` |
| 340 | |
| 341 | **Returns** |
| 342 | |
| 343 | `ToolRouterSessionDeleteResponse` |
| 344 | |
| 345 | --- |
| 346 | |
| 347 | [View source](https://github.com/composiohq/composio/blob/next/python/composio/core/models/tool_router_session.py#L316) |