Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.

next
md 70 lines 2.48 KB
Rendered Raw
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 pip install composio composio-openai openai
9 ```
10
11 Set `COMPOSIO_API_KEY` (create one at https://dashboard.composio.dev/settings) and `OPENAI_API_KEY` in your environment.
12
13 ## Quickstart
14
15 This package exports two providers: `OpenAIResponsesProvider` for the [Responses API](https://platform.openai.com/docs/api-reference/responses) and `OpenAIProvider` for Chat Completions. Both are non-agentic: the model returns tool calls, you execute them with `handle_tool_calls`, and you feed the results back.
16
17 ```python
18 import json
19 from openai import OpenAI
20 from composio import Composio
21 from composio_openai import OpenAIResponsesProvider
22
23 composio = Composio(provider=OpenAIResponsesProvider())
24 client = OpenAI()
25
26 # Create a session for your user
27 session = composio.create(user_id="user_123")
28 tools = session.tools()
29
30 response = client.responses.create(
31 model="gpt-5.2",
32 tools=tools,
33 input=[
34 {
35 "role": "user",
36 "content": "Send an email to john@example.com with the subject 'Hello' and body 'Hello from Composio!'"
37 }
38 ]
39 )
40
41 # Agentic loop: keep executing tool calls until the model responds with text
42 while True:
43 tool_calls = [o for o in response.output if o.type == "function_call"]
44 if not tool_calls:
45 break
46 results = composio.provider.handle_tool_calls(response=response, session=session)
47 response = client.responses.create(
48 model="gpt-5.2",
49 tools=tools,
50 previous_response_id=response.id,
51 input=[
52 {"type": "function_call_output", "call_id": tool_calls[i].call_id, "output": json.dumps(result)}
53 for i, result in enumerate(results)
54 ]
55 )
56
57 # Print final response
58 for item in response.output:
59 if item.type == "message":
60 print(item.content[0].text)
61 ```
62
63 ## Chat Completions
64
65 `OpenAIProvider` targets `client.chat.completions.create` and is the Composio SDK default, so `Composio()` with no provider uses it. The loop is the same shape: call `handle_tool_calls(response=response, session=session)`, append the results as `tool` messages, and call the API again. Pass `user_id` instead for tools fetched with `tools.get()`. See the [docs page](https://docs.composio.dev/docs/providers/openai) for the full example.
66
67 ## Links
68
69 - OpenAI provider docs: https://docs.composio.dev/docs/providers/openai
70 - Composio docs: https://docs.composio.dev