Registry / serialization / jsonargparse

jsonargparse

JSON →
library4.51.0pypypi✓ verified 24d ago

jsonargparse is a Python library that extends `argparse` to simplify the creation of Command-Line Interfaces (CLIs) and make Python applications easily configurable. It allows parsing configuration options from command line arguments, config files (JSON, YAML, Jsonnet, TOML), and environment variables, leveraging type hints for validation. It is a well-maintained project with frequent releases, adhering to high standards of development, including semantic versioning, deprecation periods, changelog, automated testing, and full test coverage.

pip install jsonargparse
INSTALL
IMPORT
SIG · JSONARGPARSE
J
jsonargparse
serializationpythonv4.51.0
Install
3.7s avg
Import
341ms
Disk
64MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v4.51.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.910 runs
installs and imports cleanly · install 0.0s · import 0.349s · 63.5MB
glibc
py 3.103.910 runs
installs and imports cleanly · install 3.7s · import 0.333s · 67MB
64MB installed
● package 64MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

ArgumentParser
from jsonargparse import ArgumentParser
CLI
from jsonargparse import CLI
from jsonargparse.cli import CLI
While 'CLI' originates from 'jsonargparse.cli', the public API encourages importing directly from the top-level 'jsonargparse' module to ensure stability against internal refactoring.
auto_cli
from jsonargparse import auto_cli
from jsonargparse.cli import auto_cli
Similar to CLI, 'auto_cli' is exposed at the top level for public API stability.

The simplest way to create a CLI is by using the `CLI()` function with a type-hinted Python function. `jsonargparse` automatically generates arguments, validates types, and uses docstrings for help messages.

from jsonargparse import CLI def main(name: str, greeting: str = 'Hello') -> None: """ A simple command-line interface function. Args: name: The name of the person to greet. greeting: The greeting message to use. """ print(f'{greeting}, {name}!') if __name__ == '__main__': # Run this from the command line: # python your_script.py --name World # python your_script.py --name Alice --greeting Hi CLI(main)
Debug
Known issues
breakingIn `jsonargparse` v4.0.0, the default error handling behavior of `ArgumentParser` changed. It now raises a `ParserError` by default on parsing failure instead of printing usage and exiting the program. Additionally, `error_handler` and `formatter_class` arguments no longer accept string values.
fix
If you relied on the `argparse`-like default exit-on-error behavior, you can re-enable it by setting `error_handler=usage_and_exit_error_handler` during `ArgumentParser` instantiation. For `error_handler` and `formatter_class`, pass callable objects directly instead of strings.
affects: >=4.0.0
breakingThe internal `SimpleNamespace` class was replaced by `Namespace` in `jsonargparse` v4.0.0 to align more closely with `argparse`. Code directly interacting with `jsonargparse.namespace.SimpleNamespace` might break.
fix
Update imports and class references from `SimpleNamespace` to `Namespace`. Ensure your code handles `Namespace` objects appropriately for parsed arguments.
affects: >=4.0.0
gotchaUsing `type=bool` directly in `add_argument` for boolean flags in standard `argparse` (and by extension, potentially `jsonargparse` if not careful) does not behave as expected for command-line parsing, as it treats any non-empty string as `True`. `jsonargparse` provides `ActionYesNo` for clear boolean handling.
fix
For boolean arguments, use `parser.add_argument('--flag', action=ActionYesNo)` or `type=bool` with a default value, which `jsonargparse` handles correctly with `--flag`, `--no-flag`, or environment variables like `FLAG=true/false`.
affects: All
gotchaThe `ArgumentParser.parse_known_args()` method is intentionally not implemented in `jsonargparse` to prevent typos in configuration files or arguments from going unnoticed. If `parse_known_args` were allowed, unknown arguments would be silently ignored, potentially hiding critical configuration errors.
fix
Design your CLIs to have all arguments explicitly defined or use subcommands. If you truly need to ignore unknown arguments, you might need to process `sys.argv` manually before passing to `jsonargparse` or implement custom parsing logic, but this is generally discouraged for robust applications.
affects: All
gotchaBy default, `ArgumentParser.parse_args()` does not check environment variables. You must explicitly enable this behavior.
fix
To enable environment variable parsing for `parse_args()`, initialize your parser with `ArgumentParser(default_env=True)`. Alternatively, use `parser.parse_env()` to parse only environment variables.
affects: All
Errors
Common errors & fixes
jsonargparse.util.ParserError: Error raised when parsing a value fails.
A value provided (via command line, config file, or environment variable) does not conform to the expected type or format defined for the argument.
fix
Review the argument's type hint and the input value to ensure they are compatible. Adjust the input value or the argument definition as needed.
TypeError: File is not writeable
An argument defined with a `Path_` type (e.g., `Path_frw`, `Path_dw`) was given a file path that does not have the specified write permissions, or similar `TypeError` for `Path_` types when the path does not meet existence/permission requirements.
fix
Ensure the specified file or directory exists and possesses the necessary read/write permissions matching the `Path_` type used in the argument definition (e.g., `Path_fr` for readable file, `Path_dw` for writeable directory).
ModuleNotFoundError: No module named 'jsonschema'
This error occurs when attempting to use features that rely on the `jsonschema` package (such as `type=bool` for schema validation, `ActionJsonSchema`, or Jsonnet support) without having it installed as an optional dependency.
fix
Install `jsonargparse` with the `jsonschema` extra: `pip install "jsonargparse[jsonschema]"` or `pip install "jsonargparse[all]"` if other optional features are also needed.
jsonargparse.ArgumentError: argument <ARG_NAME>: <MESSAGE>
An error occurred during the definition of an argument (e.g., conflicting options, invalid parameters in `add_argument`) or due to a missing required argument when parsing inputs.
fix
Examine the `parser.add_argument()` call for the argument specified in the error message, verifying type hints, default values, and valid parameters. If it's a runtime error, ensure all required arguments are provided.
Upgrade
Version history
4.51.0latest on PyPI · released Aug 20, 2026
Audit
Dependencies
PyYAMLrequiredDefault dependency for YAML config file support.
jsonschemaoptionalRequired for JSON Schema validation features.
jsonnetoptionalRequired for Jsonnet config file support.
validatorsoptionalRequired for URL parsing functionality.
requestsoptionalRequired for URL parsing functionality (e.g., checking accessibility).
argcompleteoptionalRequired for command-line tab completion.
shtaboptionalRequired for advanced command-line tab completion (alternative to argcomplete).
Agent activity
8 hits · last 30 days
node
6
Resources