Custom Actions are powerful building blocks that enable you to create custom functionality while leveraging existing tool authentication.

Creating a Custom Action with Authentication

1

Install Dependencies

pip install composio_openai openai
2

Import necessary modules & initialize them

import typing as t
from composio_openai import ComposioToolSet, action, Action
from openai import OpenAI

openai_client = OpenAI()
toolset = ComposioToolSet()
3

Creating a custom action

Below are examples of creating a custom action called list_repositories (Python) & star_repo (JavaScript) that integrates with the github tool.

You need to add the action, input parameters & return content description, this is what the LLM will use to understand the action.

The execute_request/executeRequest method is used to make API calls, it accepts the following arguments:

  • endpoint: Endpoint URL. The base URL of the API will be prepended to this. You can find it in your connection’s Connection Info section
  • method: HTTP method to use
  • body: Request body to pass to the API
  • parameters: Custom Authentication Parameters
  • connection_id: ID of the connected account

Since github is a registered tool in Composio, the authentication credentials are automatically injected into your custom action!

@action(toolname="github")
def list_repositories(
    owner: str,
    execute_request: t.Callable,
) -> list[str]:
    """
    List repositories for a user.

    :param owner: Name of the owner.
    :return repositories: List of repositories for given user.
    """
    return [
        repo["name"]
        for repo in execute_request(f"/users/{owner}/repos", "get", None, None).get(
            "data", []
        )
    ]
4

Executing the Custom Action

Executing the custom action using LLM. Learn how to execute the custom action without LLM here.

tools = toolset.get_tools(actions=[list_repositories])

task = "List all the repositories for the organization composiohq"

response = openai_client.chat.completions.create(
model="gpt-4o-mini",
tools=tools,
messages=
    [
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": task},
    ],
)

result = toolset.handle_tool_calls(response)
print(result)

How to Use Connection Parameters of an Account?

Connection Parameters of an account are available in the connected_account parameter of the custom action

@action(toolname="github")
def list_repositories(
    owner: str,
    execute_request: t.Callable,
    connected_account: ConnectedAccountModel
) -> list[str]:
    """
    List repositories for a user.

    :param owner: Name of the owner.
    :return repositories: List of repositories for given user.
    """
    print(connected_account.connectionParams)
    return [
        # Custom Action Logic
    ]

Below is an example of the connection parameters for a GitHub account:

scope='********'
base_url='https://api.github.com'
client_id='********'
token_type='********'
access_token='gho_Y3rMb*************'
client_secret='********'
consumer_id=None
consumer_secret=None
headers={
  'Authorization': 'Bearer gho_Y***************',
  'x-request-id': '80ce421*********************'
}
queryParams={}

Why Use Custom Actions?

Custom Actions provide several advantages:

  • Data privacy: Execution happens on the user’s machine, ensuring sensitive data doesn’t leave the local environment.
  • Flexibility: Users can create and customize as many tools and actions as needed.
  • Compatibility: Custom actions can be integrated seamlessly across various Composio-supported platforms.