Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
| 1 | """ |
| 2 | OpenAI provider implementation. |
| 3 | """ |
| 4 | |
| 5 | from __future__ import annotations |
| 6 | |
| 7 | import typing as t |
| 8 | |
| 9 | from openai.types.chat.chat_completion import ChatCompletion |
| 10 | from openai.types.chat.chat_completion_message_tool_call import ( |
| 11 | ChatCompletionMessageToolCall, |
| 12 | ) |
| 13 | from openai.types.chat.chat_completion_tool_param import ChatCompletionToolParam |
| 14 | from openai.types.shared_params.function_definition import FunctionDefinition |
| 15 | from openai.types.shared_params.function_parameters import FunctionParameters |
| 16 | |
| 17 | from composio.core.provider import NonAgenticProvider, ToolCallSession |
| 18 | from composio.types import Modifiers, Tool, ToolExecutionResponse |
| 19 | from composio.utils.shared import normalize_tool_arguments |
| 20 | |
| 21 | OpenAITool: t.TypeAlias = ChatCompletionToolParam |
| 22 | OpenAIToolCollection: t.TypeAlias = t.List[OpenAITool] |
| 23 | |
| 24 | |
| 25 | class OpenAIProvider( |
| 26 | NonAgenticProvider[OpenAITool, OpenAIToolCollection], name="openai" |
| 27 | ): |
| 28 | """OpenAIProvider class definition""" |
| 29 | |
| 30 | def wrap_tool(self, tool: Tool) -> OpenAITool: |
| 31 | return ChatCompletionToolParam( |
| 32 | function=FunctionDefinition( |
| 33 | name=tool.slug, |
| 34 | description=tool.description, |
| 35 | parameters=t.cast(FunctionParameters, tool.input_parameters), |
| 36 | strict=None, |
| 37 | ), |
| 38 | type="function", |
| 39 | ) |
| 40 | |
| 41 | def wrap_tools(self, tools: t.Sequence[Tool]) -> OpenAIToolCollection: |
| 42 | return [self.wrap_tool(tool) for tool in tools] |
| 43 | |
| 44 | @t.overload |
| 45 | def execute_tool_call( |
| 46 | self, |
| 47 | user_id: str, |
| 48 | tool_call: ChatCompletionMessageToolCall, |
| 49 | modifiers: t.Optional[Modifiers] = None, |
| 50 | ) -> ToolExecutionResponse: ... |
| 51 | |
| 52 | @t.overload |
| 53 | def execute_tool_call( |
| 54 | self, |
| 55 | *, |
| 56 | session: ToolCallSession, |
| 57 | tool_call: ChatCompletionMessageToolCall, |
| 58 | ) -> ToolExecutionResponse: ... |
| 59 | |
| 60 | def execute_tool_call( |
| 61 | self, |
| 62 | user_id: t.Optional[str] = None, |
| 63 | tool_call: t.Optional[ChatCompletionMessageToolCall] = None, |
| 64 | modifiers: t.Optional[Modifiers] = None, |
| 65 | *, |
| 66 | session: t.Optional[ToolCallSession] = None, |
| 67 | ) -> ToolExecutionResponse: |
| 68 | """Execute a tool call. |
| 69 | |
| 70 | :param tool_call: Tool call metadata. |
| 71 | :param user_id: User ID for direct tool execution. |
| 72 | :param session: Tool Router session that produced session tools. |
| 73 | :return: Object containing output data from the tool call. |
| 74 | """ |
| 75 | if tool_call is None: |
| 76 | raise TypeError("tool_call is required") |
| 77 | target = self.resolve_tool_call_execution_target( |
| 78 | user_id=user_id, session=session |
| 79 | ) |
| 80 | |
| 81 | # OpenAI always serializes tool arguments as a JSON string; normalize |
| 82 | # tolerates empty / object-shaped payloads too (issue #2406). |
| 83 | return self.execute_tool_for_target( |
| 84 | target=target, |
| 85 | slug=tool_call.function.name, |
| 86 | arguments=normalize_tool_arguments(tool_call.function.arguments), |
| 87 | modifiers=modifiers, |
| 88 | ) |
| 89 | |
| 90 | @t.overload |
| 91 | def handle_tool_calls( |
| 92 | self, |
| 93 | user_id: str, |
| 94 | response: ChatCompletion, |
| 95 | modifiers: t.Optional[Modifiers] = None, |
| 96 | ) -> t.List[ToolExecutionResponse]: ... |
| 97 | |
| 98 | @t.overload |
| 99 | def handle_tool_calls( |
| 100 | self, |
| 101 | *, |
| 102 | session: ToolCallSession, |
| 103 | response: ChatCompletion, |
| 104 | ) -> t.List[ToolExecutionResponse]: ... |
| 105 | |
| 106 | def handle_tool_calls( |
| 107 | self, |
| 108 | user_id: t.Optional[str] = None, |
| 109 | response: t.Optional[ChatCompletion] = None, |
| 110 | modifiers: t.Optional[Modifiers] = None, |
| 111 | *, |
| 112 | session: t.Optional[ToolCallSession] = None, |
| 113 | ) -> t.List[ToolExecutionResponse]: |
| 114 | """ |
| 115 | Handle tool calls from OpenAI chat completion object. |
| 116 | |
| 117 | :param response: Chat completion object from |
| 118 | openai.OpenAI.chat.completions.create function call |
| 119 | :param user_id: User ID for direct tool execution. |
| 120 | :param session: Tool Router session that produced session tools. |
| 121 | :return: A list of output objects from the function calls. |
| 122 | """ |
| 123 | if response is None: |
| 124 | raise TypeError("response is required") |
| 125 | self.resolve_tool_call_execution_target(user_id=user_id, session=session) |
| 126 | if session is not None and modifiers is not None: |
| 127 | raise ValueError( |
| 128 | "Direct execution modifiers cannot be used with a Tool Router session" |
| 129 | ) |
| 130 | outputs = [] |
| 131 | # Only the first choice is actionable: its tool results feed back into a |
| 132 | # single assistant turn. With n > 1, iterating every choice would run each |
| 133 | # tool call once per choice and orphan the tool_call_ids belonging to the |
| 134 | # alternative completions. |
| 135 | choice = response.choices[0] if response.choices else None |
| 136 | # A single assistant message can carry several tool calls (parallel tool |
| 137 | # calls, on by default); each one needs its own tool result. |
| 138 | if choice is not None and choice.message.tool_calls is not None: |
| 139 | for tool_call in choice.message.tool_calls: |
| 140 | call = t.cast(ChatCompletionMessageToolCall, tool_call) |
| 141 | result = ( |
| 142 | self.execute_tool_call( |
| 143 | session=session, |
| 144 | tool_call=call, |
| 145 | ) |
| 146 | if session is not None |
| 147 | else self.execute_tool_call( |
| 148 | user_id=t.cast(str, user_id), |
| 149 | tool_call=call, |
| 150 | modifiers=modifiers, |
| 151 | ) |
| 152 | ) |
| 153 | outputs.append(result) |
| 154 | return outputs |