Before Execution Modifiers

Learn how to use before execution modifiers to modify tool arguments before execution.

Before execution modifiers are part of Composio SDK’s powerful middleware capabilities that allow you to customize and extend the behavior of tools.

Before Execution Modifiers

These modifiers are called before the tool is executed by the LLM. This allows you to modify the arguments called by the LLM before they are executed by Composio.

Useful for:

  • Injecting an argument into the tool execution.
  • Overriding the arguments emitted by the LLM.
Before Execution Modifier

Below we use the beforeExecute modifier to modify the number of posts returned by the HACKERNEWS_GET_LATEST_POSTS tool.

Since completion providers don’t have a function execution step — Composio will execute the tool call directly.

Hence, the modifier is configured on the tools.execute method.

1from openai import OpenAI
2from composio import Composio, before_execute
3from composio.types import ToolExecuteParams
4
5composio = Composio()
6openai_client = OpenAI()
7user_id = "user@email.com"
8
9@before_execute(tools=["HACKERNEWS_GET_LATEST_POSTS"])
10def before_execute_modifier(
11 tool: str,
12 toolkit: str,
13 params: ToolExecuteParams,
14) -> ToolExecuteParams:
15 params["arguments"]["size"] = 1
16 return params
17
18
19# Get tools
20tools = composio.tools.get(user_id=user_id, slug="HACKERNEWS_GET_LATEST_POSTS")
21
22# Get response from the LLM
23response = openai_client.chat.completions.create(
24 model="gpt-4o-mini",
25 tools=tools,
26 messages=[{"role": "user", "content": "Fetch latest posts from hackernews"}],
27)
28print(response)
29
30# Execute the function calls.
31result = composio.provider.handle_tool_calls(
32 response=response,
33 user_id="default",
34 modifiers=[
35 before_execute_modifier,
36 ],
37)
38print(result)