File Generation
File Generation
The `FileGenerationTools` toolkit enables Agents and Teams to generate files in multiple formats.
v2.0.6
file_generation_tools.py
Supported file types:
- JSON
- CSV
- DOCX
- TXT
- HTML
- Source code
Prerequisites
-
Install dependencies:
uv pip install agno reportlab python-docx openai sqlalchemyreportlabis required for PDF generation,python-docxfor DOCX generation, andsqlalchemyfor theSqliteDbused in the example. JSON, CSV, TXT, and HTML generation have no external dependencies. -
Set your credentials: For OpenAI API:
export OPENAI_API_KEY="your-openai-api-key"
Example
The following agent will generate files in different formats based on user requests.
from agno.agent import Agent
from agno.db.sqlite import SqliteDb
from agno.models.openai import OpenAIResponses
from agno.tools.file_generation import FileGenerationTools
agent = Agent(
model=OpenAIResponses(id="gpt-5.2"),
db=SqliteDb(db_file="tmp/test.db"),
tools=[FileGenerationTools(output_directory="tmp")],
description="You are a helpful assistant that can generate files in various formats.",
instructions=[
"When asked to create files, use the appropriate file generation tools.",
"Always provide meaningful content and appropriate filenames.",
"Explain what you've created and how it can be used.",
],
markdown=True,
)
response = agent.run(
"Create a PDF report about renewable energy trends in 2024. Include sections on solar, wind, and hydroelectric power."
)
print(response.content)
if response.files:
for file in response.files:
print(f"Generated file: {file.filename} ({file.size} bytes)")
if file.filepath or file.url:
print(f"File location: {file.filepath or file.url}")Generated files are always available on the RunOutput object. Set output_directory (or save_files=True) to also save them to disk.
Toolkit Params
| Parameter | Type | Default | Description |
|---|---|---|---|
enable_json_generation | bool | True | Enables JSON file generation |
enable_csv_generation | bool | True | Enables CSV file generation |
enable_pdf_generation | bool | True | Enables PDF file generation (requires reportlab) |
enable_docx_generation | bool | True | Enables DOCX file generation (requires python-docx) |
enable_txt_generation | bool | True | Enables text file generation |
enable_html_generation | bool | True | Enables HTML file generation |
enable_code_generation | bool | True | Enables source-code file generation. |
output_directory | Optional[str] | None | Custom output directory path. Implies save_files=True |
save_files | bool | False | Saves generated files to disk when True (to output_directory, or the current directory if not set) |
all | bool | False | Enables all file generation types when True |
Toolkit Functions
| Name | Description |
|---|---|
generate_json_file | Generates a JSON file from data (dict, list, or JSON string) |
generate_csv_file | Generates a CSV file from tabular data |
generate_pdf_file | Generates a PDF document from text content |
generate_docx_file | Generates a Word (DOCX) document from text content |
generate_text_file | Generates a plain text file from string content |
generate_html_file | Generates an HTML file from a complete HTML5 document |
generate_code_file | Generates a source-code file from code, optional language and filename. |