split-python4gpt
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:
- Type Inference: Integrates with
pytypeto add type hints - Minification: Uses
python-minifierwith configurable options - Code Summarization: Replaces large functions/classes with
...and AI-generated summaries (requires OpenAI API key) - 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
.pyfiles - Optional Type Inference: Uses
pytypeto add type annotations - Comprehensive Minification: Leverages
python-minifierwith 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
assertand debugging statements (mini_asserts,mini_debug) - Combine imports (
mini_imports) - Remove
objectbase from classes (mini_obj) - Remove
passstatements (mini_pass) - Convert positional to keyword arguments (
mini_posargs) - Remove explicit
return None(mini_retnone) - Remove shebang (
mini_shebang)
- Remove docstrings (
- AI-Powered Code Summarization: Replaces large functions/classes with
...and summaries via OpenAI models - Token-Based Splitting: Uses
tiktokento 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") pytypeis 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
- 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.pyifiles (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:
-
Process single file with minification and type inference:
mdsplit4gpt my_script.py --out output_dir
Creates
output_dir/my_script.pyandoutput_dir/split4gpt/split1.py. -
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
-
File Discovery: Processes single files or recursively finds all
*.pyfiles in folders -
Initialization: Resolves and creates input/output directories; copies files if needed
-
Per-file Processing:
- Type Inference: Runs
pytypesubprocess, merges generated stubs back into source - Minification: Passes code through
python-minifierwith specified options
- Type Inference: Runs
-
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
- Calculates token count with
-
Splitting:
- Collects all processed sections
- Accumulates sections into portions respecting token limits
- Writes
splitN.pyfiles insplit4gptsubdirectory
Output Structure
- Processed Files: Minified/type-annotated versions in output directory (modifies in-place if no output specified)
.pyiFiles: Type stubs in specified directory (defaults to output)split4gptDirectory: Contains final chunks for LLM consumption
Core Components
PyTypingMinifier: Handles file management, type inference, and minificationPyBodySummarizer: AST transformer that summarizes large functions/classesPyLLMSplitter: Inherits fromPyTypingMinifier, adds token counting and splitting
Contributing
Development Setup
- Fork repository
- Create branch:
git checkout -b feature/your-feature-name - Set up environment:
./scripts/install-dev.sh
Development Workflow
-
Make changes
-
Run tests:
./scripts/build-and-test.sh ./scripts/build-and-test.sh --with-coverage ./scripts/build-and-test.sh --with-performance
-
Code standards:
- Formatted with
black - Imports sorted with
isort - PEP 8 compliant
- Linted with
flake8 - Pre-commit hooks run automatically
- Formatted with
-
Add tests in
tests/directory -
Commit with descriptive message
-
Push branch:
git push origin feature/your-feature-name -
Create Pull Request
Release Process
-
Create release:
./scripts/release.sh 1.2.3
-
Automated CI/CD:
- Tests run on push/PR for Linux, macOS, Windows
- Git tags trigger: PyPI publication, binary builds, GitHub releases
-
Scripts:
install-dev.sh: Development setupbuild-and-test.sh: Testingrelease.sh: Create releasesget_version.py: Check current versionvalidate_tag.py: Validate version format
License
Apache License 2.0. See LICENSE.txt.
Authors
- Adam Twardoch (@twardoch)
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)
| 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)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| 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 logRelease 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 logSecurity 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.
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 logA 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.