Metadata-Version: 2.4
Name: composio-openai
Version: 0.18.1
Summary: Use Composio to get an array of tools with your OpenAI Function Call.
Home-page: https://github.com/ComposioHQ/composio
Author: Composio
Author-email: Composio <tech@composio.dev>
Project-URL: Homepage, https://github.com/ComposioHQ/composio
Classifier: Programming Language :: Python :: 3
Classifier: License :: OSI Approved :: Apache Software License
Classifier: Operating System :: OS Independent
Requires-Python: >=3.10,<4
Description-Content-Type: text/markdown
Requires-Dist: openai>=2.48.0
Requires-Dist: composio
Dynamic: author
Dynamic: home-page
Dynamic: requires-python

# composio-openai

Adapts Composio tools to OpenAI function calling, for both the Responses API and the Chat Completions API.

## Installation

```bash
pip install composio composio-openai openai
```

Set `COMPOSIO_API_KEY` (create one at https://dashboard.composio.dev/settings) and `OPENAI_API_KEY` in your environment.

## Quickstart

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.

```python
import json
from openai import OpenAI
from composio import Composio
from composio_openai import OpenAIResponsesProvider

composio = Composio(provider=OpenAIResponsesProvider())
client = OpenAI()

# Create a session for your user
session = composio.create(user_id="user_123")
tools = session.tools()

response = client.responses.create(
    model="gpt-5.2",
    tools=tools,
    input=[
        {
            "role": "user",
            "content": "Send an email to john@example.com with the subject 'Hello' and body 'Hello from Composio!'"
        }
    ]
)

# Agentic loop: keep executing tool calls until the model responds with text
while True:
    tool_calls = [o for o in response.output if o.type == "function_call"]
    if not tool_calls:
        break
    results = composio.provider.handle_tool_calls(response=response, user_id="user_123")
    response = client.responses.create(
        model="gpt-5.2",
        tools=tools,
        previous_response_id=response.id,
        input=[
            {"type": "function_call_output", "call_id": tool_calls[i].call_id, "output": json.dumps(result)}
            for i, result in enumerate(results)
        ]
    )

# Print final response
for item in response.output:
    if item.type == "message":
        print(item.content[0].text)
```

## Chat Completions

`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` on each response, append the results as `tool` messages, and call the API again. See the [docs page](https://docs.composio.dev/docs/providers/openai) for the full example.

## Links

- OpenAI provider docs: https://docs.composio.dev/docs/providers/openai
- Composio docs: https://docs.composio.dev
