Generate sample data from JSON schema or OAS models
Project description
json_sample_generator
Generate sample data from JSON Schema or OpenAPI (OAS) schemas. Create realistic samples for tests, examples, and fixtures.
Installation
From PyPI:
pip install sample-generator
# importable module name remains `json_sample_generator`
Or with uv:
uv add sample-generator
Quickstart
Prerequisites:
- Python 3.12+
- uv installed
Install uv (Linux/macOS):
curl -LsSf https://astral.sh/uv/install.sh | sh
Set up the project:
# Clone the repo
git clone https://github.com/<your-username>/json_sample_generator.git
cd json_sample_generator
# (Optional) create a virtualenv managed by uv
uv venv # creates .venv/
# Install runtime deps
uv sync
# For development (tests, tools, etc.)
uv sync --group dev
Run tests:
uv run pytest -q
Run examples:
uv run python examples/simple_value_example.py
Developer guide
Code quality tools used by the project
- uv (astral.sh/uv) — virtualenv + task runner used in CI
- ruff — linting (CI runs
uvx ruff check .) - black — formatting (CI runs
uvx black --check .) - isort — import sorting (CI runs
uvx isort --check-only .) - pytest — test runner (CI runs
uv run pytest -q)
Automated-fix hints
- Run ruff's auto-fixer to apply quick lint fixes:
uvx ruff check . --fix - Reformat code with black:
uvx black . - Sort imports with isort:
uvx isort . - Run the full CI steps locally (create venv first):
uv venv uv sync --group dev uvx ruff check . uvx black --check . && uvx isort --check-only . uv run pytest -q
These commands mirror what GitHub Actions runs so you can reproduce and fix CI failures locally.
Build and publish reminders
The repository's publish workflow builds with uv build and uses the pypa/gh-action-pypi-publish action; for first-time releases you may need a PyPI API token (or configure OIDC/trusted publishing on PyPI).
Pre-commit (recommended):
uvx pre-commit install
uvx pre-commit run --all-files
User guide: Scenarios
Scenarios let you override generated values per field path with simple values or callables, and optionally with pattern-based rules. They accept a Context so overrides can depend on other fields.
See the full guide (including default_data) in docs/SCENARIOS.md.
User guide: Loading from OpenAPI (OAS)
To load a component schema from an OpenAPI Specification:
import yaml
from src.json_sample_generator import JSONSchemaGenerator
from src.json_sample_generator.models import Schema
with open("api.yaml") as f:
oas = yaml.safe_load(f)
schema = Schema.from_oas(oas, name="Pet")
gen = JSONSchemaGenerator(schema)
sample = gen.generate()
This correctly resolves cross-component $ref pointers. See the full guide in docs/OPENAPI.md for advanced usage, the jsonref caching details, and when to use from_raw_data vs from_oas.
User guide: Break Scenarios
Break scenarios take a valid generated sample and intentionally corrupt it so that it fails JSON Schema validation — useful for negative-path tests, validator error-message testing, and schema-evolution checks.
See the full guide in docs/BREAK_SCENARIOS.md.
User guide: Capping array size with generator_max_items
When a schema declares a large maxItems (e.g. 10000) the generator will, by default, pick a random length up to that bound and produce that many child elements. For deeply nested schemas this can be very slow and produce huge payloads that are not useful for tests or fixtures.
Pass generator_max_items to the JSONSchemaGenerator constructor to apply a generator-wide upper bound on array length. It does not replace the schema's maxItems; it only caps it from above.
from json_sample_generator import JSONSchemaGenerator
from json_sample_generator.models import Schema
schema = Schema(data={
"type": "object",
"properties": {
"tags": {
"type": "array",
"maxItems": 10000, # schema permits very large arrays
"items": {"type": "string"},
}
},
"required": ["tags"],
})
# Cap every array generated by this instance at 5 elements.
gen = JSONSchemaGenerator(schema, generator_max_items=5)
sample = gen.generate()
assert len(sample["tags"]) <= 5
Algorithm
For each array node, the effective upper bound is computed as:
- If the schema does not declare
maxItems:- use
generator_max_itemswhen it is set, - otherwise fall back to
max(minItems, 2)(the legacy default).
- use
- If the schema does declare
maxItems:- use
min(maxItems, generator_max_items)when the cap is set, - otherwise use
maxItemsas-is.
- use
- The final element count is
random.randint(minItems, max_items).
Consequences:
generator_max_items=None(the default) preserves existing behavior — no global cap is applied.- When a schema omits
maxItems, settinggenerator_max_items=Nlets arrays grow up toN(instead of being silently capped at the2default). - The schema's
minItemsis always respected. If a schema demandsminItems: 10but you setgenerator_max_items=5,random.randint(10, 5)will raiseValueError. Choose a cap that is not lower than anyminItemsyou expect to encounter. - The schema's
maxItemsstill wins when it is smaller than the global cap (min(...)semantics). - Applies to every array node in the schema for that generator instance — there is no per-path override. Use
scenario.overridesif you need to control a specific array's contents.
When to use it
- Generating fixtures from third-party OpenAPI specs that declare unrealistically large
maxItems. - Speeding up property-based tests where the array size is incidental.
- Producing compact sample payloads for documentation or examples.
Contributing
See CONTRIBUTING.md.
License
MIT. See LICENSE.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file sample_generator-0.5.0.tar.gz.
File metadata
- Download URL: sample_generator-0.5.0.tar.gz
- Upload date:
- Size: 28.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a712cec7164dc83dc6b7513760779cef45c6fd14ea53e431cb08a2356be47160
|
|
| MD5 |
d307d28c2660e5964d0bd7e618cbc5d5
|
|
| BLAKE2b-256 |
0942f8ec1b2b42b1451be27ea37b3fa8496e9f9863f388bde1d7f245bd348661
|
Provenance
The following attestation bundles were made for sample_generator-0.5.0.tar.gz:
Publisher:
publish.yml on bartoszm/sample_generator
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
sample_generator-0.5.0.tar.gz -
Subject digest:
a712cec7164dc83dc6b7513760779cef45c6fd14ea53e431cb08a2356be47160 - Sigstore transparency entry: 1469130191
- Sigstore integration time:
-
Permalink:
bartoszm/sample_generator@942616f8bc31d2c6e7c18655587d338ca39119d6 -
Branch / Tag:
refs/tags/v0.5.0 - Owner: https://github.com/bartoszm
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@942616f8bc31d2c6e7c18655587d338ca39119d6 -
Trigger Event:
release
-
Statement type:
File details
Details for the file sample_generator-0.5.0-py3-none-any.whl.
File metadata
- Download URL: sample_generator-0.5.0-py3-none-any.whl
- Upload date:
- Size: 36.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
aa699098b1e8e1ca2159f138c023033f82ed43d1967fba18526de28758e489ef
|
|
| MD5 |
cb4c8879dcfc20bd194af98bfca25365
|
|
| BLAKE2b-256 |
ce5609f5fd1508e7b4d2ee121d4b8cfc284ae6bb3878c915734672694398a825
|
Provenance
The following attestation bundles were made for sample_generator-0.5.0-py3-none-any.whl:
Publisher:
publish.yml on bartoszm/sample_generator
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
sample_generator-0.5.0-py3-none-any.whl -
Subject digest:
aa699098b1e8e1ca2159f138c023033f82ed43d1967fba18526de28758e489ef - Sigstore transparency entry: 1469130340
- Sigstore integration time:
-
Permalink:
bartoszm/sample_generator@942616f8bc31d2c6e7c18655587d338ca39119d6 -
Branch / Tag:
refs/tags/v0.5.0 - Owner: https://github.com/bartoszm
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@942616f8bc31d2c6e7c18655587d338ca39119d6 -
Trigger Event:
release
-
Statement type: