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, 06:00 UTC
| 1 | # @composio/openai |
| 2 | |
| 3 | Adapts Composio tools to OpenAI function calling, for both the Responses API and the Chat Completions API. |
| 4 | |
| 5 | ## Installation |
| 6 | |
| 7 | ```bash |
| 8 | npm install @composio/core @composio/openai openai |
| 9 | ``` |
| 10 | |
| 11 | Set `COMPOSIO_API_KEY` (create one at https://dashboard.composio.dev/settings) and `OPENAI_API_KEY` (from https://platform.openai.com/api-keys) in your environment. |
| 12 | |
| 13 | ## Quickstart |
| 14 | |
| 15 | Create a session for your user, pass its tools to the Responses API, and run the tool-call loop until the model replies with text. `handleToolCalls` executes the tool calls and returns ready-to-send `function_call_output` items. |
| 16 | |
| 17 | ```typescript |
| 18 | import OpenAI from 'openai'; |
| 19 | import { Composio } from '@composio/core'; |
| 20 | import { OpenAIResponsesProvider } from '@composio/openai'; |
| 21 | |
| 22 | const composio = new Composio({ |
| 23 | provider: new OpenAIResponsesProvider(), |
| 24 | }); |
| 25 | const client = new OpenAI(); |
| 26 | |
| 27 | // Create a session for your user |
| 28 | const session = await composio.create('user_123'); |
| 29 | const tools = await session.tools(); |
| 30 | |
| 31 | let response = await client.responses.create({ |
| 32 | model: 'gpt-5.2', |
| 33 | tools, |
| 34 | input: [ |
| 35 | { |
| 36 | role: 'user', |
| 37 | content: |
| 38 | "Send an email to john@example.com with the subject 'Hello' and body 'Hello from Composio!'", |
| 39 | }, |
| 40 | ], |
| 41 | }); |
| 42 | |
| 43 | // Agentic loop: keep executing tool calls until the model responds with text |
| 44 | while (response.output.some(o => o.type === 'function_call')) { |
| 45 | const results = await composio.provider.handleToolCalls(session, response.output); |
| 46 | response = await client.responses.create({ |
| 47 | model: 'gpt-5.2', |
| 48 | tools, |
| 49 | previous_response_id: response.id, |
| 50 | input: results, |
| 51 | }); |
| 52 | } |
| 53 | |
| 54 | // Print final response |
| 55 | for (const item of response.output) { |
| 56 | if (item.type === 'message' && item.content[0].type === 'output_text') { |
| 57 | console.log(item.content[0].text); |
| 58 | } |
| 59 | } |
| 60 | ``` |
| 61 | |
| 62 | ## Providers in this package |
| 63 | |
| 64 | The package exports one provider per API surface: |
| 65 | |
| 66 | - `OpenAIResponsesProvider` targets the Responses API. `handleToolCalls(session, response.output)` returns `function_call_output` items keyed by `call_id`; pair it with `previous_response_id` so you only resend new outputs each turn. Pass a user ID instead of a session for tools fetched with `tools.get()`. Set `{ strict: true }` to normalize tool schemas for OpenAI structured outputs: every object becomes closed and lists every property in `required`, while optional properties stay available but accept `null`. A `null` is dropped before the tool runs unless the source schema accepts it. Tools whose schema strict mode cannot express, such as objects that accept arbitrary keys, `allOf`, `prefixItems`, or unresolved `$ref`s, are sent without strict mode and log a warning. |
| 67 | - `OpenAIProvider` targets the Chat Completions API and is the Composio SDK default, so `new Composio()` with no provider uses it. `handleToolCalls(session, chatCompletion)` returns ready-to-append `tool` messages; you keep the full message list yourself. |
| 68 | |
| 69 | Use the Responses provider for new agentic flows; reach for Chat Completions when extending an existing Chat Completions codebase. See the [docs page](https://docs.composio.dev/docs/providers/openai) for the Chat Completions loop. |
| 70 | |
| 71 | ## Links |
| 72 | |
| 73 | - [OpenAI provider docs](https://docs.composio.dev/docs/providers/openai) |
| 74 | - [Composio documentation](https://docs.composio.dev) |