Composio tool authentication
Quick answer
Authenticate Composio tools by creating a
ComposioToolSet instance with your Composio API key, then use it with an OpenAI client initialized with your OpenAI API key from os.environ. Pass the tools to your chat completion calls via the tools parameter for seamless integration.PREREQUISITES
Python 3.8+OpenAI API key (free tier works)Composio API keypip install openai>=1.0 composio-core composio-openai
Setup
Install the required packages and set environment variables for your API keys.
- Install packages:
openai,composio-core, andcomposio-openai. - Set environment variables
OPENAI_API_KEYandCOMPOSIO_API_KEYwith your respective API keys.
pip install openai composio-core composio-openai output
Collecting openai Collecting composio-core Collecting composio-openai Successfully installed openai composio-core composio-openai
Step by step
Use the ComposioToolSet with your Composio API key and integrate it with the OpenAI client for tool-enabled chat completions.
import os
from openai import OpenAI
from composio_openai import ComposioToolSet, Action
# Initialize Composio toolset with your Composio API key
toolset = ComposioToolSet(api_key=os.environ["COMPOSIO_API_KEY"])
# Get tools for specific actions
tools = toolset.get_tools(actions=[Action.GITHUB_STAR_A_REPOSITORY])
# Initialize OpenAI client with your OpenAI API key
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
# Create a chat completion request with tools
response = client.chat.completions.create(
model="gpt-4o-mini",
tools=tools,
messages=[{"role": "user", "content": "Star the openai/openai-python repo"}]
)
print("Response:", response.choices[0].message.content)
# Handle any tool calls if present
result = toolset.handle_tool_calls(response)
print("Tool call result:", result) output
Response: I have starred the openai/openai-python repository for you.
Tool call result: {'success': True, 'message': 'Repository starred successfully.'} Common variations
You can use different models like gpt-4o or gpt-4o-mini depending on your needs. The tools parameter replaces the deprecated functions parameter for tool integration. Async usage is possible by using async clients and await syntax.
import asyncio
import os
from openai import OpenAI
from composio_openai import ComposioToolSet, Action
async def async_chat():
toolset = ComposioToolSet(api_key=os.environ["COMPOSIO_API_KEY"])
tools = toolset.get_tools(actions=[Action.GITHUB_STAR_A_REPOSITORY])
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
response = await client.chat.completions.acreate(
model="gpt-4o",
tools=tools,
messages=[{"role": "user", "content": "Star the openai/openai-python repo"}]
)
print("Async response:", response.choices[0].message.content)
result = toolset.handle_tool_calls(response)
print("Async tool call result:", result)
asyncio.run(async_chat()) output
Async response: I have starred the openai/openai-python repository for you.
Async tool call result: {'success': True, 'message': 'Repository starred successfully.'} Troubleshooting
- If you get authentication errors, verify that
COMPOSIO_API_KEYandOPENAI_API_KEYare correctly set in your environment. - If tools are not recognized, ensure you are passing the
toolsparameter (not deprecatedfunctions) in your chat completion call. - Check your network connectivity and API key permissions if requests fail.
Key Takeaways
- Use
ComposioToolSetwith your Composio API key to access tools. - Pass tools via the
toolsparameter inOpenAI.chat.completions.createcalls. - Always load API keys from
os.environfor security and flexibility. - Async calls are supported with
acreateandawaitsyntax. - Troubleshoot authentication by verifying environment variables and API key scopes.