File Generation

File Generation

The `FileGenerationTools` toolkit enables Agents and Teams to generate files in multiple formats.

v2.0.6

Supported file types:

  • JSON
  • CSV
  • PDF
  • DOCX
  • TXT
  • HTML
  • Source code

Prerequisites

  1. Install dependencies:

    uv pip install agno reportlab python-docx openai sqlalchemy

    reportlab is required for PDF generation, python-docx for DOCX generation, and sqlalchemy for the SqliteDb used in the example. JSON, CSV, TXT, and HTML generation have no external dependencies.

  2. 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.

file_generation_tools.py
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

ParameterTypeDefaultDescription
enable_json_generationboolTrueEnables JSON file generation
enable_csv_generationboolTrueEnables CSV file generation
enable_pdf_generationboolTrueEnables PDF file generation (requires reportlab)
enable_docx_generationboolTrueEnables DOCX file generation (requires python-docx)
enable_txt_generationboolTrueEnables text file generation
enable_html_generationboolTrueEnables HTML file generation
enable_code_generationboolTrueEnables source-code file generation.
output_directoryOptional[str]NoneCustom output directory path. Implies save_files=True
save_filesboolFalseSaves generated files to disk when True (to output_directory, or the current directory if not set)
allboolFalseEnables all file generation types when True

Toolkit Functions

NameDescription
generate_json_fileGenerates a JSON file from data (dict, list, or JSON string)
generate_csv_fileGenerates a CSV file from tabular data
generate_pdf_fileGenerates a PDF document from text content
generate_docx_fileGenerates a Word (DOCX) document from text content
generate_text_fileGenerates a plain text file from string content
generate_html_fileGenerates an HTML file from a complete HTML5 document
generate_code_fileGenerates a source-code file from code, optional language and filename.

Developer Resources