Midjargon
Midjargon is a Python library and command-line tool for parsing, manipulating, and converting Midjourney-style prompts.
Midjourney uses a distinctive syntax for its prompts, which we call "midjargon." This includes permutation with {} (e.g., a {red,blue} cat) and parameters prefixed by -- (e.g., --ar 16:9). While originally Midjourney-specific, this syntax has broader applications—particularly useful for building prompts for other generative AI models.
The midjargon package breaks down these prompts into structured components with full type safety and validation. It expands permutations, enforces parameter rules, converts to formats like Fal.ai, and serializes back to standard Midjourney format.
Who is Midjargon for?
- Midjourney Users: Those who frequently create or experiment with Midjourney prompts and want better control over variations and structure.
- Developers & AI Engineers: Programmers integrating prompt parsing into Python applications or custom tools.
- Content Creators & Prompt Engineers: People converting prompts between AI image services or systematically exploring large prompt spaces.
Why use Midjargon?
- Accurate Parsing: Splits complex Midjourney prompts into text, image URLs, parameters, and weights.
- Permutation Expansion: Handles all combinations from
{option1, option2}syntax, including nested groups and escaped characters. - Parameter Validation: Enforces Midjourney's parameter rules—numeric ranges, types, boolean flags, multi-value support.
- Image URL Support: Extracts and validates image URLs at the start of prompts.
- Weighted Prompts: Processes multi-part prompts with weighting syntax (e.g.,
text1 ::1 text2 ::0.5). - Type Safety: Full type hints and Pydantic models ensure clean data handling.
- CLI Tool: Command-line interface for fast parsing, expansion, and conversion with JSON output.
- Format Conversion: Parses Midjourney prompts and converts them to Fal.ai or back to Midjourney format.
Installation
Install with pip:
pip install midjargon
Or with uv:
uv pip install midjargon
Usage
Midjargon works as both a CLI tool and a Python library.
Command-Line Interface
Run midjargon directly, with python -m midjargon, or uv run midjargon -- if you prefer not to install globally.
Show available commands:
midjargon --help
python -m midjargon --help
Main commands:
-
perm: Expand permutations.midjargon perm "A {red, blue} cat sitting on a {mat, rug} --mood {happy, sleepy}"
-
json: Parse prompt to structured JSON. Expands permutations by default.midjargon json "A futuristic cityscape --ar 16:9 --v 6 --style raw"
-
mj: Convert to valid Midjourney prompt string(s).midjargon mj "A detailed portrait --ar 1:1 --stylize 250"
-
fal: Convert to Fal.ai format.midjargon fal "photo of a robot --ar 1:1 --seed 123"
Command-specific help:
midjargon perm --help
midjargon json --help
midjargon mj --help
midjargon fal --help
Python Library
Use midjargon programmatically in your projects.
Key functions from midjargon.core.converter:
permute_prompt(text: str) -> list[str]: Expands permutations.parse_prompt(text: str, permute: bool = True) -> MidjargonDict | list[MidjargonDict]: Parses to generic dictionaries.to_midjourney_prompts(prompt_input) -> MidjourneyPrompt | list[MidjourneyPrompt]: Parses and validates intoMidjourneyPromptPydantic models.to_fal_dicts(prompt_input) -> FalDict | list[FalDict]: Converts to Fal.ai dictionaries.
Example: Parsing for Midjourney
from midjargon.core.converter import to_midjourney_prompts
from midjargon.engines.midjourney.models import MidjourneyPrompt
prompt_string = "A {cyberpunk, steampunk} city --ar 16:9 --chaos {10, 20}"
results = to_midjourney_prompts(prompt_string)
if isinstance(results, list):
for i, mj_prompt_model in enumerate(results):
print(f"--- Variant {i + 1} ---")
print(f"Text: {mj_prompt_model.text}")
print(f"Aspect Ratio: {mj_prompt_model.aspect_ratio}")
print(f"Chaos: {mj_prompt_model.chaos}")
elif isinstance(results, MidjourneyPrompt):
print(f"Text: {results.text}")
else:
print("Unexpected result type")
# For simpler parsing without Midjourney validation:
from midjargon.core.converter import parse_prompt
parsed_dicts = parse_prompt("A simple prompt --param value")
if parsed_dicts:
print(f"\nGeneric parsed dict example: {parsed_dicts[0]}")
For more advanced use cases, explore the modules under midjargon.core and midjargon.engines.
Technical Overview
This section explains how midjargon works internally and how to contribute.
Processing Pipeline
-
Input Permutation (
midjargon.core.permutations):- The raw prompt is first expanded by
expand_permutations. - Handles
{option1, option2}, nested groups like{a, {b, c}}, and escaped characters (\,,\{,\}). - Output is a list of all possible prompt strings.
- The raw prompt is first expanded by
-
Core Parsing (
midjargon.core.parser):- Each expanded string is parsed by
parse_midjargon_prompt_to_dict. - Extracts leading image URLs.
- Separates text from parameter section (starting with
--). - Parameter parsing via
midjargon.core.parameters:- Splits on
--. - Separates names from values.
- Handles boolean flags and normalizes aliases.
- Splits on
- Each expanded string is parsed by
-
Validation & Modeling (
midjargon.engines):- Generic
MidjargonDictis passed to engine-specific parsers. - Each engine defines Pydantic models (e.g.,
MidjourneyPrompt) with type and constraint info. - Engine parser (e.g.,
MidjourneyParser) convertsMidjargonDictto typed model:- Applies type conversion.
- Validates against allowed ranges and enums.
- Sets defaults where needed.
- Validation logic uses Pydantic's
@field_validatorand@model_validator.
- Generic
-
Conversion & Serialization (
midjargon.core.converter):- High-level functions orchestrate permutation, parsing, and validation.
MidjourneyParser.to_prompt_string()serializes model back to prompt string.FalParserconvertsMidjargonDicttoFalDictfor Fal.ai API.
-
CLI (
midjargon.cli.main):- Built with
firelibrary. MidjargonCLIdefinesperm,json,mj,falcommands.- Calls
midjargon.core.converterfunctions and outputs JSON or formatted text.
- Built with
Midjourney Syntax
For full details on Midjourney prompt syntax—including parameters, ranges, multi-prompts, and reference types (--cref, --sref)—see the Midjourney Prompt Format Specification.
Project Structure
.
├── LICENSE
├── README.md
├── docs
│ ├── specification.md
│ └── ...
├── pyproject.toml
├── src
│ └── midjargon
│ ├── __init__.py
│ ├── __main__.py
│ ├── cli
│ │ └── main.py
│ ├── core
│ │ ├── converter.py
│ │ ├── input.py
│ │ ├── models.py
│ │ ├── parameters.py
│ │ ├── parser.py
│ │ ├── permutations.py
│ │ └── type_defs.py
│ └── engines
│ ├── base.py
│ ├── fal
│ └── midjourney
│ ├── constants.py
│ ├── models.py
│ └── parser.py
└── tests
Contribution Guidelines
Contributions welcome. Follow these steps:
-
Setup:
- Clone the repo:
git clone https://github.com/twardoch/midjargon.git cd midjargon
- Use a virtual environment. With
uv:uv venv uv pip install -e ".[all]"
Or standard tools:python -m venv .venv source .venv/bin/activate pip install -e ".[all]"
- Install
hatchfor task management:pip install hatch
- Clone the repo:
-
Run Tests:
hatch test
-
Format & Lint:
hatch run lint:all # Or: hatch run lint:fmt hatch run lint:style
-
Type Check:
hatch run lint:typing
-
Commit Messages: Use conventional commits:
feat:,fix:, etc. -
Pull Requests:
- Submit to main development branch.
- Include:
- Description of changes.
- Passing tests.
- Lint and type checks.
- Updated docs if needed.
-
Code Style:
- Follow PEP 8 (enforced by Ruff).
- Prefer clarity.
- Use type hints.
- Leverage Pydantic for data validation.
License
MIT. See LICENSE.
Metadata
Release files for midjargon 2.2.4
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 |
|---|---|---|---|---|
| midjargon-2.2.4.tar.gz | 1.2 MB | With provenance | Details for midjargon-2.2.4.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| midjargon-2.2.4-py3-none-any.whl | Python 3 | none | any | Details for midjargon-2.2.4-py3-none-any.whl |
Total release size: 1.3 MB
Release files / midjargon-2.2.4.tar.gz
| Download URL | midjargon-2.2.4.tar.gz |
|---|---|
| Size | 1.2 MB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
f0f61f74a1cb8e4c0e2afa85a1ad2cd1b61894de7950deac4f49a55c85b15491
|
|
BLAKE2b-256 checksum How to use checksums |
1656ef331cd70e62f817d49d882da42d78838e9aff9f248143c6519bcf53dc53
|
| 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 / midjargon-2.2.4-py3-none-any.whl
| Download URL | midjargon-2.2.4-py3-none-any.whl |
|---|---|
| Size | 43.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
f0c1c1837ff7fe516906fe936cac1336cd218856af220c1947b468e2f7cbd28b
|
|
BLAKE2b-256 checksum How to use checksums |
19c2774af41e6b25b79449fbc707c1b33504568b0f517ea9a45265556bc52537
|
| 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 midjargon 2.2.4
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 midjargon-2.2.4.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.