code: from typing import Optional, Type
  
  from langchain.callbacks.manager import CallbackManagerForToolRun
  from langchain.pydantic_v1 import BaseModel, Field
  from langchain.tools.base import BaseTool
  from langchain.tools.file_management.utils import (
      INVALID_PATH_TEMPLATE,
      BaseFileToolMixin,
      FileValidationError,
  )
  
  
  class WriteFileInput(BaseModel):
      """Input for WriteFileTool."""
  
      file_path: str = Field(..., description="name of file")
      text: str = Field(..., description="text to write to file")
      append: bool = Field(
          default=False, description="Whether to append to an existing file."
      )
  
  
  class WriteFileTool(BaseFileToolMixin, BaseTool):
      """Tool that writes a file to disk."""
  
      name: str = "write_file"
      args_schema: Type[BaseModel] = WriteFileInput
      description: str = "Write file to disk"
  
      def _run(
          self,
          file_path: str,
          text: str,
          append: bool = False,
          run_manager: Optional[CallbackManagerForToolRun] = None,
      ) -> str:
          try:
              write_path = self.get_relative_path(file_path)
          except FileValidationError:
              return INVALID_PATH_TEMPLATE.format(arg_name="file_path", value=file_path)
          try:
              write_path.parent.mkdir(exist_ok=True, parents=False)
              mode = "a" if append else "w"
              with write_path.open(mode, encoding="utf-8") as f:
                  f.write(text)
              return f"File written successfully to {file_path}."
          except Exception as e:
              return "Error: " + str(e)
  
      # TODO: Add aiofiles method
  
apis: langchain.pydantic_v1.Field langchain.tools.file_management.utils.INVALID_PATH_TEMPLATE.format
extract_api: [((415, 453), 'langchain.pydantic_v1.Field', 'Field', (['...'], {'description': '"""name of file"""'}), "(..., description='name of file')\n", (420, 453), False, 'from langchain.pydantic_v1 import BaseModel, Field\n'), ((470, 517), 'langchain.pydantic_v1.Field', 'Field', (['...'], {'description': '"""text to write to file"""'}), "(..., description='text to write to file')\n", (475, 517), False, 'from langchain.pydantic_v1 import BaseModel, Field\n'), ((537, 611), 'langchain.pydantic_v1.Field', 'Field', ([], {'default': '(False)', 'description': '"""Whether to append to an existing file."""'}), "(default=False, description='Whether to append to an existing file.')\n", (542, 611), False, 'from langchain.pydantic_v1 import BaseModel, Field\n'), ((1153, 1220), 'langchain.tools.file_management.utils.INVALID_PATH_TEMPLATE.format', 'INVALID_PATH_TEMPLATE.format', ([], {'arg_name': '"""file_path"""', 'value': 'file_path'}), "(arg_name='file_path', value=file_path)\n", (1181, 1220), False, 'from langchain.tools.file_management.utils import INVALID_PATH_TEMPLATE, BaseFileToolMixin, FileValidationError\n')]