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 | """ComposioLangChain class definition""" |
| 2 | |
| 3 | import types |
| 4 | import typing as t |
| 5 | from inspect import Signature |
| 6 | |
| 7 | import pydantic |
| 8 | from langchain_core.tools import StructuredTool as BaseStructuredTool |
| 9 | |
| 10 | from composio.core.provider import AgenticProvider, AgenticProviderExecuteFn |
| 11 | from composio.types import Tool |
| 12 | from composio.utils.pydantic import parse_pydantic_error |
| 13 | from composio.utils.shared import ( |
| 14 | get_signature_format_from_schema_params, |
| 15 | json_schema_to_model, |
| 16 | normalize_tool_arguments, |
| 17 | reinstate_reserved_python_keywords, |
| 18 | substitute_reserved_python_keywords, |
| 19 | validate_and_serialize_tool_arguments, |
| 20 | ) |
| 21 | |
| 22 | |
| 23 | class StructuredTool(BaseStructuredTool): # type: ignore[misc] |
| 24 | def _to_args_and_kwargs( |
| 25 | self, |
| 26 | tool_input: t.Union[str, t.Dict[str, t.Any]], |
| 27 | tool_call_id: t.Optional[str], |
| 28 | ) -> t.Tuple[t.Tuple[str, ...], t.Dict[str, t.Any]]: |
| 29 | """Validate dynamic-only schemas before LangChain's no-field shortcut.""" |
| 30 | args_schema = self.args_schema |
| 31 | if ( |
| 32 | isinstance(tool_input, dict) |
| 33 | and isinstance(args_schema, type) |
| 34 | and issubclass(args_schema, pydantic.BaseModel) |
| 35 | ): |
| 36 | arguments = validate_and_serialize_tool_arguments(args_schema, tool_input) |
| 37 | return (), arguments |
| 38 | return super()._to_args_and_kwargs(tool_input, tool_call_id) |
| 39 | |
| 40 | def run(self, *args, **kwargs): |
| 41 | try: |
| 42 | return super().run(*args, **kwargs) |
| 43 | except pydantic.ValidationError as e: |
| 44 | return {"successful": False, "error": parse_pydantic_error(e), "data": None} |
| 45 | |
| 46 | |
| 47 | class LanggraphProvider( |
| 48 | AgenticProvider[StructuredTool, t.List[StructuredTool]], |
| 49 | name="langgraph", |
| 50 | ): |
| 51 | """ |
| 52 | Composio toolset for Langchain framework. |
| 53 | """ |
| 54 | |
| 55 | def _wrap_action( |
| 56 | self, |
| 57 | tool: str, |
| 58 | description: str, |
| 59 | schema_params: t.Dict, |
| 60 | keywords: t.Dict, |
| 61 | execute_tool: AgenticProviderExecuteFn, |
| 62 | ): |
| 63 | def function(**kwargs: t.Any) -> t.Dict: |
| 64 | """Wrapper function for composio action.""" |
| 65 | kwargs = reinstate_reserved_python_keywords( |
| 66 | request=kwargs, keywords=keywords |
| 67 | ) |
| 68 | # Normalize defensively so a stringified payload is coerced to a dict (issue #2406). |
| 69 | return execute_tool(tool, normalize_tool_arguments(kwargs)) |
| 70 | |
| 71 | action_func = types.FunctionType( |
| 72 | function.__code__, |
| 73 | globals=globals(), |
| 74 | name=tool, |
| 75 | closure=function.__closure__, |
| 76 | ) |
| 77 | action_func.__signature__ = Signature( # type: ignore |
| 78 | parameters=get_signature_format_from_schema_params( |
| 79 | schema_params=schema_params, |
| 80 | skip_default=self.skip_default, |
| 81 | ) |
| 82 | ) |
| 83 | action_func.__doc__ = description |
| 84 | return action_func |
| 85 | |
| 86 | def wrap_tool( |
| 87 | self, tool: Tool, execute_tool: AgenticProviderExecuteFn |
| 88 | ) -> StructuredTool: |
| 89 | """Wraps composio tool as Langchain StructuredTool object.""" |
| 90 | schema_params, keywords = substitute_reserved_python_keywords( |
| 91 | schema=tool.input_parameters |
| 92 | ) |
| 93 | return t.cast( |
| 94 | StructuredTool, |
| 95 | StructuredTool.from_function( |
| 96 | name=tool.slug, |
| 97 | description=tool.description, |
| 98 | args_schema=json_schema_to_model( |
| 99 | json_schema=schema_params, |
| 100 | skip_default=self.skip_default, |
| 101 | ), |
| 102 | return_schema=True, |
| 103 | func=self._wrap_action( |
| 104 | tool=tool.slug, |
| 105 | description=tool.description, |
| 106 | schema_params=schema_params, |
| 107 | keywords=keywords, |
| 108 | execute_tool=execute_tool, |
| 109 | ), |
| 110 | handle_tool_error=True, |
| 111 | handle_validation_error=True, |
| 112 | ), |
| 113 | ) |
| 114 | |
| 115 | def wrap_tools( |
| 116 | self, |
| 117 | tools: t.Sequence[Tool], |
| 118 | execute_tool: AgenticProviderExecuteFn, |
| 119 | ) -> t.List[StructuredTool]: |
| 120 | """ |
| 121 | Get composio tools wrapped as Langchain StructuredTool objects. |
| 122 | """ |
| 123 | return [self.wrap_tool(tool=tool, execute_tool=execute_tool) for tool in tools] |