6
7
The OpenAI Provider is the default provider for the Composio SDK. It transforms Composio tools into a format compatible with OpenAI's function calling capabilities through both the Responses and Chat Completion APIs.
8
9
-## Setup
9
+<IntegrationTabs tabs={[
10
+ { value: "responses", label: "Responses API" },
11
+ { value: "chat", label: "Chat Completions" },
12
+]}>
13
11
-By default, the OpenAI Provider is installed when you install the Composio SDK. You can also install it manually:
14
+<IntegrationContent value="responses">
15
+<Steps>
16
+<Step>
17
+**Install**
18
13
-<Tabs items={["Python", "TypeScript"]}>
19
+<Tabs groupId="language" items={["Python", "TypeScript"]} persist>
20
<Tab value="Python">
21
```bash
16
-pip install composio_openai
22
+pip install composio composio_openai openai
23
```
24
</Tab>
25
<Tab value="TypeScript">
26
```bash
21
-npm install @composio/openai
27
+npm install @composio/core @composio/openai openai
28
```
29
</Tab>
30
</Tabs>
31
+</Step>
32
26
-## Responses API
33
+<Step>
34
+**Configure API Keys**
35
28
-The Responses API is the recommended way to build more agentic flows with the OpenAI API.
29
-Read more about it in the [OpenAI documentation](https://platform.openai.com/docs/api-reference/responses)
30
-
31
-<Callout>
32
-Before executing any tools that require authentication (like Gmail), you'll need to:
33
-1. [Create an Auth Configuration](/docs/tools-direct/authenticating-tools#creating-an-auth-config) for your app
34
-2. [Set up a Connected Account](/docs/tools-direct/authenticating-tools#connecting-an-account) for the user.
36
+<Callout type="info">
37
+Set `COMPOSIO_API_KEY` with your API key from [Settings](https://platform.composio.dev/?next_page=/settings) and `OPENAI_API_KEY` with your [OpenAI API key](https://platform.openai.com/api-keys).
38
</Callout>
39
37
-<Tabs items={["Python", "TypeScript"]}>
40
+```txt title=".env"
41
+COMPOSIO_API_KEY=xxxxxxxxx
42
+OPENAI_API_KEY=xxxxxxxxx
43
+```
44
+</Step>
45
+
46
+<Step>
47
+**Create session and run**
48
+
49
+The [Responses API](https://platform.openai.com/docs/api-reference/responses) is the recommended way to build agentic flows with OpenAI.
50
+
51
+<Tabs groupId="language" items={["Python", "TypeScript"]} persist>
52
<Tab value="Python">
53
```python
54
+import json
55
from openai import OpenAI
56
from composio import Composio
57
from composio_openai import OpenAIResponsesProvider
58
44
-# Initialize Composio client with OpenAI Provider
59
composio = Composio(provider=OpenAIResponsesProvider())
46
-openai = OpenAI()
60
+client = OpenAI()
61
48
-# Make sure to create an auth config and a connected account for the user with gmail toolkit
49
-# Make sure to replace "your-user-id" with the actual user ID
50
-user_id = "your-user-id"
62
+# Create a session for your user
63
+session = composio.create(user_id="user_123")
64
+tools = session.tools()
65
52
-tools = composio.tools.get(user_id=user_id, tools=["GMAIL_SEND_EMAIL"])
53
-
54
-response = openai.responses.create(
55
- model="gpt-5",
66
+response = client.responses.create(
67
+ model="gpt-5.2",
68
tools=tools,
69
input=[
70
{
71
"role": "user",
60
- "content": "Send an email to soham.g@composio.dev with the subject 'Running OpenAI Provider snippet' and body 'Hello from the code snippet in openai docs'"
72
+ "content": "Send an email to john@example.com with the subject 'Hello' and body 'Hello from Composio!'"
73
}
74
]
75
)
76
65
-# Execute the function calls
66
-result = composio.provider.handle_tool_calls(response=response, user_id=user_id)
67
-print(result)
77
+# Agentic loop — keep executing tool calls until the model responds with text
78
+while True:
79
+ tool_calls = [o for o in response.output if o.type == "function_call"]
80
+ if not tool_calls:
81
+ break
82
+ results = composio.provider.handle_tool_calls(response=response, user_id="user_123")
83
+ response = client.responses.create(
84
+ model="gpt-5.2",
85
+ tools=tools,
86
+ previous_response_id=response.id,
87
+ input=[
88
+ {"type": "function_call_output", "call_id": tool_calls[i].call_id, "output": json.dumps(result)}
89
+ for i, result in enumerate(results)
90
+ ]
91
+ )
92
+
93
+# Print final response
94
+for item in response.output:
95
+ if item.type == "message":
96
+ print(item.content[0].text)
97
```
98
</Tab>
99
<Tab value="TypeScript">
102
import { Composio } from '@composio/core';
103
import { OpenAIResponsesProvider } from '@composio/openai';
104
76
-// Initialize Composio client with OpenAI Provider
105
const composio = new Composio({
106
provider: new OpenAIResponsesProvider(),
107
});
80
-const openai = new OpenAI({});
81
-
82
-// Make sure to create an auth config and a connected account for the user with gmail toolkit
83
-// Make sure to replace "your-user-id" with the actual user ID
84
-const userId = "your-user-id";
85
-
86
-async function main() {
87
- try {
88
- const tools = await composio.tools.get(userId, {tools: ["GMAIL_SEND_EMAIL"]});
89
-
90
- const response = await openai.responses.create({
91
- model: "gpt-5",
92
- tools: tools,
93
- input: [
94
- {
95
- role: "user",
96
- content: "Send an email to soham.g@composio.dev with the subject 'Running OpenAI Provider snippet' and body 'Hello from the code snippet in openai docs'"
97
- },
98
- ],
99
- });
108
+const client = new OpenAI();
109
101
- // Execute the function calls
102
- const result = await composio.provider.handleToolCalls(userId, response.output);
103
- console.log(result);
104
- } catch (error) {
105
- console.error('Error:', error);
106
- }
110
+// Create a session for your user
111
+const session = await composio.create("user_123");
112
+const tools = await session.tools();
113
+
114
+let response = await client.responses.create({
115
+ model: "gpt-5.2",
116
+ tools: tools,
117
+ input: [
118
+ {
119
+ role: "user",
120
+ content: "Send an email to john@example.com with the subject 'Hello' and body 'Hello from Composio!'"
121
+ },
122
+ ],
123
+});
124
+
125
+// Agentic loop — keep executing tool calls until the model responds with text
126
+while (true) {
127
+ const toolCalls = response.output.filter((o) => o.type === "function_call");
128
+ if (toolCalls.length === 0) break;
129
+
130
+ const results = await composio.provider.handleToolCalls("user_123", response.output);
131
+ response = await client.responses.create({
132
+ model: "gpt-5.2",
133
+ tools: tools,
134
+ previous_response_id: response.id,
135
+ input: results.map((result, i) => ({
136
+ type: "function_call_output" as const,
137
+ call_id: toolCalls[i].call_id,
138
+ output: JSON.stringify(result),
139
+ })),
140
+ });
141
}
142
109
-main();
143
+// Print final response
144
+for (const item of response.output) {
145
+ if (item.type === "message") {
146
+ const block = item.content[0];
147
+ if (block.type === "output_text") {
148
+ console.log(block.text);
149
+ }
150
+ }
151
+}
152
```
153
</Tab>
154
</Tabs>
155
+</Step>
156
+</Steps>
157
+</IntegrationContent>
158
+
159
+<IntegrationContent value="chat">
160
+<Steps>
161
+<Step>
162
+**Install**
163
114
-## Chat Completion API
164
+<Tabs groupId="language" items={["Python", "TypeScript"]} persist>
165
+<Tab value="Python">
166
+```bash
167
+pip install composio composio_openai openai
168
+```
169
+</Tab>
170
+<Tab value="TypeScript">
171
+```bash
172
+npm install @composio/core @composio/openai openai
173
+```
174
+</Tab>
175
+</Tabs>
176
+</Step>
177
116
-The Chat Completion API generates a model response from a list of messages. Read more about it in the [OpenAI documentation](https://platform.openai.com/docs/api-reference/chat).
117
-The OpenAI Chat Provider is the default provider used by Composio SDK, but you can also explicitly initialise it.
178
+<Step>
179
+**Configure API Keys**
180
119
-<Callout>
120
-Before executing any tools that require authentication (like Gmail), you'll need to:
121
-1. [Create an Auth Configuration](/docs/tools-direct/authenticating-tools#creating-an-auth-config) for your app
122
-2. [Set up a Connected Account](/docs/tools-direct/authenticating-tools#connecting-an-account) for the user.
181
+<Callout type="info">
182
+Set `COMPOSIO_API_KEY` with your API key from [Settings](https://platform.composio.dev/?next_page=/settings) and `OPENAI_API_KEY` with your [OpenAI API key](https://platform.openai.com/api-keys).
183
</Callout>
184
125
-<Tabs items={["Python", "TypeScript"]}>
185
+```txt title=".env"
186
+COMPOSIO_API_KEY=xxxxxxxxx
187
+OPENAI_API_KEY=xxxxxxxxx
188
+```
189
+</Step>
190
+
191
+<Step>
192
+**Create session and run**
193
+
194
+The [Chat Completions API](https://platform.openai.com/docs/api-reference/chat) generates a model response from a list of messages.
195
+The `OpenAIProvider` (Chat Completions) is the default provider used by Composio SDK.
196
+
197
+<Tabs groupId="language" items={["Python", "TypeScript"]} persist>
198
<Tab value="Python">
199
```python
200
+import json
201
from openai import OpenAI
202
from composio import Composio
203
from composio_openai import OpenAIProvider
204
132
-# Initialize Composio client with OpenAI Provider
205
composio = Composio(provider=OpenAIProvider())
134
-openai = OpenAI()
206
+client = OpenAI()
207
136
-# Make sure to create an auth config and a connected account for the user with gmail toolkit
137
-# Make sure to replace "your-user-id" with the actual user ID
138
-user_id = "your-user-id"
208
+# Create a session for your user
209
+session = composio.create(user_id="user_123")
210
+tools = session.tools()
211
140
-tools = composio.tools.get(user_id=user_id, tools=["GMAIL_SEND_EMAIL"])
212
+messages = [
213
+ {"role": "user", "content": "Send an email to john@example.com with the subject 'Hello' and body 'Hello from Composio!'"}
214
+]
215
142
-response = openai.chat.completions.create(
143
- model="gpt-5",
216
+response = client.chat.completions.create(
217
+ model="gpt-5.2",
218
tools=tools,
145
- messages=[
146
- {"role": "user", "content": "Send an email to soham.g@composio.dev with the subject 'Running OpenAI Provider snippet' and body 'Hello from the code snippet in openai docs'"},
147
- ],
219
+ messages=messages,
220
)
221
150
-# Execute the function calls
151
-result = composio.provider.handle_tool_calls(response=response, user_id=user_id)
152
-print(result)
222
+# Agentic loop — keep executing tool calls until the model responds with text
223
+while response.choices[0].message.tool_calls:
224
+ results = composio.provider.handle_tool_calls(response=response, user_id="user_123")
225
+ messages.append(response.choices[0].message)
226
+ for i, tc in enumerate(response.choices[0].message.tool_calls):
227
+ messages.append({
228
+ "role": "tool",
229
+ "tool_call_id": tc.id,
230
+ "content": json.dumps(results[i]),
231
+ })
232
+ response = client.chat.completions.create(
233
+ model="gpt-5.2",
234
+ tools=tools,
235
+ messages=messages,
236
+ )
237
+
238
+print(response.choices[0].message.content)
239
```
240
</Tab>
241
<Tab value="TypeScript">
244
import { Composio } from '@composio/core';
245
import { OpenAIProvider } from '@composio/openai';
246
161
-// Initialize Composio client with OpenAI Provider
247
const composio = new Composio({
248
provider: new OpenAIProvider(),
249
});
250
+const client = new OpenAI();
251
+
252
+// Create a session for your user
253
+const session = await composio.create("user_123");
254
+const tools = await session.tools();
255
+
256
+const messages: OpenAI.Chat.ChatCompletionMessageParam[] = [
257
+ {
258
+ role: "user",
259
+ content: "Send an email to john@example.com with the subject 'Hello' and body 'Hello from Composio!'"
260
+ },
261
+];
262
+
263
+let response = await client.chat.completions.create({
264
+ model: "gpt-5.2",
265
+ tools: tools,
266
+ messages: messages,
267
+});
268
166
-const openai = new OpenAI();
167
-
168
-// Make sure to create an auth config and a connected account for the user with gmail toolkit
169
-// Make sure to replace "your-user-id" with the actual user ID
170
-const userId = "your-user-id";
171
-
172
-async function main() {
173
- try {
174
- const tools = await composio.tools.get(userId, {tools: ["GMAIL_SEND_EMAIL"]});
175
-
176
- const response = await openai.chat.completions.create({
177
- model: "gpt-5",
178
- tools: tools,
179
- messages: [
180
- {
181
- role: "user",
182
- content: "Send an email to soham.g@composio.dev with the subject 'Running OpenAI Provider snippet' and body 'Hello from the code snippet in openai docs'"
183
- },
184
- ],
269
+// Agentic loop — keep executing tool calls until the model responds with text
270
+while (response.choices[0].message.tool_calls) {
271
+ const results = await composio.provider.handleToolCalls("user_123", response);
272
+ messages.push(response.choices[0].message);
273
+ for (const [i, tc] of response.choices[0].message.tool_calls.entries()) {
274
+ messages.push({
275
+ role: "tool",
276
+ tool_call_id: tc.id,
277
+ content: JSON.stringify(results[i]),
278
});
186
-
187
- // Execute the function calls
188
- const result = await composio.provider.handleToolCalls(userId, response);
189
- console.log(result);
190
- } catch (error) {
191
- console.error('Error:', error);
279
}
280
+ response = await client.chat.completions.create({
281
+ model: "gpt-5.2",
282
+ tools: tools,
283
+ messages: messages,
284
+ });
285
}
286
195
-main();
287
+console.log(response.choices[0].message.content);
288
```
289
</Tab>
290
</Tabs>
291
+</Step>
292
+</Steps>
293
+</IntegrationContent>
294
+
295
+</IntegrationTabs>