Skip to main content

split-python4gpt

PyPI version License Docs CI

split-python4gpt processes and reorganizes large Python projects into minified, type-annotated, and token-limited files. This prepares codebases for analysis by Large Language Models (LLMs) like OpenAI's GPT series, allowing them to handle data in manageable chunks.

Overview

What is split-python4gpt?

A command-line and programmatic tool that takes Python files or project directories as input and performs:

  1. Type Inference: Integrates with pytype to add type hints
  2. Minification: Uses python-minifier with configurable options
  3. Code Summarization: Replaces large functions/classes with ... and AI-generated summaries (requires OpenAI API key)
  4. Splitting for LLMs: Breaks processed code into smaller files respecting token limits

Who is it for?

  • Developers working with LLMs who need to analyze large Python codebases
  • Researchers in software engineering or NLP preprocessing Python code for studies
  • Anyone reducing Python code size while preserving structure before passing to token-sensitive systems

Why is it useful?

  • Manages LLM Context Limits: Breaks code into chunks that fit finite context windows
  • Reduces Token Count: Minification and summarization significantly cut tokens, reducing processing time and API costs
  • Improves Code Clarity: Type hints help LLMs understand code; summaries provide context for complex blocks
  • Automates Preprocessing: Handles tedious parts of the MLOps pipeline for code-based LLM tasks

Features

  • Process Single Files or Entire Directories: Handles individual scripts or recursively processes all .py files
  • Optional Type Inference: Uses pytype to add type annotations
  • Comprehensive Minification: Leverages python-minifier with configurable options:
    • Remove docstrings (mini_docs)
    • Rename global/local variables (mini_globs, mini_locs)
    • Hoist literal statements (mini_lits)
    • Remove type annotations (mini_annotations)
    • Remove assert and debugging statements (mini_asserts, mini_debug)
    • Combine imports (mini_imports)
    • Remove object base from classes (mini_obj)
    • Remove pass statements (mini_pass)
    • Convert positional to keyword arguments (mini_posargs)
    • Remove explicit return None (mini_retnone)
    • Remove shebang (mini_shebang)
  • AI-Powered Code Summarization: Replaces large functions/classes with ... and summaries via OpenAI models
  • Token-Based Splitting: Uses tiktoken to count tokens and split code into multiple files respecting limits
  • Configurable Output: Specify output directories for processed files and type stubs
  • Preserves Relative Paths: Maintains original project structure in output

Installation

Prerequisites:

  • Python 3.10 or later (3.11, 3.12, 3.13 are all supported)
  • An OpenAI API key is required only for LLM summarisation (export OPENAI_API_KEY="your_api_key_here")
  • pytype is optional — install the [types] extra for type inference

Installation

pip install split-python4gpt

Optional extras:

# Type inference via pytype
pip install "split-python4gpt[types]"

# LLM summarisation (tiktoken + simpleaichat)
pip install "split-python4gpt[llm]"

From source

  1. Install in virtual environment:
    python -m venv .venv
    source .venv/bin/activate
    pip install -e ".[dev]"
    

Installs dependencies: fire, tiktoken, python-minifier, pytype, and simpleaichat.

Option 2: Pre-built Binary

Download from releases page:

  • Linux: mdsplit4gpt-linux-x86_64
  • macOS: mdsplit4gpt-macos-x86_64
  • Windows: mdsplit4gpt-windows-x86_64.exe

Make executable and add to PATH:

chmod +x mdsplit4gpt-linux-x86_64
mv mdsplit4gpt-linux-x86_64 ~/.local/bin/mdsplit4gpt

Option 3: Source Installation

git clone https://github.com/twardoch/split-python4gpt.git
cd split-python4gpt
./scripts/install-dev.sh

Usage

Command-Line Interface

Primary command: mdsplit4gpt

mdsplit4gpt [PATH_OR_FOLDER] [OPTIONS]

Key Arguments:

  • path_or_folder: Input Python file or folder
  • --out: Output folder for processed files (defaults to input folder)
  • --pyis: Directory for .pyi files (defaults to output folder)
  • --types: Infer types using PyType (default: True)
  • --mini: Minify scripts (default: True)

Minification Options:

  • --mini_docs: Remove docstrings
  • --mini_globs: Rename global names (default: False)
  • --mini_locs: Rename local names (default: False)
  • --mini_lits: Hoist literal statements
  • --mini_annotations: Remove annotations
  • --mini_asserts: Remove asserts
  • --mini_debug: Remove debugging statements
  • --mini_imports: Combine imports
  • --mini_obj: Remove object base
  • --mini_pass: Remove pass statements
  • --mini_posargs: Convert positional to keyword args
  • --mini_retnone: Remove explicit return None
  • --mini_shebang: Remove shebang (set to False to preserve)

Examples:

  1. Process single file with minification and type inference:

    mdsplit4gpt my_script.py --out output_dir
    

    Creates output_dir/my_script.py and output_dir/split4gpt/split1.py.

  2. Process project without type inference, preserving docstrings:

    mdsplit4gpt my_project/ --out processed_project/ --types=False --mini_docs=False
    

Programmatic Usage

from pathlib import Path
from split_python4gpt import PyLLMSplitter

# Set OPENAI_API_KEY environment variable for summarization
# os.environ["OPENAI_API_KEY"] = "your_api_key"

splitter = PyLLMSplitter(
    gptok_model="gpt-3.5-turbo", gptok_limit=4000, gptok_threshold=200
)

input_path = "path/to/your/python_project_or_file"
output_dir = "path/to/output_directory"
pyi_dir = "path/to/pyi_files_directory"

processed_file_paths = splitter.process_py(
    py_path_or_folder=input_path,
    out_py_folder=output_dir,
    pyi_folder=pyi_dir,
    types=True,
    mini=True,
    remove_literal_statements=True,
    rename_globals=False,
)

splitter.write_splits()

print(f"Processed files: {processed_file_paths}")
print(f"LLM splits: {Path(output_dir) / 'split4gpt'}")

Technical Details

Workflow

  1. File Discovery: Processes single files or recursively finds all *.py files in folders

  2. Initialization: Resolves and creates input/output directories; copies files if needed

  3. Per-file Processing:

    • Type Inference: Runs pytype subprocess, merges generated stubs back into source
    • Minification: Passes code through python-minifier with specified options
  4. LLM Preparation:

    • Parses code into AST
    • Processes top-level nodes (imports, assignments, functions, classes)
    • For each function/class:
      • Calculates token count with tiktoken
      • If count exceeds threshold, replaces body with ... and AI summary
  5. Splitting:

    • Collects all processed sections
    • Accumulates sections into portions respecting token limits
    • Writes splitN.py files in split4gpt subdirectory

Output Structure

  • Processed Files: Minified/type-annotated versions in output directory (modifies in-place if no output specified)
  • .pyi Files: Type stubs in specified directory (defaults to output)
  • split4gpt Directory: Contains final chunks for LLM consumption

Core Components

  • PyTypingMinifier: Handles file management, type inference, and minification
  • PyBodySummarizer: AST transformer that summarizes large functions/classes
  • PyLLMSplitter: Inherits from PyTypingMinifier, adds token counting and splitting

Contributing

Development Setup

  1. Fork repository
  2. Create branch: git checkout -b feature/your-feature-name
  3. Set up environment:
    ./scripts/install-dev.sh
    

Development Workflow

  1. Make changes

  2. Run tests:

    ./scripts/build-and-test.sh
    ./scripts/build-and-test.sh --with-coverage
    ./scripts/build-and-test.sh --with-performance
    
  3. Code standards:

    • Formatted with black
    • Imports sorted with isort
    • PEP 8 compliant
    • Linted with flake8
    • Pre-commit hooks run automatically
  4. Add tests in tests/ directory

  5. Commit with descriptive message

  6. Push branch: git push origin feature/your-feature-name

  7. Create Pull Request

Release Process

  1. Create release:

    ./scripts/release.sh 1.2.3
    
  2. Automated CI/CD:

    • Tests run on push/PR for Linux, macOS, Windows
    • Git tags trigger: PyPI publication, binary builds, GitHub releases
  3. Scripts:

    • install-dev.sh: Development setup
    • build-and-test.sh: Testing
    • release.sh: Create releases
    • get_version.py: Check current version
    • validate_tag.py: Validate version format

License

Apache License 2.0. See LICENSE.txt.

Authors

Scaffolded with PyScaffold.

Metadata

Release files for split-python4gpt 1.0.5

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for split-python4gpt 1.0.5
File Size Uploaded Provenance Actions
split_python4gpt-1.0.5.tar.gz 24.6 kB With provenance Details for split_python4gpt-1.0.5.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for split-python4gpt 1.0.5
File Interpreter ABI Platform Actions
split_python4gpt-1.0.5-py3-none-any.whl Python 3 none any Details for split_python4gpt-1.0.5-py3-none-any.whl

Total release size: 41.9 kB

Release files / split_python4gpt-1.0.5.tar.gz

Download URL split_python4gpt-1.0.5.tar.gz
Size 24.6 kB
Tags Source
SHA-256 checksum
How to use checksums
74ad94a24a04bbc17a45f25f99c8e4416873b95f78abcd3eb5626f98a98def65
BLAKE2b-256 checksum
How to use checksums
d0df17b275bcda0009233ca829c0c2806522c3911014b20268bcbd8ae898bb11
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 17, 2026.

Transparency log

Release files / split_python4gpt-1.0.5-py3-none-any.whl

Download URL split_python4gpt-1.0.5-py3-none-any.whl
Size 17.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b15b8dd1f54af39d6fce2a84f2c778db967885261e0e6129a92a806808ffec06
BLAKE2b-256 checksum
How to use checksums
6ac30d4bc553ad640160f07169c77fe0fc1fa9cb4050929ee28c83c8fd27026e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 17, 2026.

Transparency log

Security for split-python4gpt 1.0.5

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for split_python4gpt-1.0.5.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 17, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page