How to authenticate Composio tools
Quick answer
Authenticate Composio tools by initializing
ComposioToolSet with your Composio API key from os.environ. Use this toolset with an OpenAI client authenticated via your OpenAI API key to enable tool usage in chat completions.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>=1.0 composio-core composio-openai
# In your shell or environment setup
export OPENAI_API_KEY=os.environ["OPENAI_API_KEY"]
export COMPOSIO_API_KEY=os.environ["COMPOSIO_API_KEY"] output
Collecting openai Collecting composio-core Collecting composio-openai Successfully installed openai composio-core composio-openai-<version>
Step by step
Use the ComposioToolSet class to authenticate Composio tools with your Composio API key. Then create an OpenAI client with your OpenAI API key. Pass the tools to the tools parameter in chat.completions.create to enable tool usage.
import os
from composio_openai import ComposioToolSet, Action
from openai import OpenAI
# Initialize Composio tools with your Composio API key
toolset = ComposioToolSet(api_key=os.environ["COMPOSIO_API_KEY"])
# Get tools you want to use, e.g., GitHub star repository action
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"])
# Use the tools in a chat completion request
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 tool calls if any
result = toolset.handle_tool_calls(response)
print("Tool call result:", result) output
Response: I have initiated the process to star the openai/openai-python repository.
Tool call result: {'success': True, 'message': 'Repository starred successfully.'} Common variations
You can authenticate Composio tools asynchronously or use different OpenAI models by changing the model parameter. The ComposioToolSet supports multiple actions; specify them in the actions list.
import asyncio
import os
from composio_openai import ComposioToolSet, Action
from openai import OpenAI
async def main():
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.create(
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(main()) output
Async response: I have started starring the openai/openai-python repository.
Async tool call result: {'success': True, 'message': 'Repository starred successfully.'} Troubleshooting
- If you get authentication errors, verify your
COMPOSIO_API_KEYandOPENAI_API_KEYenvironment variables are set correctly. - If tools are not recognized, ensure you installed
composio-coreandcomposio-openaipackages. - For unexpected API errors, check your network connectivity and API usage limits.
Key Takeaways
- Always initialize
ComposioToolSetwith your Composio API key from environment variables. - Pass the tools from
ComposioToolSet.get_tools()to thetoolsparameter inOpenAI.chat.completions.create. - Use
toolset.handle_tool_calls()to process tool call responses after completion. - Set both
OPENAI_API_KEYandCOMPOSIO_API_KEYin your environment before running code. - For async usage, await the OpenAI client calls and run inside an async event loop.